method_chainable 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ace4f462627b23eee9266c6c3a48b6b56e2737a
4
+ data.tar.gz: 2b8115f5c2613cda0930e82e67703a771fae8e44
5
+ SHA512:
6
+ metadata.gz: 7c4c1e919aa02070c89d206e96c970dd6349969147d1ce8416f3538009ad182cf926c0f53bec6fc0eaa56d830b22834d0bc1dafbca8a248b9076dfb5e1d5ee7d
7
+ data.tar.gz: 71cd46735e3870a69c1e58135c24a46903dc6dcd3e83e90132edeb1f32ea85131448985ba4a85b2d00d51c7fed733a531cf8715c0ec092486ddec6636e4544bc
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 method_chainable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2017 abhisheksarka
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,53 @@
1
+ # Method Chainable
2
+
3
+ The idea here is easy chaining of methods on classes or objects in Ruby. Primarily it routes output from one method to the next
4
+ ![Screenshot](https://image.ibb.co/miSA9Q/chain_link_hi.png)
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'method_chainable'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install method_chainable
21
+
22
+ ## Usage
23
+
24
+ Consider this simple example
25
+
26
+ ```ruby
27
+ class Simpleton
28
+ include MethodChainable
29
+
30
+ def foo
31
+ 'Foo'
32
+ end
33
+
34
+ def bar(n)
35
+ n + 'Bar'
36
+ end
37
+ end
38
+
39
+ # Without chaining
40
+ s = Simpleton.new
41
+ s.bar(s.foo) # FooBar
42
+
43
+ # With chaining(You should call .val to get the final result)
44
+ Simpleton.new.chain.foo.bar.val # FooBar
45
+ ```
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it ( https://github.com/abhisheksarka/method_chainable/fork )
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "method_chainable"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/example.rb ADDED
@@ -0,0 +1,22 @@
1
+ class Example
2
+ extend MethodChainable
3
+ class << self
4
+ def increment(n)
5
+ return n+1
6
+ end
7
+
8
+ def decrement(n)
9
+ return n-1
10
+ end
11
+
12
+ def mult_by_2(n)
13
+ n*2
14
+ end
15
+ end
16
+ end
17
+
18
+ Example.chain(10).increment.increment.val # 12
19
+
20
+ Example.chain(10).decrement.decrement.val # 8
21
+
22
+ Example.chain(10).increment.decrement.val # 10
@@ -0,0 +1,64 @@
1
+ # This is a proxy class that is used
2
+ # to intercept the method calls on the
3
+ # actual object
4
+
5
+
6
+ module MethodChainable
7
+ class Proxy
8
+ attr_accessor :m_chainable,
9
+ :output,
10
+ :input,
11
+ :m_chain_count
12
+
13
+
14
+ def initialize(m_chainable, input)
15
+ @m_chainable = m_chainable
16
+ @output = output
17
+ @input = input
18
+ @m_chain_count = 0
19
+ end
20
+
21
+
22
+ # Overrides method_missing since .chain
23
+ # will return an instance of this
24
+
25
+ # On a method call on the chainable object
26
+ # this will intercept it and determine how
27
+ # the call should be made
28
+ # For methods with 0 arity, no need to pass
29
+ # any args
30
+
31
+ # Remembers the output of the previous call and
32
+ # passes on to the new one
33
+ def method_missing(m, *args, &block)
34
+ m = m.to_s
35
+
36
+ # Return the final output if value is called
37
+ if m == 'val'
38
+ return output
39
+ else
40
+
41
+ # Since value is not called yet keep calling
42
+ # the actual method on chainable and pass
43
+ # the previous output
44
+
45
+ if m_chainable.respond_to? m
46
+ arg = (@m_chain_count == 0) ? input : output
47
+ arity = m_chainable.method(m).arity
48
+
49
+ if arity > 0
50
+ @output = m_chainable.send(m, arg)
51
+ else
52
+ @output = m_chainable.send(m)
53
+ end
54
+
55
+ @m_chain_count += 1
56
+
57
+ return self
58
+ else
59
+ super(m, *args, &block)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module MethodChainable
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "method_chainable/version"
2
+ require "method_chainable/proxy"
3
+
4
+ module MethodChainable
5
+
6
+ # Returns a Proxy object which intercepts
7
+ # method calls using method missing
8
+ def chain(input=nil)
9
+ MethodChainable::Proxy.new(self, input)
10
+ end
11
+ end
@@ -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 'method_chainable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "method_chainable"
8
+ spec.version = MethodChainable::VERSION
9
+ spec.authors = ["abhisheksarka"]
10
+ spec.email = ["abhisheksarka@gmail.com"]
11
+ spec.summary = %q{Chaining methods on classes and objects until you decide to retrieve the value}
12
+ spec.description = %q{Chaining methods on classes and objects until you decide to retrieve the value}
13
+ spec.homepage = "https://github.com/abhisheksarka/method_chainable"
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 "pry"
24
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: method_chainable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - abhisheksarka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-14 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: pry
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: Chaining methods on classes and objects until you decide to retrieve
56
+ the value
57
+ email:
58
+ - abhisheksarka@gmail.com
59
+ executables:
60
+ - console
61
+ - setup
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/example.rb
73
+ - lib/method_chainable.rb
74
+ - lib/method_chainable/proxy.rb
75
+ - lib/method_chainable/version.rb
76
+ - method_chainable.gemspec
77
+ homepage: https://github.com/abhisheksarka/method_chainable
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.3
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Chaining methods on classes and objects until you decide to retrieve the
101
+ value
102
+ test_files: []