conditional 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +80 -0
- data/Rakefile +2 -0
- data/conditional.gemspec +23 -0
- data/lib/conditional.rb +34 -0
- data/lib/conditional/core_ext.rb +9 -0
- data/spec/conditional_spec.rb +51 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a962acf7811ae585689cd9429e2af26c213d99cf
|
4
|
+
data.tar.gz: c11a00ce1ea03fa2f4a30abdbadc5a396e793679
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d0ea429b99b9cfa15e777a5659f4409c7ef2b37d16bac41b0d0fc3c8cdedd6de9a4cfcce9ae738f3294a580e8d44c65e108bc018bc409651e74a086a5ee155c2
|
7
|
+
data.tar.gz: 30bd972b6c355809ff56a86e5e7412b0379539f3c572aefe319547364c05797bd9f508766198c09f9e64ad695c467ad9e52000c74efcefdbb7ee8c9658c9b0d9
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Cameron Martin
|
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
|
+
# Conditional
|
2
|
+
|
3
|
+
Ruby's if/elsif/else, but not using native constructs.
|
4
|
+
Mostly just as an exercise on how ruby should have implemented ifs, but it has some practical uses (see usage).
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'conditional'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install conditional
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
It works almost exactly like ruby's native constructs, except for:
|
25
|
+
|
26
|
+
* Body of conditionals have their own scope
|
27
|
+
* else/else_nil branch must be specified
|
28
|
+
* The condition is passed to the block
|
29
|
+
|
30
|
+
The third point comes in very useful when dealing with regexes.
|
31
|
+
|
32
|
+
Instead of
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
match = %r{^http://github.com/(.*?)/}.match(url)
|
36
|
+
|
37
|
+
user_name = if match
|
38
|
+
match[1]
|
39
|
+
else
|
40
|
+
raise 'Incorrect url format'
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
which pollutes your current scope with the throw-away match object, you can write
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
user_name = If %r{^http://github.com/(.*?)/}.match(url) do |match|
|
48
|
+
match[1]
|
49
|
+
end.else do
|
50
|
+
raise 'Incorrect url format'
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
elsifs are supported too:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
If if_condition do
|
58
|
+
# do something
|
59
|
+
end.elsif another_condition do
|
60
|
+
# or this
|
61
|
+
end.else do
|
62
|
+
# otherwise this
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
If you don't want to supply an if condition, you can use else_nil:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
value_or_nil = If condition do
|
70
|
+
:value
|
71
|
+
end.else_nil
|
72
|
+
```
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
1. Fork it ( https://github.com/cameron-martin/conditional/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
data/conditional.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'conditional'
|
7
|
+
spec.version = '0.0.1'
|
8
|
+
spec.authors = ['Cameron Martin']
|
9
|
+
spec.email = ['cameronmartin123@gmail.com']
|
10
|
+
spec.summary = %q{Non-native conditionals in ruby}
|
11
|
+
spec.description = %q{Non-native conditionals in ruby}
|
12
|
+
spec.homepage = 'https://github.com/cameron-martin/conditional'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
23
|
+
end
|
data/lib/conditional.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'conditional/core_ext'
|
2
|
+
|
3
|
+
class Conditional
|
4
|
+
def self.if(*args, &block)
|
5
|
+
new.if(*args, &block)
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@conditions = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def if(condition, &block)
|
13
|
+
@conditions << [condition, block]
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
alias_method :elsif, :if
|
18
|
+
|
19
|
+
def else(&block)
|
20
|
+
@conditions << [true, block]
|
21
|
+
evaluate
|
22
|
+
end
|
23
|
+
|
24
|
+
def else_nil
|
25
|
+
self.else {}
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def evaluate
|
31
|
+
condition, block = @conditions.find { |(condition, _)| condition }
|
32
|
+
block.call(condition)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'conditional'
|
2
|
+
|
3
|
+
describe Conditional do
|
4
|
+
|
5
|
+
context 'with all branches' do
|
6
|
+
|
7
|
+
def build_conditional(if_value, elsif_value, else_value)
|
8
|
+
If if_value do |value|
|
9
|
+
value
|
10
|
+
end.elsif elsif_value do |value|
|
11
|
+
value
|
12
|
+
end.else do
|
13
|
+
else_value
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'evaluates to if block for truthy if' do
|
18
|
+
expect(build_conditional(:if_value, :elsif_value, :else_value)).to eq(:if_value)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'evaluates to elsif block for falsey if, truthy elsif' do
|
22
|
+
expect(build_conditional(false, :elsif_value, :else_value)).to eq(:elsif_value)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'evaluates to else block for falsey if, falsey elsif' do
|
26
|
+
expect(build_conditional(false, false, :else_value)).to eq(:else_value)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'with else_nil' do
|
32
|
+
|
33
|
+
it 'evaluates to if block for truthy if' do
|
34
|
+
return_value = If(true) do
|
35
|
+
:true_value
|
36
|
+
end.else_nil
|
37
|
+
|
38
|
+
expect(return_value).to eq(:true_value)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'evaluates to nil for falsey if' do
|
42
|
+
return_value = If(false) do
|
43
|
+
:true_value
|
44
|
+
end.else_nil
|
45
|
+
|
46
|
+
expect(return_value).to eq(nil)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: conditional
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cameron Martin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-08 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Non-native conditionals in ruby
|
56
|
+
email:
|
57
|
+
- cameronmartin123@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- conditional.gemspec
|
68
|
+
- lib/conditional.rb
|
69
|
+
- lib/conditional/core_ext.rb
|
70
|
+
- spec/conditional_spec.rb
|
71
|
+
homepage: https://github.com/cameron-martin/conditional
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.2.2
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Non-native conditionals in ruby
|
95
|
+
test_files:
|
96
|
+
- spec/conditional_spec.rb
|