everyday-cli-utils 1.7.4 → 1.8.0

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: eaf90e157bfdb099b4a6192ff01710cbd09582fc
4
- data.tar.gz: 1355b0150b18848b5c004ad4692c1b21d8bbd9fb
3
+ metadata.gz: a9ee0af3e7bd0801b0f8d10fba5065ce4460c372
4
+ data.tar.gz: fd9cd12f7942c1cdca57a2b7f14e3ab3c742e415
5
5
  SHA512:
6
- metadata.gz: a0e325c6f9781a542271d0817e8a3df9087f11c091d5d855f3a6f55508f440bde7c09952b89efadda036bd176de6fef91c4f197f32394eac8683790bf63e708e
7
- data.tar.gz: eed8a8d71943c189f8656d41cee453bcf75b0c105e3ddaadbe21840e00bc4a10d50ad4b524605b435737f715cd1612ed0e1a4582d8a4d87a97dc25e315bb2c26
6
+ metadata.gz: f49ed1abee59229a42e4579398985a661c4010a0af531ccac75255bfdd9807f32104f9478823b696e4c87df4eaaa3575f40f07544b6e53b4535308c12612de8c
7
+ data.tar.gz: 2271b2e4218baef1c5c43e4aa1e2e40d212725c1c8acf7bd3d4fc944702432c246c8994bd1b26dc9ce3f53693f3f608d518214f8438ac2e52c2cce6d03100dd5
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.required_ruby_version = '>= 1.9.3'
22
22
 
23
- spec.add_development_dependency 'bundler', '~> 1.5'
23
+ spec.add_development_dependency 'bundler', '~> 1.6'
24
24
  spec.add_development_dependency 'rake', '~> 10.0'
25
25
  spec.add_development_dependency 'simplecov', '>= 0.8.2'
26
26
  spec.add_development_dependency 'coveralls', '>= 0.7.0'
@@ -0,0 +1,102 @@
1
+ class MethodOverrides
2
+ def initialize
3
+ @overrides = {}
4
+ end
5
+
6
+ def empty?(method_name)
7
+ @overrides[method_name].nil? || @overrides[method_name].empty?
8
+ end
9
+
10
+ def store_override(method_name, method)
11
+ @overrides[method_name] ||= []
12
+ @overrides[method_name] << method
13
+ end
14
+
15
+ def get(obj)
16
+ OverridesInstance.new(@overrides, obj)
17
+ end
18
+ end
19
+
20
+ class OverridesInstance
21
+ def initialize(overrides, obj, ind = 0)
22
+ @overrides = overrides
23
+ @obj = obj
24
+ @ind = ind
25
+ end
26
+
27
+ def [](ind)
28
+ OverridesInstance.new(@overrides, @obj, @ind - ind)
29
+ end
30
+
31
+ def call_override(method_symbol, *args, &block)
32
+ if @overrides.has_key?(method_symbol.to_sym)
33
+ overrides = @overrides[method_symbol.to_sym]
34
+ ind = overrides.count + (@ind - 1)
35
+ ovin = Thread.current["overrides_ind_#{@obj.__id__}"] || 0
36
+ ind += ovin
37
+ Thread.current["overrides_ind_#{@obj.__id__}"] = @ind + ovin - 1
38
+ rv = overrides[ind].bind(@obj).call(*args, &block)
39
+ Thread.current["overrides_ind_#{@obj.__id__}"] = ovin
40
+ rv
41
+ else
42
+ @obj.send(method_symbol.to_sym, *args, &block)
43
+ end
44
+ end
45
+
46
+ def method_missing(symbol, *args, &block)
47
+ call_override(symbol, *args, &block)
48
+ end
49
+ end
50
+
51
+ class Object
52
+ def create_method(name, &block)
53
+ self.send(:define_method, name, &block)
54
+ end
55
+
56
+ def overrides
57
+ (@overrides && @overrides.get(self)) || self.class.overrides(self)
58
+ end
59
+
60
+ def true_overrides
61
+ @overrides
62
+ end
63
+
64
+ def true_overrides=(overrides)
65
+ @overrides = overrides
66
+ end
67
+
68
+ def override(method_name, &block)
69
+ s2 = self
70
+ class << self
71
+ self
72
+ end.class_eval {
73
+ original_method = instance_method(method_name.to_sym)
74
+ s2.true_overrides ||= MethodOverrides.new
75
+ s2.true_overrides.store_override(method_name.to_sym, original_method)
76
+ self.create_method(method_name.to_sym, &block)
77
+ }
78
+ end
79
+
80
+ class << self
81
+ def overrides(s)
82
+ @overrides && @overrides.get(s)
83
+ end
84
+
85
+ def true_overrides
86
+ @overrides
87
+ end
88
+
89
+ def true_overrides=(overrides)
90
+ @overrides = overrides
91
+ end
92
+
93
+ def override(method_name, &block)
94
+ self.class_eval {
95
+ original_method = instance_method(method_name.to_sym)
96
+ self.true_overrides ||= MethodOverrides.new
97
+ self.true_overrides.store_override(method_name.to_sym, original_method)
98
+ self.create_method(method_name.to_sym, &block)
99
+ }
100
+ end
101
+ end
102
+ end
@@ -1,3 +1,3 @@
1
1
  module EverydayCliUtils
2
- VERSION = '1.7.4'
2
+ VERSION = '1.8.0'
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require_relative 'everyday-cli-utils/version'
2
2
 
3
3
  module EverydayCliUtils
4
- AVAILABLE_MODULES = [:ask, :format, :format_safe, :histogram, :histogram_safe, :kmeans, :kmeans_safe, :maputil, :maputil_safe, :option]
4
+ AVAILABLE_MODULES = [:ask, :format, :format_safe, :histogram, :histogram_safe, :kmeans, :kmeans_safe, :maputil, :maputil_safe, :option, :override]
5
5
  MODULE_TO_RELATIVE = {
6
6
  ask: 'everyday-cli-utils/ask',
7
7
  format: 'everyday-cli-utils/format',
@@ -12,7 +12,8 @@ module EverydayCliUtils
12
12
  kmeans_safe: 'everyday-cli-utils/safe/kmeans',
13
13
  maputil: 'everyday-cli-utils/maputil',
14
14
  maputil_safe: 'everyday-cli-utils/safe/maputil',
15
- option: 'everyday-cli-utils/option'
15
+ option: 'everyday-cli-utils/option',
16
+ override: 'everyday-cli-utils/override',
16
17
  }
17
18
 
18
19
  def import(*names)
@@ -77,7 +77,7 @@ describe 'maputil' do
77
77
 
78
78
  it 'provides a means of using map with a hash' do
79
79
  hash = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }
80
- mapped = hash.map { |v| v[1] + ((v[0] == :a || v[0] == :c || v[0] == :e) ? 1 : -1) }
80
+ mapped = hash.hashmap { |v| v[1] + ((v[0] == :a || v[0] == :c || v[0] == :e) ? 1 : -1) }
81
81
  expected = { a: 2, b: 1, c: 4, d: 3, e: 6, f: 5}
82
82
  mapped.should eq expected
83
83
  end
