mapping 1.0.1 → 1.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 518729390fbe28863bc436cefe3393ec40d10a87
4
- data.tar.gz: e4f1954ce1c52b3081b6e660566cf92ae2116380
3
+ metadata.gz: 38e9698d7009c8d838738039e9ba4c29f44d5790
4
+ data.tar.gz: db7a33ede063bb85ac88333b6ffb5995b69e37bb
5
5
  SHA512:
6
- metadata.gz: 5fbd2d73f546d8ebde886914c10d2ec4e7b6828553d3411b014833864b3a398bc3f1ad5e9bb037c15ec283e2c1d33adffe7139e62c66d6ba915217f092ff414e
7
- data.tar.gz: e89ea99b339695029eebb3ae0daa4008579dd287ff55ef98a5a00eb7f58aff566c9bc4c808d133cd613b50b64499440a1b0d6482a8a66be821108eed9e50b191
6
+ metadata.gz: a08b617fa48172c3dcea82eea31724964d2df9e3a9010ad281e80b58d0bd9b56a22575960d743ca30c15d678475ae9a1901a5c89aa444aacbb4db469c7ce078e
7
+ data.tar.gz: 0714653af91b12b198574f05b17f68e0440931f438d59418339109f816a8a128e510a739b8026eb1c061930f84ed86e393de14f12bbe052375f6c6814bb321a9
data/.travis.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.3.0
4
+ - 2.4.0
4
5
  env: COVERAGE=true
@@ -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(klass, &block)
37
- method_name = self.method_for_mapping(klass)
38
- define_method(method_name, &block)
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.each do |klass|
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(klass)
50
- method_name = self.method_for_mapping(klass)
51
- undef_method(method_name)
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.
@@ -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, Float, Rational, String)
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)}
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Mapping
22
- VERSION = "1.0.1"
22
+ VERSION = "1.1.0"
23
23
  end
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.1
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-02-22 00:00:00.000000000 Z
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