mapping 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/lib/mapping/descendants.rb +25 -0
- data/lib/mapping/model.rb +11 -9
- data/lib/mapping/object_model.rb +1 -9
- data/lib/mapping/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38e9698d7009c8d838738039e9ba4c29f44d5790
|
4
|
+
data.tar.gz: db7a33ede063bb85ac88333b6ffb5995b69e37bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a08b617fa48172c3dcea82eea31724964d2df9e3a9010ad281e80b58d0bd9b56a22575960d743ca30c15d678475ae9a1901a5c89aa444aacbb4db469c7ce078e
|
7
|
+
data.tar.gz: 0714653af91b12b198574f05b17f68e0440931f438d59418339109f816a8a128e510a739b8026eb1c061930f84ed86e393de14f12bbe052375f6c6814bb321a9
|
data/.travis.yml
CHANGED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
module Mapping
|
22
|
+
def self.lookup_descendants(klass)
|
23
|
+
ObjectSpace.each_object(Class).select{|other| other < klass}
|
24
|
+
end
|
25
|
+
end
|
data/lib/mapping/model.rb
CHANGED
@@ -33,22 +33,24 @@ module Mapping
|
|
33
33
|
end
|
34
34
|
|
35
35
|
# Add a mapping from a given input class to a specific block.
|
36
|
-
def self.map(
|
37
|
-
|
38
|
-
|
36
|
+
def self.map(*klasses, &block)
|
37
|
+
klasses.each do |klass|
|
38
|
+
method_name = self.method_for_mapping(klass)
|
39
|
+
define_method(method_name, &block)
|
40
|
+
end
|
39
41
|
end
|
40
42
|
|
41
43
|
# Sometimes you just want to map things to themselves (the identity function). This makes it convenient to specify a lot of identity mappings.
|
42
44
|
def self.map_identity(*klasses)
|
43
|
-
klasses
|
44
|
-
self.map(klass) {|value| value}
|
45
|
-
end
|
45
|
+
self.map(*klasses) {|value| value}
|
46
46
|
end
|
47
47
|
|
48
48
|
# Remove a mapping, usually an inherited one, which you don't want.
|
49
|
-
def self.unmap(
|
50
|
-
|
51
|
-
|
49
|
+
def self.unmap(*klasses)
|
50
|
+
klasses.each do |klass|
|
51
|
+
method_name = self.method_for_mapping(klass)
|
52
|
+
undef_method(method_name)
|
53
|
+
end
|
52
54
|
end
|
53
55
|
|
54
56
|
# The primary function, which maps an input object to an output object.
|
data/lib/mapping/object_model.rb
CHANGED
@@ -23,15 +23,7 @@ require_relative 'model'
|
|
23
23
|
module Mapping
|
24
24
|
# Provides a useful starting point for object based mappings. Handles, true, false, nil, Array and Hash by default, simply by passing through.
|
25
25
|
class ObjectModel < Model
|
26
|
-
map_identity(NilClass, TrueClass, FalseClass,
|
27
|
-
|
28
|
-
if 0.class == Integer
|
29
|
-
# Ruby 2.4+ Integer unification
|
30
|
-
map_identity(Integer)
|
31
|
-
else
|
32
|
-
# Ruby < 2.4
|
33
|
-
map_identity(Fixnum, Bignum)
|
34
|
-
end
|
26
|
+
map_identity(NilClass, TrueClass, FalseClass, String, *Mapping.lookup_descendants(Numeric))
|
35
27
|
|
36
28
|
map(Array) do |items|
|
37
29
|
items.collect{|object| map(object)}
|
data/lib/mapping/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mapping
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- README.md
|
70
70
|
- Rakefile
|
71
71
|
- lib/mapping.rb
|
72
|
+
- lib/mapping/descendants.rb
|
72
73
|
- lib/mapping/model.rb
|
73
74
|
- lib/mapping/object_model.rb
|
74
75
|
- lib/mapping/version.rb
|