tieable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MjQ0MDg1ZjNmY2MwYjM5OWVhMGM1ODU1YWNlNThkMTlhZGE3ODgwNw==
5
+ data.tar.gz: !binary |-
6
+ NmFkNzI2ZjIzNmExZTY2ZmMwMzM0N2RmYzY3ODIxMzIzZWM0ZmYxMQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YmJlYThiZTYzMzA3YWZjNjE5ODFiZmU4OTJhZjY4ZWVjYTVlNmIxODJhZDI3
10
+ MjJmZWI1NzgxMzlmNmU4ZWFjZmI0YmVjN2E1ZDNhZTQzMThkOGExOGZhOTJk
11
+ ZmVlNmU5YzRkZmVhMTIxZmZlYTVjOTExODVmN2M3M2IwMDBhNjM=
12
+ data.tar.gz: !binary |-
13
+ ODgwZmRjYmQ4NWQxMmM4YTg0ODBiMzE4MGM2NzJmMzUyZjBiMjdiNjIxYTgx
14
+ OGU3NzA1ODdmNmU0YTUyZWQyYTVhOTUyNDVhZTgwMTk3Y2QyNTY4M2IxZGZm
15
+ MjkwMDIyOTZlMmIwNmRlNzJhNjI4MTRjN2VmYjAzODFhNzE2ODI=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tieable.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alexander Avoyants
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.
@@ -0,0 +1,35 @@
1
+ # Tieable
2
+
3
+ Provides Object#tie method which acts as public_send if receives some arguments,
4
+ except if first argument is nil it returns object itself.
5
+
6
+ Method created for creating conditional method chains.
7
+
8
+ Also can be used with block only (and no arguments).
9
+ In this case method returns result of block call (whith self as block argument) or self (if block returns nil or false).
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'tieable'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install tieable
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it ( http://github.com/<my-github-username>/tieable/fork )
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.rspec_opts = ["-c", "-f documentation", "-r ./spec/spec_helper.rb"]
6
+ t.pattern = 'spec/**/*_spec.rb'
7
+ end
8
+ task default: :spec
@@ -0,0 +1,21 @@
1
+ require "tieable/version"
2
+
3
+ module Tieable
4
+ def tie(*args, &block)
5
+ if args.size > 0
6
+ if args.first.nil?
7
+ self
8
+ else
9
+ self.public_send(*args, &block)
10
+ end
11
+ elsif block_given?
12
+ yield(self) || self
13
+ else
14
+ raise ArgumentError, "#{self.class}#tie method requires any arguments or block"
15
+ end
16
+ end
17
+ end
18
+
19
+ class Object
20
+ include Tieable
21
+ end
@@ -0,0 +1,3 @@
1
+ module Tieable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "tieable"
2
+
3
+ RSpec.configure do |config|
4
+ config.mock_with :rspec
5
+ end
@@ -0,0 +1,84 @@
1
+ require "spec_helper"
2
+
3
+ describe Object do
4
+ context 'receives some arguments' do
5
+ context 'returns self if first argument is nil' do
6
+ specify 'nil argument' do
7
+ expect(subject.tie(nil)).to eq subject
8
+ end
9
+
10
+ specify 'nil and one more argument' do
11
+ expect(subject.tie(nil, 'arg1')).to eq subject
12
+ end
13
+
14
+ specify 'nil and two more arguments' do
15
+ expect(subject.tie(nil, 'arg1', 'arg2')).to eq subject
16
+ end
17
+
18
+ specify 'nil argument with block' do
19
+ expect(subject.tie(nil) {|o| o.do_something } ).to eq subject
20
+ end
21
+
22
+ specify 'nil and two arguments with block' do
23
+ expect(subject.tie(nil, 'arg1', 'arg2') {|o| o.do_something } ).to eq subject
24
+ end
25
+ end
26
+
27
+ context 'acts like public_send if there is not-nil first argument' do
28
+ let(:expected_result) { double('result returned from method be_cool') }
29
+ let(:p) { proc {} }
30
+
31
+ specify 'method name argument' do
32
+ subject.should_receive(:be_cool).with().once.and_return(expected_result)
33
+ subject.tie(:be_cool).should == expected_result
34
+ end
35
+
36
+ specify 'method name and one more argument' do
37
+ subject.should_receive(:be_cool).with('arg1').once.and_return(expected_result)
38
+ subject.tie(:be_cool, 'arg1').should == expected_result
39
+ end
40
+
41
+ specify 'method name and two more arguments' do
42
+ subject.should_receive(:be_cool).with('arg1', 'arg2').once.and_return(expected_result)
43
+ subject.tie(:be_cool, 'arg1', 'arg2').should == expected_result
44
+ end
45
+
46
+ specify 'method name with block' do
47
+ subject.should_receive(:be_cool).and_yield(&p).once.and_return(expected_result)
48
+ subject.tie(:be_cool, &p).should == expected_result
49
+ end
50
+
51
+ specify 'method name and two more arguments with block' do
52
+ subject.should_receive(:be_cool).with('arg1', 'arg2').and_yield(&p).once.and_return(expected_result)
53
+ subject.tie(:be_cool, 'arg1', 'arg2', &p).should == expected_result
54
+ end
55
+
56
+ it 'indeed uses public_send' do
57
+ subject.should_receive(:public_send).with(:be_cool, 'arg1').once.and_return(expected_result)
58
+ subject.tie(:be_cool, 'arg1').should == expected_result
59
+ end
60
+ end
61
+ end
62
+
63
+ context 'only block given (no arguments)' do
64
+ let(:p) { proc {|o| 'result returned from calling block on self' } }
65
+
66
+ it 'yields self and returns result if it is not nil' do
67
+ subject.tie(&p).should == 'result returned from calling block on self'
68
+ end
69
+
70
+ it 'yields self and returns self if result of yielding is nil' do
71
+ subject.tie{|o| nil }.should == subject
72
+ end
73
+
74
+ it 'yields self and returns self if result of yielding is false' do
75
+ subject.tie{|o| false }.should == subject
76
+ end
77
+ end
78
+
79
+ context 'no arguments and no block given' do
80
+ it 'raises ArgumentError' do
81
+ expect { subject.tie }.to raise_error ArgumentError, "Object#tie method requires any arguments or block"
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tieable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tieable"
8
+ spec.version = Tieable::VERSION
9
+ spec.authors = ["Alexander Avoyants"]
10
+ spec.email = ["shhavel@gmail.com"]
11
+ spec.summary = %q{Provides Object#tie method which acts as public_send if receives some arguments,
12
+ except if first argument is nil it returns object itself.}
13
+ spec.description = %q{Method created for creating conditional method chains.
14
+ Also can be used with block only (no arguments).
15
+ In this case method returns result of calling block on self or itself (if block returns nil or false).}
16
+ spec.homepage = ""
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0")
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tieable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Avoyants
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-26 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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: ! 'Method created for creating conditional method chains.
56
+
57
+ Also can be used with block only (no arguments).
58
+
59
+ In this case method returns result of calling block on self or itself (if block
60
+ returns nil or false).'
61
+ email:
62
+ - shhavel@gmail.com
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - .gitignore
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - lib/tieable.rb
73
+ - lib/tieable/version.rb
74
+ - spec/spec_helper.rb
75
+ - spec/tieable_spec.rb
76
+ - tieable.gemspec
77
+ homepage: ''
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.2.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Provides Object#tie method which acts as public_send if receives some arguments,
101
+ except if first argument is nil it returns object itself.
102
+ test_files:
103
+ - spec/spec_helper.rb
104
+ - spec/tieable_spec.rb