HornsAndHooves-flat_map 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e476b0c2831807b9734932bcec75f3d798a09d08
4
- data.tar.gz: 88d957bf1d9467c8abcf2f1e75e655c28630a51c
3
+ metadata.gz: 1f3b82b7ae0f88b1c76017b5ac2410dee62efeed
4
+ data.tar.gz: 5f72a1ca80df379ddfd7dfeb22b8b8d1530c8b0f
5
5
  SHA512:
6
- metadata.gz: 6d721973e3a24a1f9e42599f7daaa3ce4c8ca34b7de8cbdcd8cec93249a72ef4adbe29025ddf481a52de4622309dd04aa2d1cd8f89028cb426001cfa5f449b9f
7
- data.tar.gz: f8b5c8efcdff94145f7a329abc3f124797754bcc50baf3ecd828d70cd9bb37973f8a247e4fb4d99b7fcc1b4ea14a61da4d808b30c923d40b032ad5641bee688a
6
+ metadata.gz: 62115ee9279c68f7ed109ed43b769fb28669d9f6a3c884d69fb8680e41cc20b2139f2993578b73a9b79a924de5d790bde331755190d6f6fbd77467c3ad40f511
7
+ data.tar.gz: 50f70f29acdc63ba0de90725f79694e877ca983dcd84bea897e9defa9c7463fcff6dd62ee48233e7981d55eae9e50735ec4ad23905c071f7d2a5f69ec29e558c
@@ -0,0 +1,42 @@
1
+ require "simplecov-rcov-text"
2
+ require "colorize"
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::RcovTextFormatter,
6
+ SimpleCov::Formatter::HTMLFormatter
7
+ ]
8
+ SimpleCov.start do
9
+ add_filter "/spec/"
10
+
11
+ # Fail the build when coverage is weak:
12
+ at_exit do
13
+ SimpleCov.result.format!
14
+ threshold, actual = 100.0, SimpleCov.result.covered_percent
15
+ if actual < threshold then # FAIL
16
+ msg = "\nLow coverage: "
17
+ msg << "#{actual}%".colorize(:red)
18
+ msg << ' is ' << 'under'.colorize(:red) << ' the threshold: '
19
+ msg << "#{threshold}%.".colorize(:green)
20
+ msg << "\n"
21
+ $stderr.puts msg
22
+ exit 1
23
+ else # PASS
24
+ # Precision: three decimal places:
25
+ actual_trunc = (actual * 1000).floor / 1000.0
26
+ msg = "\nCoverage: "
27
+ msg << "#{actual}%".colorize(:green)
28
+ if actual_trunc > threshold
29
+ msg << ' is ' << 'over'.colorize(:green) << ' the threshold: '
30
+ msg << "#{threshold}%. ".colorize(color: :yellow, mode: :bold)
31
+ msg << 'Please update the threshold to: '
32
+ msg << "#{actual_trunc}% ".colorize(color: :green, mode: :bold)
33
+ msg << 'in ./.simplecov.'
34
+ else
35
+ msg << ' is ' << 'at'.colorize(:green) << ' the threshold: '
36
+ msg << "#{threshold}%.".colorize(:green)
37
+ end
38
+ msg << "\n"
39
+ $stdout.puts msg
40
+ end
41
+ end
42
+ end
data/Gemfile CHANGED
@@ -6,15 +6,16 @@ gemspec
6
6
  group :development do
7
7
  gem 'redcarpet'
8
8
  gem 'yard'
9
- gem 'pry'
10
9
  end
11
10
 
12
11
  group :development, :test do
13
12
  # code metrics:
14
13
  gem "metric_fu"
14
+ gem "pry"
15
15
  end
16
16
 
17
17
  group :test do
18
+ gem 'colorize' , :require => false
18
19
  gem 'simplecov' , :require => false
19
20
  gem 'simplecov-rcov-text', :require => false
20
21
  end
@@ -5,7 +5,7 @@ require "flat_map/version"
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "HornsAndHooves-flat_map"
7
7
  s.version = FlatMap::VERSION
8
- s.authors = ["Artem Kuzko", "Zachary Belzer", "Sergey Potapov"]
8
+ s.authors = ["HornsAndHooves", "Artem Kuzko", "Zachary Belzer", "Sergey Potapov"]
9
9
  s.email = ["a.kuzko@gmail.com", "zbelzer@gmail.com", "blake131313@gmail.com"]
10
10
  s.homepage = "https://github.com/HornsAndHooves/flat_map"
11
11
  s.licenses = ["LICENSE"]
@@ -8,6 +8,8 @@ into a mapper object with all mapped attributes accessible in a plain way.
8
8
 
9
9
  ## Usage
10
10
 
11
+ gem "HornsAndHooves-flat_map", require: "flat_map"
12
+
11
13
  ### Mapper
12
14
 
13
15
  FlatMap mappers are designed to provide complex set of data, distributed over
@@ -15,22 +15,64 @@ module FlatMap
15
15
  if name == :to_ary ||
16
16
  @attribute_methods_defined ||
17
17
  self.class.protected_instance_methods.include?(name)
18
- return super
18
+ super
19
+ else
20
+ mappings = all_mappings
21
+
22
+ if mapped_name?(mappings, name)
23
+ define_attribute_methods(mappings)
24
+
25
+ send(name, *args, &block)
26
+ else
27
+ super
28
+ end
29
+ end
30
+ end
31
+
32
+ # Look for methods that might be dynamically defined and define them for lookup.
33
+ def respond_to_missing?(name, include_private = false)
34
+ # Added magically by Ruby 1.9.3
35
+ if name == :to_ary || name == :empty?
36
+ false
37
+ else
38
+ unless @attribute_methods_defined
39
+ define_attribute_methods(all_mappings)
40
+ end
41
+
42
+ mapped_name?(all_mappings, name)
19
43
  end
44
+ end
20
45
 
21
- mappings = all_mappings
46
+ # Is the name given part of the attribute mappings?
47
+ #
48
+ # @param [Array<FlatMap::Mapping>] mappings
49
+ # @param [String] name
50
+ # @return Boolean
51
+ def mapped_name?(mappings, name)
22
52
  valid_names = mappings.map do |mapping|
23
53
  full_name = mapping.full_name
24
54
  [full_name, "#{full_name}=".to_sym]
25
55
  end
26
56
  valid_names.flatten!
27
57
 
28
- return super unless valid_names.include?(name)
58
+ valid_names.include?(name)
59
+ end
60
+
61
+ # Return the list of all mapped attributes
62
+ #
63
+ # @return [Array<String>]
64
+ def attribute_names
65
+ all_mappings.map { |mapping| mapping.full_name }
66
+ end
29
67
 
68
+ # Actually define the attribute methods on this object.
69
+ #
70
+ # @param [Array<FlatMap::Mapping>] mappings list of mappings
71
+ def define_attribute_methods(mappings)
30
72
  extend attribute_methods(mappings)
31
73
  @attribute_methods_defined = true
32
- send(name, *args, &block)
33
74
  end
75
+ private :define_attribute_methods
34
76
 
35
77
  # Define anonymous module with reader and writer methods for
36
78
  # all the +mappings+ being passed.
@@ -164,5 +164,18 @@ module FlatMap
164
164
  return super if mounting.nil?
165
165
  mounting.send(name, *args, &block)
166
166
  end
167
+
168
+ # Look for methods that might be dynamically defined and define them for lookup.
169
+ def respond_to_missing?(name, include_private = false)
170
+ # Added magically by Ruby 1.9.3
171
+ if name == :to_ary || name == :empty?
172
+ false
173
+ else
174
+ return true if mapping(name).present?
175
+
176
+ mounting = all_mountings.find{ |mount| mount.respond_to?(name) }
177
+ return false if mounting.nil?
178
+ end
179
+ end
167
180
  end
168
181
  end
@@ -1,3 +1,3 @@
1
1
  module FlatMap # :nodoc:
2
- VERSION = "0.2.0" # :nodoc:
2
+ VERSION = "0.2.1" # :nodoc:
3
3
  end
@@ -16,6 +16,14 @@ module FlatMap
16
16
  target.attr_b = 'b'
17
17
  end
18
18
 
19
+ it 'correctly responds to dynamic methods' do
20
+ mapper.respond_to?(:attr_a=).should be true
21
+ mapper.method(:attr_a=).should_not be nil
22
+
23
+ mapper.respond_to?(:attr_b=).should be true
24
+ mapper.method(:attr_b=).should_not be nil
25
+ end
26
+
19
27
  it 'should be able to read values via method calls' do
20
28
  mapper.attr_a.should == 'a'
21
29
  mapper.attr_b.should == 'b'
@@ -95,19 +95,39 @@ module FlatMap
95
95
  mapper.mounting(:undefined_mount).should be_nil
96
96
  end
97
97
 
98
- it '#read should add mounted mappers to a result' do
99
- mapper.read.should == {
100
- :host_attr => 'attr',
101
- :attr_a => 'a',
102
- :mapped_attr_b => 'b'
103
- }
104
- end
105
-
106
- it '#write should pass values to mounted mappers' do
107
- target = MountingSpec.mount_target
108
- mapper.write :attr_a => 'A', :mapped_attr_b => 'B'
109
- target.attr_a.should == 'A'
110
- target.attr_b.should == 'B'
98
+ describe "#read" do
99
+ it 'should add mounted mappers to a result' do
100
+ mapper.read.should == {
101
+ :host_attr => 'attr',
102
+ :attr_a => 'a',
103
+ :mapped_attr_b => 'b'
104
+ }
105
+ end
106
+
107
+ it 'should define dynamic writer methods' do
108
+ mapper.respond_to?(:attr_a).should be true
109
+ mapper.method(:attr_a).should_not be nil
110
+
111
+ mapper.respond_to?(:mapped_attr_b).should be true
112
+ mapper.method(:mapped_attr_b).should_not be nil
113
+ end
114
+ end
115
+
116
+ describe "#write" do
117
+ it 'should define dynamic writer methods' do
118
+ mapper.respond_to?(:attr_a=).should be true
119
+ mapper.method(:attr_a=).should_not be nil
120
+
121
+ mapper.respond_to?(:mapped_attr_b=).should be true
122
+ mapper.method(:mapped_attr_b=).should_not be nil
123
+ end
124
+
125
+ it 'should pass values to mounted mappers' do
126
+ target = MountingSpec.mount_target
127
+ mapper.write :attr_a => 'A', :mapped_attr_b => 'B'
128
+ target.attr_a.should == 'A'
129
+ target.attr_b.should == 'B'
130
+ end
111
131
  end
112
132
 
113
133
  it 'should delegate missing methods to mounted mappers' do
@@ -1,4 +1,6 @@
1
1
  require 'flat_map'
2
+ require 'ostruct'
3
+ require 'pry'
2
4
  require 'rspec/its'
3
5
 
4
6
  require 'simplecov'
metadata CHANGED
@@ -1,9 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: HornsAndHooves-flat_map
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
+ - HornsAndHooves
7
8
  - Artem Kuzko
8
9
  - Zachary Belzer
9
10
  - Sergey Potapov
@@ -112,6 +113,7 @@ files:
112
113
  - ".rspec"
113
114
  - ".ruby-gemset"
114
115
  - ".ruby-version"
116
+ - ".simplecov"
115
117
  - ".travis.yml"
116
118
  - Gemfile
117
119
  - HornsAndHooves-flat_map.gemspec