parameter_chain 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.
Files changed (3) hide show
  1. data/README.md +74 -0
  2. data/lib/parameter_chain.rb +46 -0
  3. metadata +58 -0
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ ## Parameter Chain
2
+
3
+ Parameter Chain allows you to chain methods to specify parameters, e.g.
4
+
5
+ ```ruby
6
+ instance.bar(123).baz(321)
7
+ ```
8
+
9
+ Which would be equivalent to:
10
+
11
+ ```ruby
12
+ instance.some_method(:bar => 123, :baz => 321)
13
+ ```
14
+
15
+ Parameter Chain provides a great out-of-the-box interface for dealing with search, or querying an API:
16
+
17
+ ```ruby
18
+ api = SomeApi.new(key)
19
+ api.category(3).price('> 1000').brand(:apple)
20
+ ```
21
+
22
+ Which would be equivalent to:
23
+
24
+ ```ruby
25
+ api = SomeApi.new(key)
26
+ api.search(:category => 3, :price => '> 1000', :brand => :apple)
27
+ ```
28
+
29
+ ## Setup
30
+
31
+ ```
32
+ gem install parameter_chain
33
+ ```
34
+
35
+ ```ruby
36
+ require 'parameter_chain'
37
+
38
+ class MyClass
39
+ def my_method(params = {})
40
+ # Do something with params, e.g.
41
+ "Params: #{params.inspect}"
42
+ end
43
+ parameter_chain :my_method, :foo, :bar
44
+ end
45
+
46
+ instance = MyClass.new
47
+ instance.foo(123).bar(321)
48
+ #=> "Params: { :foo => 123, :bar => 321 }"
49
+ ```
50
+
51
+ ## Lazy Evaluation
52
+
53
+ Parameter chains are evaluated lazily. When you call something on the end of your chain, it passes the parameters to the method and evaluates.
54
+
55
+ If you're in IRB, the inspect method is called implicitly. This is why your chains look like hashes. You can verify this by running:
56
+
57
+ ```ruby
58
+ instance.foo(123).bar(321).__class__
59
+ #=> ParameterChain
60
+ ```
61
+
62
+ ## Coming Soon
63
+
64
+ Chaining from class methods, e.g.
65
+
66
+ ```ruby
67
+ MyClass.foo(123).bar(321)
68
+ ```
69
+
70
+ ## Contribution
71
+
72
+ Feel free. No pull request is too small.
73
+
74
+ Twitter: [@cpatuzzo](https://twitter.com/#!/cpatuzzo)
@@ -0,0 +1,46 @@
1
+ class ParameterChain
2
+ alias_method :__class__, :class
3
+
4
+ instance_methods.each do |method|
5
+ next if method == :object_id
6
+ next if method =~ /^__/ # Internal methods.
7
+
8
+ undef_method method
9
+ end
10
+
11
+ def initialize(object, callback, params)
12
+ @object = object
13
+ @callback = callback
14
+
15
+ @hash = {}
16
+ params.each do |param|
17
+ __class__.send(:define_method, param) do |arg|
18
+ @hash[param] = arg
19
+ self
20
+ end
21
+ end
22
+ end
23
+
24
+ def method_missing(method, *args)
25
+ @object.send(@callback, @hash).send(method, *args)
26
+ end
27
+
28
+ def self.for(klass, callback, params)
29
+ this = self # Store self for use in block scope.
30
+
31
+ params.each do |param|
32
+ klass.send(:define_method, param) do |arg|
33
+ chain = this.new(self, callback, params)
34
+ chain.__send__(param, arg)
35
+ end
36
+ end
37
+
38
+ nil
39
+ end
40
+ end
41
+
42
+ class Object
43
+ def self.parameter_chain(callback, *params)
44
+ ParameterChain.for(self, callback, params)
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parameter_chain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christopher Patuzzo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2157132360 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2157132360
25
+ description: Chain methods to specify parameters
26
+ email: chris@patuzzo.co.uk
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - README.md
32
+ - lib/parameter_chain.rb
33
+ homepage: https://github.com/cpatuzzo/parameter_chain
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.15
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Parameter Chain
57
+ test_files: []
58
+ has_rdoc: