first_of 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: bfa6409aeab593f113e0a33c7101b4436e48a376
4
+ data.tar.gz: 7819687d70b99d685eb4b558a2650849250f6df5
5
+ SHA512:
6
+ metadata.gz: 520d9061901771af7850f611e2696376a090447d6a236f6dc6d28819a176d00924ccb7c0b4b520990e1e37e9bed6a1fc3d8cb982b352874d8fb79787990202ac
7
+ data.tar.gz: 0b218207ef65993a280fa96b1ffc79728350a954ab43075214165c6fd9747c47cfcd4d9896f80457ecf5c8e400065593ec1ed62301914cfe2d3a472125a87661
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .ruby-*
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in first_of.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brandon Dewitt
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,29 @@
1
+ # FirstOf
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'first_of'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install first_of
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ desc "Run specs"
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ desc "Run specs (default)"
8
+ task :default => :spec
9
+
10
+ Dir["lib/tasks/**/*.rake"].each { |ext| load ext } if defined?(Rake)
data/first_of.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'first_of/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "first_of"
8
+ spec.version = FirstOf::VERSION
9
+ spec.authors = ["Brandon Dewitt"]
10
+ spec.email = ["brandonsdewitt@gmail.com"]
11
+ spec.description = %q{ prioritize values of fallback methods }
12
+ spec.summary = %q{ first_of allows you to setup a method that falls back to the next prioritized value
13
+ if the first and subsequent return objects are not present. To be used in situations
14
+ where multiple values are "valid" but have a priority of when to use each value }
15
+ spec.homepage = ""
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files`.split($/)
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "activesupport"
24
+ spec.add_dependency "try_chain"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "rake"
29
+ spec.add_development_dependency "rspec"
30
+ end
@@ -0,0 +1,5 @@
1
+ require 'first_of'
2
+
3
+ class Object
4
+ include ::FirstOf
5
+ end
@@ -0,0 +1,3 @@
1
+ module FirstOf
2
+ VERSION = "0.0.1"
3
+ end
data/lib/first_of.rb ADDED
@@ -0,0 +1,81 @@
1
+ require "first_of/version"
2
+
3
+ require "try_chain"
4
+ require "active_support/core_ext/object/blank"
5
+ require "active_support/core_ext/array/extract_options"
6
+ require "active_support/core_ext/object/try"
7
+
8
+ module FirstOf
9
+
10
+ # Should take arguments:
11
+ # first_of(:symbol1, :symbol2) # will return first respond to and present
12
+ # first_of([:symbol1, :symbol2]) # will return value if try_chain(:symbol1, :symbol2) present
13
+ # first_of(1 => :symbol1, 2 => :symbol2) # will prioritize trying by key and return first present
14
+ # first_of(1 => :symbol1, 2 => lambda { _call }) # will prioritize and only execute callable if first not present
15
+ # first_of(1 => [:symbol1, :symbol2], 2 => -> { _call }) # will prioritize and execute try_chain on first and call 2nd if first not present
16
+ # first_of(1 => :symbol1, 2 => 4) # will prioritize and return value (4) if first not present
17
+ # first_of(lambda { _call }, 1 => :symbol1, 2 => :symbol2)
18
+ def first_of(*args)
19
+ extract_hash = args.extract_options!
20
+
21
+ # Don't care if the hash is empty or not because of #each call
22
+ sorted_keys = extract_hash.keys.sort
23
+ sorted_keys.each do |key|
24
+ args << extract_hash[key]
25
+ end
26
+
27
+ args.each do |argument|
28
+ if argument.respond_to?(:call)
29
+ value = argument.call
30
+ else
31
+ value = _extract_from_message_chain(argument) # calls try_chain if array, or try if symbol, or returns value
32
+ end
33
+
34
+ return value if _valid_value?(value) # return value if found
35
+ end
36
+
37
+ return nil
38
+ end
39
+
40
+ def first_of!(*args)
41
+ extract_hash = args.extract_options!
42
+
43
+ # Don't care if the hash is empty or not because of #each call
44
+ sorted_keys = extract_hash.keys.sort
45
+ sorted_keys.each do |key|
46
+ args << extract_hash[key]
47
+ end
48
+
49
+ args.each do |argument|
50
+ if argument.respond_to?(:call)
51
+ value = argument.call
52
+ else
53
+ value = _extract_from_message_chain(argument, :try_chain!, :proxy_try_chain!) # calls try_chain if array, or try if symbol, or returns value
54
+ end
55
+
56
+ return value if _valid_value?(value) # return value if found
57
+ end
58
+
59
+ return nil
60
+ end
61
+
62
+ private
63
+
64
+ def _extract_from_message_chain(methods_or_value, try_method = :try_chain, proxy_try_method = :proxy_try_chain)
65
+ case methods_or_value
66
+ when Symbol, Array then
67
+ if self.respond_to?(:try_chain)
68
+ self.__send__(try_method, *methods_or_value)
69
+ else
70
+ ::TryChain.__send__(proxy_try_method, self, *methods_or_value)
71
+ end
72
+ else
73
+ methods_or_value
74
+ end
75
+ end
76
+
77
+ def _valid_value?(value)
78
+ value.present? || [true, false].include?(value)
79
+ end
80
+
81
+ end
@@ -0,0 +1,208 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::FirstOf do
4
+ class SpecObject
5
+ include ::FirstOf
6
+
7
+ def callable; return lambda { "here" }; end
8
+ def nothing; nil; end
9
+ def selfie; self; end
10
+ def something; true; end
11
+ def symbol; :symbol; end
12
+ end
13
+
14
+ context "#first_of" do
15
+ context ":symbol, :symbol args" do
16
+ it "returns the first symbolized argument that the object responds_to if the result is present" do
17
+ test = SpecObject.new
18
+ test.first_of(:something, :nothing).should be_true
19
+ end
20
+
21
+ it "returns the second symbolized argument that the object responds_to if the first is not present" do
22
+ test = SpecObject.new
23
+ test.first_of(:nothing, :something).should be_true
24
+ end
25
+
26
+ it "returns a symbol if the first present value is a symbol" do
27
+ test = SpecObject.new
28
+ test.first_of(:nothing, :symbol).should eq(:symbol)
29
+ end
30
+
31
+ it "does not throw an exception if the object does not respond to the symbol" do
32
+ test = SpecObject.new
33
+ expect { test.first_of(:nothing, :what) }.to_not raise_error
34
+ end
35
+ end
36
+
37
+ context "[:symbol :symbol]" do
38
+ it "returns the first symbolized argument that the object responds_to if the result is present" do
39
+ test = SpecObject.new
40
+ test.first_of([:selfie, :nothing]).should be_nil
41
+ end
42
+
43
+ it "returns the second symbolized argument that the object responds_to if the first is not present" do
44
+ test = SpecObject.new
45
+ test.first_of([:selfie, :something]).should be_true
46
+ end
47
+
48
+ it "returns a symbol if the first present value is a symbol" do
49
+ test = SpecObject.new
50
+ test.first_of([:selfie, :symbol]).should eq(:symbol)
51
+ end
52
+
53
+ it "does not throw an exception if the object does not respond to the symbol" do
54
+ test = SpecObject.new
55
+ expect { test.first_of([:selfie, :what]) }.to_not raise_error
56
+ end
57
+ end
58
+
59
+ context "lambda {}, :symbol" do
60
+ it "returns the first symbolized argument that the object responds_to if the result is present" do
61
+ test = SpecObject.new
62
+ test.first_of( -> { nil }, :nothing).should be_nil
63
+ end
64
+
65
+ it "returns the second symbolized argument that the object responds_to if the first is not present" do
66
+ test = SpecObject.new
67
+ test.first_of( -> { nil }, :something).should be_true
68
+ end
69
+
70
+ it "returns a symbol if the first present value is a symbol" do
71
+ test = SpecObject.new
72
+ test.first_of( -> { :symbol }, nil).should eq(:symbol)
73
+ end
74
+
75
+ it "does not throw an exception if the object does not respond to the symbol" do
76
+ test = SpecObject.new
77
+ expect { test.first_of( -> { nil }, :what) }.to_not raise_error
78
+ end
79
+ end
80
+
81
+ context "2 => :symbol, 1 => :symbol" do
82
+ it "prioritizes the return value by the hash key" do
83
+ test = SpecObject.new
84
+ test.first_of(2 => :selfie, 1 => :symbol).should eq(:symbol)
85
+ end
86
+
87
+ it "does not return the first hash key if not present" do
88
+ test = SpecObject.new
89
+ test.first_of(2 => :symbol, 1 => nil).should eq(:symbol)
90
+ end
91
+
92
+ it "returns a symbol if the first present value is a lambda that returns a symbol" do
93
+ test = SpecObject.new
94
+ test.first_of(1 => lambda { :selfie }, 2 => :symbol).should eq(:selfie)
95
+ end
96
+
97
+ it "does not throw an exception if the object does not respond to the symbol" do
98
+ test = SpecObject.new
99
+ expect { test.first_of(2 => :selfie, 1 => :what) }.to_not raise_error
100
+ end
101
+ end
102
+
103
+ context "lambda {}, 2 => :symbol, 1 => :symbol" do
104
+ it "prioritizes the lambda over the return value by the hash key" do
105
+ test = SpecObject.new
106
+ test.first_of(lambda { :lambda }, 2 => :selfie, 1 => :symbol).should eq(:lambda)
107
+ end
108
+ end
109
+ end
110
+
111
+ context "#first_of!" do
112
+ context ":symbol, :symbol args" do
113
+ it "returns the first symbolized argument that the object responds_to if the result is present" do
114
+ test = SpecObject.new
115
+ test.first_of!(:something, :nothing).should be_true
116
+ end
117
+
118
+ it "returns the second symbolized argument that the object responds_to if the first is not present" do
119
+ test = SpecObject.new
120
+ test.first_of!(:nothing, :something).should be_true
121
+ end
122
+
123
+ it "returns a symbol if the first present value is a symbol" do
124
+ test = SpecObject.new
125
+ test.first_of!(:nothing, :symbol).should eq(:symbol)
126
+ end
127
+
128
+ it "throws an exception if the object does not respond to the symbol" do
129
+ test = SpecObject.new
130
+ expect { test.first_of!(:nothing, :what) }.to raise_error
131
+ end
132
+ end
133
+
134
+ context "[:symbol :symbol]" do
135
+ it "returns the first symbolized argument that the object responds_to if the result is present" do
136
+ test = SpecObject.new
137
+ test.first_of!([:selfie, :nothing]).should be_nil
138
+ end
139
+
140
+ it "returns the second symbolized argument that the object responds_to if the first is not present" do
141
+ test = SpecObject.new
142
+ test.first_of!([:selfie, :something]).should be_true
143
+ end
144
+
145
+ it "returns a symbol if the first present value is a symbol" do
146
+ test = SpecObject.new
147
+ test.first_of!([:selfie, :symbol]).should eq(:symbol)
148
+ end
149
+
150
+ it "throws an exception if the object does not respond to the symbol" do
151
+ test = SpecObject.new
152
+ expect { test.first_of!([:selfie, :what]) }.to raise_error
153
+ end
154
+ end
155
+
156
+ context "lambda {}, :symbol" do
157
+ it "returns the first symbolized argument that the object responds_to if the result is present" do
158
+ test = SpecObject.new
159
+ test.first_of!( -> { nil }, :nothing).should be_nil
160
+ end
161
+
162
+ it "returns the second symbolized argument that the object responds_to if the first is not present" do
163
+ test = SpecObject.new
164
+ test.first_of!( -> { nil }, :something).should be_true
165
+ end
166
+
167
+ it "returns a symbol if the first present value is a symbol" do
168
+ test = SpecObject.new
169
+ test.first_of!( -> { :symbol }, nil).should eq(:symbol)
170
+ end
171
+
172
+ it "throws an exception if the object does not respond to the symbol" do
173
+ test = SpecObject.new
174
+ expect { test.first_of!( -> { nil }, :what) }.to raise_error
175
+ end
176
+ end
177
+
178
+ context "2 => :symbol, 1 => :symbol" do
179
+ it "prioritizes the return value by the hash key" do
180
+ test = SpecObject.new
181
+ test.first_of!(2 => :selfie, 1 => :symbol).should eq(:symbol)
182
+ end
183
+
184
+ it "does not return the first hash key if not present" do
185
+ test = SpecObject.new
186
+ test.first_of!(2 => :symbol, 1 => nil).should eq(:symbol)
187
+ end
188
+
189
+ it "returns a symbol if the first present value is a lambda that returns a symbol" do
190
+ test = SpecObject.new
191
+ test.first_of!(1 => lambda { :selfie }, 2 => :symbol).should eq(:selfie)
192
+ end
193
+
194
+ it "throws an exception if the object does not respond to the symbol" do
195
+ test = SpecObject.new
196
+ expect { test.first_of!(2 => :selfie, 1 => :what) }.to raise_error
197
+ end
198
+ end
199
+
200
+ context "lambda {}, 2 => :symbol, 1 => :symbol" do
201
+ it "prioritizes the lambda over the return value by the hash key" do
202
+ test = SpecObject.new
203
+ test.first_of!(lambda { :lambda }, 2 => :selfie, 1 => :symbol).should eq(:lambda)
204
+ end
205
+ end
206
+ end
207
+
208
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ ::Bundler.require(:default, :development, :test)
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: first_of
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Dewitt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: try_chain
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: ' prioritize values of fallback methods '
98
+ email:
99
+ - brandonsdewitt@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - first_of.gemspec
110
+ - lib/first_of.rb
111
+ - lib/first_of/core_ext/object/first_of.rb
112
+ - lib/first_of/version.rb
113
+ - spec/first_of_spec.rb
114
+ - spec/spec_helper.rb
115
+ homepage: ''
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.0.3
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: first_of allows you to setup a method that falls back to the next prioritized
139
+ value if the first and subsequent return objects are not present. To be used in
140
+ situations where multiple values are "valid" but have a priority of when to use
141
+ each value
142
+ test_files:
143
+ - spec/first_of_spec.rb
144
+ - spec/spec_helper.rb