simple_attribute_mapper 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +7 -0
 - data/README.md +12 -3
 - data/lib/simple_attribute_mapper/mapper.rb +7 -4
 - data/lib/simple_attribute_mapper/version.rb +1 -1
 - data/lib/simple_attribute_mapper.rb +9 -2
 - data/spec/simple_attribute_mapper_spec.rb +20 -1
 - metadata +4 -3
 
    
        data/.travis.yml
    ADDED
    
    
    
        data/README.md
    CHANGED
    
    | 
         @@ -1,5 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # SimpleAttributeMapper
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
      
 3 
     | 
    
         
            +
            [](https://travis-ci.org/house9/simple_attribute_mapper) [](https://codeclimate.com/github/house9/simple_attribute_mapper)
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
       3 
5 
     | 
    
         
             
            Maps attributes values from one object to another
         
     | 
| 
       4 
6 
     | 
    
         | 
| 
       5 
7 
     | 
    
         
             
            ## Installation
         
     | 
| 
         @@ -23,7 +25,7 @@ class Person 
     | 
|
| 
       23 
25 
     | 
    
         
             
              include Virtus
         
     | 
| 
       24 
26 
     | 
    
         | 
| 
       25 
27 
     | 
    
         
             
              attribute :first_name, String
         
     | 
| 
       26 
     | 
    
         
            -
               
     | 
| 
      
 28 
     | 
    
         
            +
              attribute :last_name, String
         
     | 
| 
       27 
29 
     | 
    
         
             
              attribute :email, String
         
     | 
| 
       28 
30 
     | 
    
         
             
              attribute :home_phone, String
         
     | 
| 
       29 
31 
     | 
    
         
             
              attribute :mailing_address, String
         
     | 
| 
         @@ -49,7 +51,14 @@ specify additional mappings, i.e. user_name -> email 
     | 
|
| 
       49 
51 
     | 
    
         
             
            user = User.find(0000)
         
     | 
| 
       50 
52 
     | 
    
         
             
            user.user_name # => "test@example.com"
         
     | 
| 
       51 
53 
     | 
    
         
             
            mapper = SimpleAttributeMapper::Mapper.new({:user_name => :email})
         
     | 
| 
       52 
     | 
    
         
            -
             
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
            # map returns a new instance of Person
         
     | 
| 
      
 56 
     | 
    
         
            +
            person = mapper.map(user, Person)
         
     | 
| 
      
 57 
     | 
    
         
            +
            person.email # => "test@example.com"
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
            # map_attributes will map onto an existing instance
         
     | 
| 
      
 60 
     | 
    
         
            +
            person = Person.new({email: "foo@bar.com"})
         
     | 
| 
      
 61 
     | 
    
         
            +
            person = mapper.map_attributes(user, person)
         
     | 
| 
       53 
62 
     | 
    
         
             
            person.email # => "test@example.com"
         
     | 
| 
       54 
63 
     | 
    
         
             
            ```
         
     | 
| 
       55 
64 
     | 
    
         | 
| 
         @@ -63,7 +72,7 @@ SimpleAttributeMapper.configure do |config| 
     | 
|
| 
       63 
72 
     | 
    
         
             
              config << SimpleAttributeMapper.from(User).to(Person).with({:user_name => :email}).with({:phone_number => :home_phone})
         
     | 
| 
       64 
73 
     | 
    
         
             
            end
         
     | 
| 
       65 
74 
     | 
    
         | 
| 
       66 
     | 
    
         
            -
            # use later in application to map 
     | 
| 
      
 75 
     | 
    
         
            +
            # use later in application to map
         
     | 
| 
       67 
76 
     | 
    
         
             
            user = User.find(0000)
         
     | 
| 
       68 
77 
     | 
    
         
             
            person = SimpleAttributeMapper.map(user, Person)
         
     | 
| 
       69 
78 
     | 
    
         | 
| 
         @@ -7,13 +7,16 @@ module SimpleAttributeMapper 
     | 
|
| 
       7 
7 
     | 
    
         
             
                attr_reader :mappings
         
     | 
| 
       8 
8 
     | 
    
         | 
| 
       9 
9 
     | 
    
         
             
                def map(source, target_class)
         
     | 
| 
      
 10 
     | 
    
         
            +
                  map_attributes(source, target_class.new)
         
     | 
| 
      
 11 
     | 
    
         
            +
                end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                def map_attributes(source, target_instance)
         
     | 
| 
       10 
14 
     | 
    
         
             
                  raise UnMappableError.new("source has no attributes") unless source.respond_to?(:attributes)
         
     | 
| 
       11 
15 
     | 
    
         | 
| 
       12 
     | 
    
         
            -
                   
     | 
| 
       13 
     | 
    
         
            -
                   
     | 
| 
       14 
     | 
    
         
            -
                  map_specified_attributes(source, target)
         
     | 
| 
      
 16 
     | 
    
         
            +
                  map_matching_attributes(source, target_instance)
         
     | 
| 
      
 17 
     | 
    
         
            +
                  map_specified_attributes(source, target_instance)
         
     | 
| 
       15 
18 
     | 
    
         | 
| 
       16 
     | 
    
         
            -
                   
     | 
| 
      
 19 
     | 
    
         
            +
                  target_instance
         
     | 
| 
       17 
20 
     | 
    
         
             
                end
         
     | 
| 
       18 
21 
     | 
    
         | 
| 
       19 
22 
     | 
    
         
             
                def map_matching_attributes(source, target)
         
     | 
| 
         @@ -21,6 +21,14 @@ module SimpleAttributeMapper 
     | 
|
| 
       21 
21 
     | 
    
         
             
              end
         
     | 
| 
       22 
22 
     | 
    
         | 
| 
       23 
23 
     | 
    
         
             
              def self.map(source_instance, target_class)
         
     | 
| 
      
 24 
     | 
    
         
            +
                mapper_for(source_instance, target_class).map(source_instance, target_class)
         
     | 
| 
      
 25 
     | 
    
         
            +
              end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
              def self.map_attributes(source_instance, target_instance)
         
     | 
| 
      
 28 
     | 
    
         
            +
                mapper_for(source_instance, target_instance.class).map_attributes(source_instance, target_instance)
         
     | 
| 
      
 29 
     | 
    
         
            +
              end
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
              def self.mapper_for(source_instance, target_class)
         
     | 
| 
       24 
32 
     | 
    
         
             
                if self.configuration.maps.length == 0
         
     | 
| 
       25 
33 
     | 
    
         
             
                  raise ConfigurationError.new("There are no mappings configured, check the documentation for configuration steps")
         
     | 
| 
       26 
34 
     | 
    
         
             
                end
         
     | 
| 
         @@ -30,7 +38,6 @@ module SimpleAttributeMapper 
     | 
|
| 
       30 
38 
     | 
    
         
             
                  raise ConfigurationError.new("There are no mappings configured for '#{source_instance.class}' -> '#{target_class}'")
         
     | 
| 
       31 
39 
     | 
    
         
             
                end
         
     | 
| 
       32 
40 
     | 
    
         | 
| 
       33 
     | 
    
         
            -
                 
     | 
| 
       34 
     | 
    
         
            -
                mapper.map(source_instance, target_class)
         
     | 
| 
      
 41 
     | 
    
         
            +
                Mapper.new(mappings[0].mappings)
         
     | 
| 
       35 
42 
     | 
    
         
             
              end
         
     | 
| 
       36 
43 
     | 
    
         
             
            end
         
     | 
| 
         @@ -47,6 +47,17 @@ describe SimpleAttributeMapper do 
     | 
|
| 
       47 
47 
     | 
    
         
             
                  target_instance.abc.should == "ABC"
         
     | 
| 
       48 
48 
     | 
    
         
             
                  target_instance.target_baz.should == "BAZ"
         
     | 
| 
       49 
49 
     | 
    
         
             
                end
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
                it "maps attributes on instance" do
         
     | 
| 
      
 52 
     | 
    
         
            +
                  maps = [ SimpleAttributeMapper.from(source).to(target).with({source_baz: :target_baz}) ]
         
     | 
| 
      
 53 
     | 
    
         
            +
                  SimpleAttributeMapper.configuration.stub(:maps).and_return(maps)
         
     | 
| 
      
 54 
     | 
    
         
            +
                  source_instance = source.new(foo: "FOO", abc: "ABC", source_baz: "BAZ")
         
     | 
| 
      
 55 
     | 
    
         
            +
                  target_instance = target.new(abc: "ABC", target_baz: "ORIGINAL")
         
     | 
| 
      
 56 
     | 
    
         
            +
                  target_instance = SimpleAttributeMapper.map_attributes(source_instance, target_instance)
         
     | 
| 
      
 57 
     | 
    
         
            +
                  target_instance.bar.should be_nil
         
     | 
| 
      
 58 
     | 
    
         
            +
                  target_instance.abc.should == "ABC"
         
     | 
| 
      
 59 
     | 
    
         
            +
                  target_instance.target_baz.should == "BAZ"
         
     | 
| 
      
 60 
     | 
    
         
            +
                end
         
     | 
| 
       50 
61 
     | 
    
         
             
              end
         
     | 
| 
       51 
62 
     | 
    
         | 
| 
       52 
63 
     | 
    
         
             
              describe "configure" do
         
     | 
| 
         @@ -134,7 +145,7 @@ describe SimpleAttributeMapper::Mapper do 
     | 
|
| 
       134 
145 
     | 
    
         
             
                SimpleAttributeMapper::Mapper.new.should be_an_instance_of(SimpleAttributeMapper::Mapper)
         
     | 
| 
       135 
146 
     | 
    
         
             
              end
         
     | 
| 
       136 
147 
     | 
    
         | 
| 
       137 
     | 
    
         
            -
              describe "map" do
         
     | 
| 
      
 148 
     | 
    
         
            +
              describe "map and map_attributes" do
         
     | 
| 
       138 
149 
     | 
    
         
             
                class Country
         
     | 
| 
       139 
150 
     | 
    
         
             
                  include Virtus
         
     | 
| 
       140 
151 
     | 
    
         | 
| 
         @@ -183,6 +194,14 @@ describe SimpleAttributeMapper::Mapper do 
     | 
|
| 
       183 
194 
     | 
    
         
             
                  bar.baz.should == "BAZ"
         
     | 
| 
       184 
195 
     | 
    
         
             
                end
         
     | 
| 
       185 
196 
     | 
    
         | 
| 
      
 197 
     | 
    
         
            +
                it "maps attributes on an existing instance" do
         
     | 
| 
      
 198 
     | 
    
         
            +
                  mapper = SimpleAttributeMapper::Mapper.new
         
     | 
| 
      
 199 
     | 
    
         
            +
                  foo = Foo.new({baz: "BAZ"})
         
     | 
| 
      
 200 
     | 
    
         
            +
                  bar = Bar.new({baz: "ORIGINAL"})
         
     | 
| 
      
 201 
     | 
    
         
            +
                  bar = mapper.map_attributes(foo, bar)
         
     | 
| 
      
 202 
     | 
    
         
            +
                  bar.baz.should == "BAZ"
         
     | 
| 
      
 203 
     | 
    
         
            +
                end
         
     | 
| 
      
 204 
     | 
    
         
            +
             
     | 
| 
       186 
205 
     | 
    
         
             
                context "mapping nested attributes" do
         
     | 
| 
       187 
206 
     | 
    
         
             
                  it "maps 1 level deep" do
         
     | 
| 
       188 
207 
     | 
    
         
             
                    mapper = SimpleAttributeMapper::Mapper.new({[:address, :city] => :city})
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: simple_attribute_mapper
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.3
         
     | 
| 
       5 
5 
     | 
    
         
             
              prerelease: 
         
     | 
| 
       6 
6 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       7 
7 
     | 
    
         
             
            authors:
         
     | 
| 
         @@ -9,7 +9,7 @@ authors: 
     | 
|
| 
       9 
9 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       10 
10 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       11 
11 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       12 
     | 
    
         
            -
            date:  
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2013-01-30 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: rspec
         
     | 
| 
         @@ -52,6 +52,7 @@ extensions: [] 
     | 
|
| 
       52 
52 
     | 
    
         
             
            extra_rdoc_files: []
         
     | 
| 
       53 
53 
     | 
    
         
             
            files:
         
     | 
| 
       54 
54 
     | 
    
         
             
            - .gitignore
         
     | 
| 
      
 55 
     | 
    
         
            +
            - .travis.yml
         
     | 
| 
       55 
56 
     | 
    
         
             
            - Gemfile
         
     | 
| 
       56 
57 
     | 
    
         
             
            - LICENSE.txt
         
     | 
| 
       57 
58 
     | 
    
         
             
            - README.md
         
     | 
| 
         @@ -87,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       87 
88 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       88 
89 
     | 
    
         
             
            requirements: []
         
     | 
| 
       89 
90 
     | 
    
         
             
            rubyforge_project: 
         
     | 
| 
       90 
     | 
    
         
            -
            rubygems_version: 1.8. 
     | 
| 
      
 91 
     | 
    
         
            +
            rubygems_version: 1.8.24
         
     | 
| 
       91 
92 
     | 
    
         
             
            signing_key: 
         
     | 
| 
       92 
93 
     | 
    
         
             
            specification_version: 3
         
     | 
| 
       93 
94 
     | 
    
         
             
            summary: See the README for more information
         
     |