mapping 0.3.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mapping/model.rb +17 -1
- data/lib/mapping/object_model.rb +3 -12
- data/lib/mapping/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ec2c35a62c7b930aa17c5947cbe9c9801f3e789
|
4
|
+
data.tar.gz: 3bcea9b342572056d969e9f5d902c77e0eadca6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 860b19eb22e25a0a8ed437fa7b73623f7964da1120c63459a8d4a09aa5d0a467ef812eade6b665c87a9b977036a34a3e6d32640b07bf09e13c61821d8d2133f5
|
7
|
+
data.tar.gz: 3cc993ec244884087615ad250d7299f9f84a9b0f6ed7c73123d3e2d117eac2cca897e3b89da0a7859e4565c9815dcf81c7dd507d17b1d68e032246f126638868
|
data/lib/mapping/model.rb
CHANGED
@@ -27,18 +27,34 @@ module Mapping
|
|
27
27
|
PREFIX + klass.name.gsub(/::/, '_')
|
28
28
|
end
|
29
29
|
|
30
|
+
# Get the name of the method for mapping the given object.
|
30
31
|
def method_for_mapping(object)
|
31
32
|
self.class.method_for_mapping(object.class)
|
32
33
|
end
|
33
34
|
|
35
|
+
# Add a mapping from a given input class to a specific block.
|
34
36
|
def self.map(klass, &block)
|
35
37
|
method_name = self.method_for_mapping(klass)
|
36
38
|
define_method(method_name, &block)
|
37
39
|
end
|
38
40
|
|
41
|
+
# Sometimes you just want to map things to themselves (the identity function). This makes it convenient to specify a lot of identity mappings.
|
42
|
+
def self.map_identity(*klasses)
|
43
|
+
klasses.each do |klass|
|
44
|
+
self.map(klass) {|value| value}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Remove a mapping, usually an inherited one, which you don't want.
|
49
|
+
def self.unmap(klass)
|
50
|
+
method_name = self.method_for_mapping(klass)
|
51
|
+
undef_method(method_name)
|
52
|
+
end
|
53
|
+
|
54
|
+
# The primary function, which maps an input object to an output object.
|
39
55
|
def map(root, *args)
|
40
56
|
method_name = self.method_for_mapping(root)
|
41
|
-
|
57
|
+
|
42
58
|
self.send(method_name, root, *args)
|
43
59
|
end
|
44
60
|
end
|
data/lib/mapping/object_model.rb
CHANGED
@@ -21,25 +21,16 @@
|
|
21
21
|
require_relative 'model'
|
22
22
|
|
23
23
|
module Mapping
|
24
|
+
# Provides a useful starting point for object based mappings. Handles, true, false, nil, Array and Hash by default, simply by passing through.
|
24
25
|
class ObjectModel < Model
|
25
|
-
|
26
|
-
nil
|
27
|
-
end
|
28
|
-
|
29
|
-
map(TrueClass) do |object|
|
30
|
-
true
|
31
|
-
end
|
32
|
-
|
33
|
-
map(FalseClass) do |object|
|
34
|
-
false
|
35
|
-
end
|
26
|
+
map_identity(NilClass, TrueClass, FalseClass, Fixnum, Bignum, Float, Rational, String)
|
36
27
|
|
37
28
|
map(Array) do |items|
|
38
29
|
items.collect{|object| map(object)}
|
39
30
|
end
|
40
31
|
|
41
32
|
map(Hash) do |hash|
|
42
|
-
hash
|
33
|
+
hash.inject(Hash.new) { |output, (key, value)| output[key] = map(value); output }
|
43
34
|
end
|
44
35
|
end
|
45
36
|
end
|
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: 0.
|
4
|
+
version: 1.0.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: 2016-03-
|
11
|
+
date: 2016-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|