This repository has been archived by the owner on Aug 17, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Markus Schirp
committed
Jun 5, 2012
1 parent
9baf1cd
commit 92e2724
Showing
7 changed files
with
201 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
--- | ||
threshold: 14 | ||
total_score: 211 | ||
total_score: 203 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
spec/unit/mapper/attribute/class_methods/determine_type_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'spec_helper' | ||
|
||
describe Mapper::Attribute, '.determine_type' do | ||
let(:object) { described_class } | ||
|
||
subject { object.determine_type(class_or_name) } | ||
|
||
context 'with ::Object' do | ||
let(:class_or_name) { ::Object } | ||
|
||
it 'should return Mapper::Attribute::Object' do | ||
should be(Mapper::Attribute::Object) | ||
end | ||
end | ||
|
||
context 'with undeterminable type' do | ||
let(:class_or_name) { Object.new } | ||
|
||
it 'should raise error' do | ||
expect { subject }.to raise_error(ArgumentError,"unable to determine type from: #{class_or_name.inspect}") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
require 'spec_helper' | ||
|
||
describe 'spike spec' do | ||
class Phone | ||
include Virtus::ValueObject | ||
|
||
attribute :number,String | ||
attribute :type,String | ||
|
||
class Mapper | ||
include ::Mapper | ||
model Phone | ||
map :number, Object | ||
map :type, Object | ||
end | ||
|
||
class LoaderByHand | ||
def initialize(dump) | ||
@dump = dump | ||
end | ||
|
||
def number | ||
@dump.fetch(:number) | ||
end | ||
|
||
def type | ||
@dump.fetch(:type) | ||
end | ||
|
||
def load | ||
Phone.new( | ||
:number => number, | ||
:type => type | ||
) | ||
end | ||
end | ||
|
||
class DumperByHand | ||
def initialize(phone) | ||
@phone = phone | ||
end | ||
|
||
def phone | ||
@phone.number | ||
end | ||
|
||
def type | ||
@phone.type | ||
end | ||
|
||
def dump | ||
{ | ||
:number => number, | ||
:type => type | ||
} | ||
end | ||
end | ||
end | ||
|
||
class Address | ||
include Virtus::ValueObject | ||
|
||
attribute :lines,String | ||
attribute :postcode,String | ||
|
||
class Mapper | ||
include ::Mapper | ||
model Address | ||
map :lines, Object | ||
map :postcode, Object | ||
end | ||
end | ||
|
||
class User | ||
include Virtus::ValueObject | ||
|
||
attribute :firstname,String | ||
attribute :lastname,String | ||
attribute :address,Address | ||
attribute :phones,Array[Phone] | ||
attribute :preferred_phone,Phone | ||
attribute :vat_rate,Rational | ||
|
||
class Mapper | ||
include ::Mapper | ||
model ::User | ||
|
||
map :firstname, Object, :to => :surname | ||
map :lastname, Object | ||
map :address, EmbeddedDocument, :mapper => Address::Mapper | ||
map :phones, EmbeddedCollection, :mapper => Phone::Mapper | ||
map :vat_rate, Custom, :to => [:vat_rate_numerator,:vat_rate_denominator,:vat_rate] | ||
map :preferred_phone, Custom, :to => :preferred_phone_idx | ||
|
||
class Dumper | ||
def preferred_phone_idx | ||
object.phones.index(object.preferred_phone) | ||
end | ||
|
||
def vat_rate_numerator | ||
with_vat_rate(&:numerator) | ||
end | ||
|
||
def vat_rate_denominator | ||
with_vat_rate(&:denominator) | ||
end | ||
|
||
def vat_rate | ||
with_vat_rate(&:to_f) | ||
end | ||
|
||
def with_vat_rate | ||
vat_rate = object.vat_rate | ||
yield vat_rate if vat_rate | ||
end | ||
end | ||
|
||
class Loader | ||
def vat_rate | ||
Rational(@dump[:vat_rate_numerator],@dump[:vat_rate_denominator]) | ||
end | ||
|
||
def preferred_phone | ||
prefered_phone_idx = @dump[:preferred_phone_idx] | ||
if prefered_phone_idx | ||
phones.fetch(prefered_phone_idx) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
it 'should work' do | ||
phone_a = Phone.new(:type => :home, :number => '0815 - 1') | ||
phone_b = Phone.new(:type => :mobile,:number => '0815 - 2') | ||
|
||
address = Address.new( | ||
:lines => "Snake Oil Ink\nPostbox foo bar", | ||
:postcode => "085123" | ||
) | ||
|
||
user = User.new( | ||
:firstname => 'John', | ||
:lastname => 'Doe', | ||
:address => address, | ||
:phones => [phone_a,phone_b], | ||
:preferred_phone => phone_b, | ||
:vat_rate => Rational(21,100) | ||
) | ||
|
||
dump = User::Mapper.dump(user) | ||
|
||
dump.should == { | ||
:surname =>'John', | ||
:lastname =>'Doe', | ||
:address =>{:lines=>"Snake Oil Ink\nPostbox foo bar", :postcode=>'085123'}, | ||
:phones => | ||
[ | ||
{:number=>"0815 - 1", :type=>"home"}, | ||
{:number=>"0815 - 2", :type=>"mobile"} | ||
], | ||
:vat_rate_numerator => 21, | ||
:vat_rate_denominator => 100, | ||
:vat_rate => 0.21, | ||
:preferred_phone_idx=>1 | ||
} | ||
|
||
loaded = User::Mapper.load(dump) | ||
loaded.should == user | ||
end | ||
end |
Oops, something went wrong.