simple_attribute_mapper 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_attribute_mapper.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jesse House
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # SimpleAttributeMapper
2
+
3
+ Maps attributes values from one object to another
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'simple_attribute_mapper'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install simple_attribute_mapper
18
+
19
+ ## Usage
20
+
21
+ ```
22
+ class Person
23
+ include Virtus
24
+
25
+ attribute :first_name, String
26
+ atrribute :last_name, String
27
+ attribute :email, String
28
+ attribute :home_phone, String
29
+ end
30
+
31
+ class User < ActiveRecord::Base
32
+ # first_name
33
+ # last_name
34
+ # user_name
35
+ # phone_number
36
+ end
37
+ ```
38
+
39
+ first_name and last_name will be automatically mapped
40
+
41
+ specify additional mappings, i.e. user_name -> email
42
+
43
+ ```
44
+ user = User.find(0000)
45
+ user.user_name # => "test@example.com"
46
+ mapper = SimpleAttributeMapper::Mapper.new({:user_name => :email})
47
+ person = mapper.map(user, Person) # returns a new instance of Person
48
+ person.email # => "test@example.com"
49
+ ```
50
+
51
+ ## TODO
52
+
53
+ * configure the mappings, i.e.
54
+
55
+ ```
56
+ # configure once
57
+ SimpleAttributeMapper.from(User).to(Person).with({:user_name => :email}).with({:phone_number => :home_phone})
58
+ # use later
59
+ person = SimpleAttributeMapper.map(user, Person)
60
+ ```
61
+
62
+ * nested attribute mappings
63
+ * composite mappings through lambdas
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ module SimpleAttributeMapper
2
+ class Map
3
+ def initialize(source_class)
4
+ @source_class = source_class
5
+ @mappings = {}
6
+ end
7
+
8
+ attr_accessor :target_class
9
+ attr_accessor :source_class
10
+ attr_accessor :mappings
11
+
12
+ def to(target_class)
13
+ @target_class = target_class
14
+ self
15
+ end
16
+
17
+ def with(args)
18
+ mappings.merge!(args)
19
+ self
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,34 @@
1
+ module SimpleAttributeMapper
2
+ class Mapper
3
+ def initialize(mappings = {})
4
+ @mappings = mappings
5
+ end
6
+
7
+ attr_reader :mappings
8
+
9
+ def map(source, target_class)
10
+ raise UnMappableError.new("source has no attributes") unless source.respond_to?(:attributes)
11
+
12
+ target = target_class.new
13
+
14
+ source.attributes.each do |attribute|
15
+ attribute_writer = "#{attribute[0]}=".to_sym
16
+ attribute_value = attribute[1]
17
+
18
+ if target.respond_to?(attribute_writer)
19
+ target.send(attribute_writer, attribute_value)
20
+ end
21
+ end
22
+
23
+ mappings.each do |source_attribute, target_attribute|
24
+ attribute_writer = "#{target_attribute}=".to_sym
25
+ attribute_value = source.send(source_attribute)
26
+ # puts attribute_writer
27
+ # puts attribute_value
28
+ target.send(attribute_writer, attribute_value)
29
+ end
30
+
31
+ target
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,4 @@
1
+ module SimpleAttributeMapper
2
+ class UnMappableError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleAttributeMapper
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,14 @@
1
+ require "simple_attribute_mapper/version"
2
+ require "simple_attribute_mapper/mapper"
3
+ require "simple_attribute_mapper/un_mappable_error"
4
+ require "simple_attribute_mapper/map"
5
+
6
+ module SimpleAttributeMapper
7
+ def self.from(source_class)
8
+ Map.new(source_class)
9
+ end
10
+
11
+ def self.map(source_instance, target_class)
12
+ raise "TODO: implement"
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_attribute_mapper/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "simple_attribute_mapper"
8
+ gem.version = SimpleAttributeMapper::VERSION
9
+ gem.authors = ["Jesse House"]
10
+ gem.email = ["jesse.house@gmail.com"]
11
+ gem.description = %q{Maps attribute values from one object to another}
12
+ gem.summary = %q{See the README for more information}
13
+ gem.homepage = "https://github.com/house9/simple_attribute_mapper"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'virtus'
23
+ end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleAttributeMapper do
4
+ describe "from" do
5
+ it "returns map" do
6
+ source = Struct.new(:foo)
7
+ map = SimpleAttributeMapper.from(source).should be_an_instance_of(SimpleAttributeMapper::Map)
8
+ end
9
+ end
10
+ end
11
+
12
+ describe SimpleAttributeMapper::Map do
13
+ let(:source) { Struct.new(:foo) }
14
+ let(:target) { Struct.new(:bar) }
15
+
16
+ context "returns the same map" do
17
+ it "to" do
18
+ map = SimpleAttributeMapper::Map.new(source)
19
+ to = map.to(target)
20
+ to.should be_an_instance_of(SimpleAttributeMapper::Map)
21
+ to.should == map
22
+ end
23
+
24
+ it "with" do
25
+ map = SimpleAttributeMapper::Map.new(source)
26
+ with = map.with({})
27
+ with.should be_an_instance_of(SimpleAttributeMapper::Map)
28
+ with.should == map
29
+ end
30
+ end
31
+
32
+ describe "with" do
33
+ it "adds mapping" do
34
+ map = SimpleAttributeMapper::Map.new(source)
35
+ map.with({:a => :b})
36
+ map.mappings.has_key?(:a).should be_true
37
+ map.with({:x => :y})
38
+ map.mappings.has_key?(:x).should be_true
39
+ end
40
+ end
41
+ end
42
+
43
+ describe SimpleAttributeMapper::Mapper do
44
+ it "can be" do
45
+ SimpleAttributeMapper::Mapper.new.should be_an_instance_of(SimpleAttributeMapper::Mapper)
46
+ end
47
+
48
+ describe "map" do
49
+ class Foo
50
+ include Virtus
51
+
52
+ attribute :baz, String
53
+ attribute :foo, String
54
+ attribute :abc, String
55
+ end
56
+
57
+ class Bar
58
+ include Virtus
59
+
60
+ attribute :baz, String
61
+ attribute :xyz, String
62
+ end
63
+
64
+ it "raises error when no source has no attributes" do
65
+ expect { SimpleAttributeMapper::Mapper.new.map(Object.new, Bar) }.to raise_error(SimpleAttributeMapper::UnMappableError)
66
+ end
67
+
68
+ it "maps attributes" do
69
+ mapper = SimpleAttributeMapper::Mapper.new
70
+ foo = Foo.new({baz: "BAZ"})
71
+ bar = mapper.map(foo, Bar)
72
+ bar.baz.should == "BAZ"
73
+ end
74
+
75
+ it "does not map when no target attribute" do
76
+ mapper = SimpleAttributeMapper::Mapper.new
77
+ foo = Foo.new({baz: "BAZ", foo: "FOO"})
78
+ bar = mapper.map(foo, Bar)
79
+ bar.baz.respond_to?(:foo).should be_false
80
+ end
81
+
82
+ it "maps specified attributes" do
83
+ mapper = SimpleAttributeMapper::Mapper.new({:abc => :xyz})
84
+ foo = Foo.new({baz: "BAZ", foo: "FOO", abc: "ABC"})
85
+ bar = mapper.map(foo, Bar)
86
+ bar.xyz.should == "ABC"
87
+ end
88
+ end
89
+ end
90
+
91
+ describe "Building a mapping" do
92
+ let(:source) { Struct.new(:foo) }
93
+ let(:target) { Struct.new(:bar) }
94
+
95
+ it "is built" do
96
+ map = SimpleAttributeMapper.from(source).to(target).with({foo: :bar}).with(xyz: :abc)
97
+ map.should be_an_instance_of(SimpleAttributeMapper::Map)
98
+ end
99
+ end
@@ -0,0 +1,2 @@
1
+ require 'simple_attribute_mapper'
2
+ require 'virtus'
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_attribute_mapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jesse House
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: virtus
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Maps attribute values from one object to another
47
+ email:
48
+ - jesse.house@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/simple_attribute_mapper.rb
59
+ - lib/simple_attribute_mapper/map.rb
60
+ - lib/simple_attribute_mapper/mapper.rb
61
+ - lib/simple_attribute_mapper/un_mappable_error.rb
62
+ - lib/simple_attribute_mapper/version.rb
63
+ - simple_attribute_mapper.gemspec
64
+ - spec/simple_attribute_mapper_spec.rb
65
+ - spec/spec_helper.rb
66
+ homepage: https://github.com/house9/simple_attribute_mapper
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.23
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: See the README for more information
90
+ test_files:
91
+ - spec/simple_attribute_mapper_spec.rb
92
+ - spec/spec_helper.rb