@@ -0,0 +1,48 @@
1
+ require_relative '../../lib/everyday-cli-utils'
2
+ include EverydayCliUtils
3
+ import :override
4
+
5
+ class TestClass
6
+ def test(t)
7
+ "Hi #{t}!"
8
+ end
9
+ end
10
+
11
+ describe 'override' do
12
+ it 'supports overriding methods' do
13
+ arr = [1, 2, 3]
14
+ arr.override(:first) { "Boo! #{self.overrides.first}"}
15
+ arr.first.should eq 'Boo! 1'
16
+ end
17
+
18
+ it 'supports overriding methods multiple times' do
19
+ arr = [1, 2, 3]
20
+ arr.override(:first) { "Boo! #{self.overrides.first}" }
21
+ arr.override(:first) { "Boo Again! #{self.overrides[1].first}" }
22
+ arr.override(:first) { "Boo Again Again! #{self.overrides[1].first} | #{self.overrides.first}" }
23
+ arr.first.should eq 'Boo Again Again! Boo! 1 | Boo Again! 1'
24
+ end
25
+
26
+ it 'supports overriding methods multiple times on a class' do
27
+ TestClass.override(:test) { |t| "Boo! #{self.overrides.test("-#{t}-")}" }
28
+ TestClass.override(:test) { |t| "Boo Again! #{self.overrides[1].test("+#{t}+")}" }
29
+ TestClass.override(:test) { |t| "Boo Again Again! #{self.overrides[1].test("!#{t}!")} | #{self.overrides.test("~#{t}~")}" }
30
+ TestClass.new.test('Eric').should eq 'Boo Again Again! Boo! Hi -!Eric!-! | Boo Again! Hi +~Eric~+!'
31
+ end
32
+
33
+ it 'supports calling methods defined by Object that do not trigger method_missing' do
34
+ arr = [1, 2, 3]
35
+ arr.override(:inspect) { "Boo! #{self.overrides.call_override :inspect}" }
36
+ arr.override(:inspect) { "Boo Again! #{self.overrides[1].call_override :inspect}" }
37
+ arr.override(:inspect) { "Boo Again Again! #{self.overrides[1].call_override :inspect} | #{self.overrides.call_override :inspect}" }
38
+ arr.inspect.should eq 'Boo Again Again! Boo! [1, 2, 3] | Boo Again! [1, 2, 3]'
39
+ end
40
+
41
+ it 'supports calling methods on the overrides object that were not overridden' do
42
+ arr = [1, 2, 3]
43
+ arr.override(:first) { "Boo! #{self.overrides.first}" }
44
+ arr.override(:first) { "Boo Again! #{self.overrides[1].first}" }
45
+ arr.override(:first) { "Boo Again Again! #{self.overrides[1].first} | #{self.overrides.first} || #{self.overrides.last}" }
46
+ arr.first.should eq 'Boo Again Again! Boo! 1 | Boo Again! 1 || 3'
47
+ end
48
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: everyday-cli-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.4
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Henderson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-30 00:00:00.000000000 Z
11
+ date: 2014-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
19
+ version: '1.6'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.5'
26
+ version: '1.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -107,6 +107,7 @@ files:
107
107
  - lib/everyday-cli-utils/kmeans.rb
108
108
  - lib/everyday-cli-utils/maputil.rb
109
109
  - lib/everyday-cli-utils/option.rb
110
+ - lib/everyday-cli-utils/override.rb
110
111
  - lib/everyday-cli-utils/safe/format.rb
111
112
  - lib/everyday-cli-utils/safe/histogram.rb
112
113
  - lib/everyday-cli-utils/safe/kmeans.rb
@@ -116,6 +117,7 @@ files:
116
117
  - spec/everyday-cli-utils/kmeans_spec.rb
117
118
  - spec/everyday-cli-utils/maputil_spec.rb
118
119
  - spec/everyday-cli-utils/option_spec.rb
120
+ - spec/everyday-cli-utils/override_spec.rb
119
121
  - spec/spec_helper.rb
120
122
  homepage: https://github.com/henderea/everyday-cli-utils
121
123
  licenses:
@@ -146,5 +148,6 @@ test_files:
146
148
  - spec/everyday-cli-utils/kmeans_spec.rb
147
149
  - spec/everyday-cli-utils/maputil_spec.rb
148
150
  - spec/everyday-cli-utils/option_spec.rb
151
+ - spec/everyday-cli-utils/override_spec.rb
149
152
  - spec/spec_helper.rb
150
153
  has_rdoc: