hook_lying_syncer 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 50543d3682fdd1714cba83c22081c01cbe134447
4
+ data.tar.gz: 8dd510ad63d6e6dc0440356ea1aa0089d87685f1
5
+ SHA512:
6
+ metadata.gz: b0d3dc628398cb5816bf5bd6f643a7becbff5c90ab5a93df13176c20e2782a0fafb8074a92c840c3d4537f0ff1b99855ddf53ccd872922b7f4c72490994fb1bb
7
+ data.tar.gz: 58ad28b4921f4d9340aebd3991591a1e6031d16294b340996d3b263e9f0871b5731f78cac3acce59f905581fc6ba9abb50abe75c1d99fdf29285b313dd0d39e2
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hook_lying_syncer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dave Aronson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ hook_lying_syncer
2
+ =================
3
+
4
+ This project presents a way for Ruby coders to keep method_missing and
5
+ respond_to_missing? in sync.
6
+
7
+ ## Background
8
+
9
+ The whole idea of finding a way to automagically keep method_missing and
10
+ respond_to_missing? in sync, was originally inspired by [Avdi
11
+ Grimm](http://about.avdi.org/)'s [blog
12
+ post](http://devblog.avdi.org/2011/12/07/defining-method_missing-and-respond_to-at-the-same-time/)
13
+ about the need to sync them. I came up with a quick and dirty hack, and a
14
+ still-hacky improvement that seems to have been mangled by a blog platform
15
+ change or some such.
16
+
17
+ Then at RubyConf 2014, [Betsy Haibel](http://betsyhaibel.com/) gave a talk on
18
+ metaprogramming, including the need. That inspired me to take another whack at
19
+ it, this time using the different approach shown in this repo (essentially, a
20
+ decorator class).
21
+
22
+ I got some suggestions and other help from [Chris
23
+ Hoffman](https://github.com/yarmiganosca), mainly in figuring out that I
24
+ shouldn't do the in-block object access the way I was trying to! :-)
25
+
26
+ ## Installation
27
+
28
+ Add this line to your application's Gemfile:
29
+
30
+ ```ruby
31
+ gem 'hook_lying_syncer'
32
+ ```
33
+
34
+ And then execute:
35
+
36
+ $ bundle
37
+
38
+ Or install it yourself as:
39
+
40
+ $ gem install hook_lying_syncer
41
+
42
+
43
+
44
+ ## Usage
45
+
46
+ Create a HookLyingSyncer by passing it something to wrap, a lambda to turn the
47
+ method name into the subparts of interest, and a block to execute when there's
48
+ a match.
49
+
50
+ * The "something to wrap" can be any object, even a class. Note however that
51
+ if you wrap a class, that will not affect its instances! You can affect
52
+ future instances by using a wrapper to override .new, but if you need to
53
+ affect _extant_ instances, you have to wrap them yourself.
54
+
55
+ * The lambda must return an Array with some truthy content (or at least
56
+ _something_ that responds positively to #any?) if the method name is one
57
+ you're interested in, and either an empty Array (or at least _something_ that
58
+ responds negatively to #any?) or something falsey (i.e., false or nil)
59
+ otherwise. If you are not comfortable making lambdas, feel free to copy the
60
+ lambda_maker method in the tests.
61
+
62
+ * The block will be called when the lambda indicates that a method of interest
63
+ has been called. The block will receive three arguments: the original object
64
+ the HookLyingSyncer wrapped, the matches returned by the lambda, and the list
65
+ of arguments (if any) passed in the method call.
66
+
67
+ See the tests for examples.
68
+
69
+ ## Status
70
+
71
+ I have barely begun to work on this repo, so it's still a bit rough, as a
72
+ project per se. My plan is to turn it into a gem.
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it ( https://github.com/davearonson/hook_lying_syncer/fork )
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hook_lying_syncer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hook_lying_syncer"
8
+ spec.version = HookLyingSyncer::VERSION
9
+ spec.authors = ["Dave Aronson"]
10
+ spec.email = ["hook_lying_syncer_gemspec.2.TRex@Codosaur.us"]
11
+ spec.summary = %q{Keeps method_missing and respond_to_missing? in sync.}
12
+ spec.description = %q{Provides a decorator class you can wrap objects in (even classes!) to keep method_missing and respond_to_missing? in sync.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,44 @@
1
+ require "hook_lying_syncer/version"
2
+
3
+ class HookLyingSyncer
4
+
5
+ # object is the object being wrapped, for purposes of endowing it with some
6
+ # pattern of methods it should respond to.
7
+ #
8
+ # matcher is a lambda that returns any words of interest to the block, given
9
+ # the called method name. if there are any, it should return an array. if
10
+ # there are none, it may return an empty array, nil, or false, and the method
11
+ # name will be ass-u-me'd to be "not of interest".
12
+ #
13
+ # block is what you want to do with the object, the matches, and any
14
+ # additional args given to the dynamic method. ideally this should include
15
+ # actually declaring the method, so further uses won't be inefficiently done
16
+ # via method_missing. maybe a later version of hls will do that for you.
17
+ def initialize(object, matcher, &block)
18
+ @object = object
19
+ @matcher = matcher
20
+ @block = block
21
+ end
22
+
23
+ private
24
+
25
+ def respond_to_missing?(sym, include_all=false)
26
+ matches = find_matches(sym)
27
+ matches.any? ? true : @object.send(:respond_to?, sym, include_all)
28
+ end
29
+
30
+ def method_missing(sym, *args, &blk)
31
+ matches = find_matches(sym)
32
+ if matches.any?
33
+ @block.call(@object, matches, *args)
34
+ else
35
+ @object.send(sym, *args, &blk)
36
+ end
37
+ end
38
+
39
+ def find_matches(sym)
40
+ result = @matcher.call(sym)
41
+ result ? result : []
42
+ end
43
+
44
+ end
@@ -0,0 +1,3 @@
1
+ class HookLyingSyncer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,271 @@
1
+ require 'hook_lying_syncer'
2
+
3
+ def lambda_maker(prefix, separator, suffix=nil)
4
+ lambda { |method_name|
5
+ matches = /\A#{prefix}(\w+)#{suffix}\Z/.match(method_name)
6
+ matches[1].split(separator) if matches
7
+ }
8
+ end
9
+
10
+ class Person
11
+ attr_reader :name
12
+ def initialize(name)
13
+ @name = name
14
+ end
15
+ def self.find(params)
16
+ self.seek("Looking for", params)
17
+ end
18
+ def self.need(params)
19
+ self.seek("I need", params)
20
+ end
21
+ def self.what_are_they
22
+ "Are we not men? We are Devo! D-E-V-O!"
23
+ end
24
+ private
25
+ def self.seek(look_how, params)
26
+ wants = [].tap { |list| params.each { |key, val| list << "#{val} #{key}" } }
27
+
28
+ # there's got to be some way to do this more cleanly...
29
+ # wouldn't be surprised if Rails has something like Array#sentencize....
30
+ wants[-1] = "and #{wants[-1]}" if wants.length > 1
31
+ separator = wants.length > 2 ? ", " : " "
32
+ description = wants.join(separator)
33
+
34
+ "#{look_how} a person with #{ description }"
35
+ end
36
+ end
37
+
38
+ describe HookLyingSyncer do
39
+
40
+ describe "on instances" do
41
+
42
+ before do
43
+ @person = Person.new("Dave")
44
+ kinds_getter = lambda_maker("find_", "_", "_widgets")
45
+ @syncer = HookLyingSyncer.new(@person, kinds_getter) do |p, kinds, *args|
46
+ addons = args.any? ? ", with #{args.join(" and ")}" : nil
47
+ "#{p.name} wants #{kinds.join(" ")} widgets#{addons}"
48
+ end
49
+ end
50
+
51
+ describe "respond_to_missing?" do
52
+
53
+ it "can handle dynamically defined methods" do
54
+ expect(@syncer.respond_to? :find_green_widgets).to equal true
55
+ end
56
+
57
+ it "can handle the original object's methods" do
58
+ expect(@syncer.respond_to? :name).to equal true
59
+ end
60
+
61
+ it "still rejects unknown methods" do
62
+ expect(@syncer.respond_to? :blargh).to equal false
63
+ end
64
+
65
+ end
66
+
67
+ describe "method_missing" do
68
+
69
+ describe "can handle dynamically defined methods" do
70
+
71
+ it "with no args" do
72
+ expect(@syncer.find_green_widgets).to eql "Dave wants green widgets"
73
+ end
74
+
75
+ it "with an arg" do
76
+ expect(@syncer.find_green_widgets(:stripes)).to eql(
77
+ "Dave wants green widgets, with stripes")
78
+ end
79
+
80
+ it "with multiple args" do
81
+ expect(@syncer.find_green_widgets(:stripes, :spots)).to eql(
82
+ "Dave wants green widgets, with stripes and spots")
83
+ end
84
+
85
+ it "with multiple subparts" do
86
+ expect(@syncer.find_big_green_widgets).to eql(
87
+ "Dave wants big green widgets")
88
+ end
89
+
90
+ end
91
+
92
+ describe "can handle the object's original methods" do
93
+
94
+ it "using the original object" do
95
+ expect(@syncer.name).to eql "Dave"
96
+ end
97
+
98
+ it "even if the object-pointing var is changed" do
99
+ @person = Person.new("Chris")
100
+ expect(@person.name).to eql "Chris" # just a sanity check
101
+ expect(@syncer.name).to eql "Dave"
102
+ end
103
+
104
+ end
105
+
106
+ it "doesn't prevent blowup on totally unknown methods" do
107
+ expect { @syncer.blarg }.to raise_error NoMethodError
108
+ end
109
+
110
+ it "can add methods" do
111
+ method_matcher = lambda { |name| name == :foo ? [name] : nil }
112
+ syncer = HookLyingSyncer.new(@person, method_matcher) do |p, wants, *args|
113
+ def foo
114
+ :foo
115
+ end
116
+ end
117
+ expect(syncer.foo).to equal :foo
118
+ end
119
+
120
+ it "can override methods" do
121
+ method_matcher = lambda { |name| name == :name ? [name] : nil }
122
+ syncer = HookLyingSyncer.new(@person, method_matcher) do |p, wants, *args|
123
+ p.name.reverse.capitalize
124
+ end
125
+ expect(syncer.name).to eql "Evad"
126
+ end
127
+
128
+ end
129
+
130
+ describe "with multiple levels" do
131
+
132
+ before do
133
+ name_getter = lambda_maker("say_to_", "_and_")
134
+ @inner = @syncer
135
+ @outer = HookLyingSyncer.new(@inner, name_getter) do |inner, names, *args|
136
+ "#{inner.name} says \"#{args.join("\" and \"")}\" to #{names.map(&:capitalize).join(" and ")}"
137
+ end
138
+ end
139
+
140
+ describe "respond_to_missing?" do
141
+
142
+ it "can handle dynamically defined methods" do
143
+ expect(@person.respond_to? :say_to_fred).to equal false
144
+ expect(@inner.respond_to? :say_to_fred).to equal false
145
+ expect(@outer.respond_to? :say_to_fred).to equal true
146
+ end
147
+
148
+ it "can handle the original object's methods" do
149
+ expect(@outer.respond_to? :name).to equal true
150
+ end
151
+
152
+ it "still rejects unknown methods" do
153
+ expect(@outer.respond_to? :blargh).to equal false
154
+ end
155
+
156
+ end
157
+
158
+ describe "method_missing" do
159
+
160
+ it "can handle dynamically defined methods" do
161
+ expect(@outer.say_to_fred_and_ethel("hail", "well met")).to eql(
162
+ "Dave says \"hail\" and \"well met\" to Fred and Ethel")
163
+ end
164
+
165
+ it "can handle the inner object's methods" do
166
+ expect(@outer.name).to eql "Dave"
167
+ end
168
+
169
+ it "can handle the inner syncer's methods" do
170
+ expect(@outer.find_big_green_widgets(:stripes, :spots)).to eql(
171
+ "Dave wants big green widgets, with stripes and spots")
172
+ end
173
+
174
+ it "still barfs on unknown methods" do
175
+ expect { @outer.blarg }.to raise_error NoMethodError
176
+ end
177
+
178
+ end
179
+
180
+ end
181
+
182
+ end
183
+
184
+ describe "can wrap classes" do
185
+
186
+ before do
187
+ wants_getter = lambda_maker("find_by_", "_and_")
188
+ @syncer = HookLyingSyncer.new(Person, wants_getter) do |c, wants, *args|
189
+ if wants.length != args.length
190
+ raise "#{wants.length} qualities but #{args.length} values"
191
+ end
192
+ c.find wants.zip(args).to_h
193
+ end
194
+ end
195
+
196
+ it "and receive method-name-parts and args" do
197
+ expect(@syncer.find_by_eyes_and_hair_and_skin(:red, :blue, :green)).to eql(
198
+ "Looking for a person with red eyes, blue hair, and green skin")
199
+ end
200
+
201
+ it "to override class methods" do
202
+ method_matcher = lambda { |name| name == :what_are_they ? [name] : nil }
203
+ what_they_are = "three little maids from school"
204
+ syncer = HookLyingSyncer.new(Person, method_matcher) do |c, wants, *args|
205
+ what_they_are
206
+ end
207
+ expect(syncer.what_are_they).to eql what_they_are
208
+ end
209
+
210
+ it "to add class methods" do
211
+ method_matcher = lambda { |name| name == :foo ? [name] : nil }
212
+ syncer = HookLyingSyncer.new(Person, method_matcher) do |c, wants, *args|
213
+ def c.foo
214
+ :foo
215
+ end
216
+ end
217
+ expect(syncer.foo).to equal :foo
218
+ end
219
+
220
+ it "to override the class's .new method" do
221
+ method_matcher = lambda { |name| name == :new ? [name] : nil }
222
+ syncer = HookLyingSyncer.new(Person, method_matcher) do |c, wants, *args|
223
+ c.new(args[0].reverse.capitalize)
224
+ end
225
+ expect(syncer.new("Dave").name).to eql "Evad"
226
+ end
227
+
228
+ it "to add instance methods" do
229
+ method_matcher = lambda { |name| name == :new ? [name] : nil }
230
+ syncer = HookLyingSyncer.new(Person, method_matcher) do |c, wants, *args|
231
+ c.new(args).tap { |obj|
232
+ def obj.foo
233
+ :foo
234
+ end
235
+ }
236
+ end
237
+ expect(syncer.new("Dave").foo).to equal :foo
238
+ end
239
+
240
+ it "can still call the class's methods" do
241
+ expect(@syncer.what_are_they).to eql Person.what_are_they
242
+ end
243
+
244
+ describe "wrapping an already wrapped class" do
245
+
246
+ before do
247
+ needs_getter = lambda_maker("need_with_", "_and_")
248
+ @outer = HookLyingSyncer.new(@syncer, needs_getter) do |c, wants, *args|
249
+ c.need wants.zip(args).to_h
250
+ end
251
+ end
252
+
253
+ it "can call the new thing" do
254
+ expect(@outer.need_with_eyes_and_hair(:red, :blue)).to eql(
255
+ "I need a person with red eyes and blue hair")
256
+ end
257
+
258
+ it "can still call the old thing" do
259
+ expect(@outer.find_by_eyes_and_hair(:red, :blue)).to eql(
260
+ "Looking for a person with red eyes and blue hair")
261
+ end
262
+
263
+ it "can still call the class's methods" do
264
+ expect(@syncer.what_are_they).to eql Person.what_are_they
265
+ end
266
+
267
+ end
268
+
269
+ end
270
+
271
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hook_lying_syncer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dave Aronson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Provides a decorator class you can wrap objects in (even classes!) to
56
+ keep method_missing and respond_to_missing? in sync.
57
+ email:
58
+ - hook_lying_syncer_gemspec.2.TRex@Codosaur.us
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - hook_lying_syncer.gemspec
69
+ - lib/hook_lying_syncer.rb
70
+ - lib/hook_lying_syncer/version.rb
71
+ - spec/hook_lying_syncer_spec.rb
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Keeps method_missing and respond_to_missing? in sync.
96
+ test_files:
97
+ - spec/hook_lying_syncer_spec.rb