domain_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 +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +8 -0
- data/domain_mapper.gemspec +24 -0
- data/lib/domain_mapper.rb +39 -0
- data/lib/domain_mapper/version.rb +3 -0
- data/test/map_test.rb +53 -0
- data/test/test_helper.rb +2 -0
- metadata +112 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 Jan Filipowski
|
|
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,38 @@
|
|
|
1
|
+
# DomainMapper
|
|
2
|
+
|
|
3
|
+
DomainMapper let you map data to your domain objects and represent your domain objects as data.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
gem 'domain_mapper'
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install domain_mapper
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
How to define map:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
class DomainClass
|
|
25
|
+
def initialize
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
map = DomainMapper::Map.new(DomainClass) do
|
|
30
|
+
attribute :instance_variable_name, from: "key value"
|
|
31
|
+
end
|
|
32
|
+
hash = {
|
|
33
|
+
"key value" => "value"
|
|
34
|
+
}
|
|
35
|
+
object = map.build_object(hash)
|
|
36
|
+
object.is_a?(DomainClass) #=> true
|
|
37
|
+
object.instance_variable_get(:@instance_variable_name) #=> "value"
|
|
38
|
+
```
|
data/Rakefile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'domain_mapper/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "domain_mapper"
|
|
8
|
+
spec.version = DomainMapper::VERSION
|
|
9
|
+
spec.authors = ["Jan Filipowski"]
|
|
10
|
+
spec.email = ["jachuf@gmail.com"]
|
|
11
|
+
spec.description = %q{Map any hash to domain object using reflection.}
|
|
12
|
+
spec.summary = %q{Map any hash to domain object using reflection.}
|
|
13
|
+
spec.homepage = ""
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files`.split($/)
|
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
|
22
|
+
spec.add_development_dependency "rake"
|
|
23
|
+
spec.add_development_dependency "minitest"
|
|
24
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require "domain_mapper/version"
|
|
2
|
+
|
|
3
|
+
module DomainMapper
|
|
4
|
+
class Map
|
|
5
|
+
def initialize(object_class, &block)
|
|
6
|
+
@object_class = object_class
|
|
7
|
+
@rules = []
|
|
8
|
+
instance_eval &block
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def attribute(attribute_name, hash_key)
|
|
12
|
+
@rules << [attribute_name, hash_key]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def build_object(data_hash)
|
|
16
|
+
object = @object_class.new
|
|
17
|
+
@rules.each do |attribute_name, hash_key|
|
|
18
|
+
instance_variable_name = instance_variable_symbol(attribute_name)
|
|
19
|
+
value = data_hash[hash_key]
|
|
20
|
+
object.instance_variable_set(instance_variable_name, value)
|
|
21
|
+
end
|
|
22
|
+
object
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def serialize(object)
|
|
26
|
+
fields = []
|
|
27
|
+
@rules.each do |attribute_name, hash_key|
|
|
28
|
+
instance_variable_name = instance_variable_symbol(attribute_name)
|
|
29
|
+
value = object.instance_variable_get(instance_variable_name)
|
|
30
|
+
fields << [hash_key, value]
|
|
31
|
+
end
|
|
32
|
+
Hash[fields]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def instance_variable_symbol(name)
|
|
36
|
+
"@#{name}".to_sym
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
data/test/map_test.rb
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
require 'domain_mapper'
|
|
3
|
+
|
|
4
|
+
module DomainMapper
|
|
5
|
+
class DemoClass
|
|
6
|
+
attr_reader :first_instance_variable, :other_instance_variable
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
describe Map do
|
|
10
|
+
describe "flat data hash" do
|
|
11
|
+
before do
|
|
12
|
+
@map = Map.new(DemoClass) do
|
|
13
|
+
attribute :first_instance_variable, "hash key"
|
|
14
|
+
attribute :other_instance_variable, "other hash key"
|
|
15
|
+
end
|
|
16
|
+
@data_hash = {
|
|
17
|
+
"hash key" => "first value",
|
|
18
|
+
"other hash key" => 4
|
|
19
|
+
}
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe "#build_object" do
|
|
23
|
+
it "should build object of given type" do
|
|
24
|
+
assert_instance_of DemoClass, @map.build_object(@data_hash)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "should set given instance variables" do
|
|
28
|
+
object = @map.build_object(@data_hash)
|
|
29
|
+
assert_equal "first value", object.first_instance_variable
|
|
30
|
+
assert_equal 4, object.other_instance_variable
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
describe "#serialize" do
|
|
35
|
+
before do
|
|
36
|
+
@object = DemoClass.new
|
|
37
|
+
@object.instance_variable_set(:@first_instance_variable, "kaka")
|
|
38
|
+
@object.instance_variable_set(:@other_instance_variable, 1)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "should return hash" do
|
|
42
|
+
assert_instance_of Hash, @map.serialize(@object)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "should have declared keys with values from instance variables" do
|
|
46
|
+
hash = @map.serialize(@object)
|
|
47
|
+
assert_equal "kaka", hash["hash key"]
|
|
48
|
+
assert_equal 1, hash["other hash key"]
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: domain_mapper
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Jan Filipowski
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2013-05-03 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: bundler
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ~>
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '1.3'
|
|
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: '1.3'
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: rake
|
|
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
|
+
- !ruby/object:Gem::Dependency
|
|
47
|
+
name: minitest
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
none: false
|
|
50
|
+
requirements:
|
|
51
|
+
- - ! '>='
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
type: :development
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
none: false
|
|
58
|
+
requirements:
|
|
59
|
+
- - ! '>='
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
description: Map any hash to domain object using reflection.
|
|
63
|
+
email:
|
|
64
|
+
- jachuf@gmail.com
|
|
65
|
+
executables: []
|
|
66
|
+
extensions: []
|
|
67
|
+
extra_rdoc_files: []
|
|
68
|
+
files:
|
|
69
|
+
- .gitignore
|
|
70
|
+
- Gemfile
|
|
71
|
+
- LICENSE.txt
|
|
72
|
+
- README.md
|
|
73
|
+
- Rakefile
|
|
74
|
+
- domain_mapper.gemspec
|
|
75
|
+
- lib/domain_mapper.rb
|
|
76
|
+
- lib/domain_mapper/version.rb
|
|
77
|
+
- test/map_test.rb
|
|
78
|
+
- test/test_helper.rb
|
|
79
|
+
homepage: ''
|
|
80
|
+
licenses:
|
|
81
|
+
- MIT
|
|
82
|
+
post_install_message:
|
|
83
|
+
rdoc_options: []
|
|
84
|
+
require_paths:
|
|
85
|
+
- lib
|
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
|
+
none: false
|
|
88
|
+
requirements:
|
|
89
|
+
- - ! '>='
|
|
90
|
+
- !ruby/object:Gem::Version
|
|
91
|
+
version: '0'
|
|
92
|
+
segments:
|
|
93
|
+
- 0
|
|
94
|
+
hash: 1942390453890777948
|
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
|
+
none: false
|
|
97
|
+
requirements:
|
|
98
|
+
- - ! '>='
|
|
99
|
+
- !ruby/object:Gem::Version
|
|
100
|
+
version: '0'
|
|
101
|
+
segments:
|
|
102
|
+
- 0
|
|
103
|
+
hash: 1942390453890777948
|
|
104
|
+
requirements: []
|
|
105
|
+
rubyforge_project:
|
|
106
|
+
rubygems_version: 1.8.25
|
|
107
|
+
signing_key:
|
|
108
|
+
specification_version: 3
|
|
109
|
+
summary: Map any hash to domain object using reflection.
|
|
110
|
+
test_files:
|
|
111
|
+
- test/map_test.rb
|
|
112
|
+
- test/test_helper.rb
|