if 1.0.0 → 1.1.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 +7 -0
- data/lib/if.rb +23 -4
- data/spec/if_spec.rb +99 -21
- data/spec/spec_helper.rb +17 -0
- metadata +19 -16
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 48f4dc2f902936d9e33d8dbd8b5ac638de6cb812
|
4
|
+
data.tar.gz: 537cdc652b31d2b68cbe2380d2416e4d4e36bdda
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f9bfba1de91f2f086b3d6026f5751df343c317d74e3a15c6e288ff169945a6f64d1e373335cf6b7fae5fe5f086c538d5fa82bc89290fba483f005bcb03e47d18
|
7
|
+
data.tar.gz: 6f9b97f3d2619ebc8a3eaa33e3c06f94574ae7c1a0a062f541f9a48ce03908c3e0c4bbe8794ad8d3d956898cd251f9b85efcefde6afa365e2949b0d8c566c09d
|
data/lib/if.rb
CHANGED
@@ -1,15 +1,35 @@
|
|
1
1
|
class BasicObject
|
2
|
-
def if(if_true,
|
2
|
+
def if(if_true, _options = {})
|
3
3
|
if_true.call
|
4
4
|
end
|
5
|
+
|
6
|
+
def if_true
|
7
|
+
yield
|
8
|
+
|
9
|
+
self
|
10
|
+
end
|
11
|
+
|
12
|
+
def if_false
|
13
|
+
self
|
14
|
+
end
|
5
15
|
end
|
6
16
|
|
7
17
|
module Falsey
|
8
|
-
def if(
|
9
|
-
if_false = options.fetch(:else, ->{})
|
18
|
+
def if(_if_true, options = {})
|
19
|
+
if_false = options.fetch(:else, -> {})
|
10
20
|
|
11
21
|
if_false.call
|
12
22
|
end
|
23
|
+
|
24
|
+
def if_true
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def if_false
|
29
|
+
yield
|
30
|
+
|
31
|
+
self
|
32
|
+
end
|
13
33
|
end
|
14
34
|
|
15
35
|
class NilClass
|
@@ -19,4 +39,3 @@ end
|
|
19
39
|
class FalseClass
|
20
40
|
include Falsey
|
21
41
|
end
|
22
|
-
|
data/spec/if_spec.rb
CHANGED
@@ -1,38 +1,116 @@
|
|
1
|
-
require
|
2
|
-
require_relative "../lib/if"
|
1
|
+
require 'if'
|
3
2
|
|
4
|
-
describe BasicObject do
|
3
|
+
RSpec.describe BasicObject do
|
5
4
|
let(:object) { BasicObject.new }
|
6
5
|
|
7
|
-
|
8
|
-
|
6
|
+
describe '#if' do
|
7
|
+
it 'always evaluates the first block to if' do
|
8
|
+
expect(object.if(-> { "I'm true!" })).to eq("I'm true!")
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'ignores any else blocks' do
|
12
|
+
expect(object.if(-> { "I'm true!" },
|
13
|
+
else: -> { "I'm false!" })).to eq("I'm true!")
|
14
|
+
end
|
9
15
|
end
|
10
16
|
|
11
|
-
|
12
|
-
|
13
|
-
|
17
|
+
describe '#if_true' do
|
18
|
+
it 'evaluates the given block' do
|
19
|
+
expect { object.if_true { puts "I'm true!" } }
|
20
|
+
.to output("I'm true!\n").to_stdout
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'returns the object for chaining' do
|
24
|
+
expect(object.if_true { "I'm true!" }).to eq(object)
|
25
|
+
end
|
14
26
|
end
|
15
|
-
end
|
16
27
|
|
17
|
-
describe
|
18
|
-
|
19
|
-
|
20
|
-
|
28
|
+
describe '#if_false' do
|
29
|
+
it 'returns self' do
|
30
|
+
expect(object.if_false { "I'm false!" }).to eq(object)
|
31
|
+
end
|
21
32
|
end
|
22
33
|
|
23
|
-
|
24
|
-
|
34
|
+
describe 'chaining #if_true and #if_false' do
|
35
|
+
it 'evaluates based on the original object' do
|
36
|
+
expect { object.if_true { puts "I'm true!"; false }
|
37
|
+
.if_false { puts "I'm false!" } }
|
38
|
+
.to output("I'm true!\n").to_stdout
|
39
|
+
end
|
25
40
|
end
|
26
41
|
end
|
27
42
|
|
28
|
-
describe
|
29
|
-
|
30
|
-
|
31
|
-
|
43
|
+
RSpec.describe NilClass do
|
44
|
+
describe '#if' do
|
45
|
+
it 'always returns the else block' do
|
46
|
+
expect(nil.if(-> { "I'm true!" },
|
47
|
+
else: -> { "I'm false!" })).to eq("I'm false!")
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'does nothing by default' do
|
51
|
+
expect(nil.if(-> { "I'm true!" })).to be_nil
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#if_true' do
|
56
|
+
it 'returns self' do
|
57
|
+
expect(nil.if_true { "I'm true!" }).to be_nil
|
58
|
+
end
|
32
59
|
end
|
33
60
|
|
34
|
-
|
35
|
-
|
61
|
+
describe '#if_false' do
|
62
|
+
it 'evaluates the given block' do
|
63
|
+
expect { nil.if_false { puts "I'm false!" } }
|
64
|
+
.to output("I'm false!\n").to_stdout
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'returns nil' do
|
68
|
+
expect(nil.if_false { "I'm false!" }).to be_nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe 'chaining #if_false and #if_true' do
|
73
|
+
it 'evaluates based on the original object' do
|
74
|
+
expect { nil.if_false { puts "I'm false!"; true }
|
75
|
+
.if_true { puts "I'm true!" } }
|
76
|
+
.to output("I'm false!\n").to_stdout
|
77
|
+
end
|
36
78
|
end
|
37
79
|
end
|
38
80
|
|
81
|
+
RSpec.describe FalseClass do
|
82
|
+
describe '#if' do
|
83
|
+
it 'always returns the else block' do
|
84
|
+
expect(false.if(-> { "I'm true!" },
|
85
|
+
else: -> { "I'm false!" })).to eq("I'm false!")
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'does nothing by default' do
|
89
|
+
expect(false.if(-> { "I'm true!" })).to be_nil
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe '#if_true' do
|
94
|
+
it 'returns self' do
|
95
|
+
expect(false.if_true { "I'm true!" }).to eq(false)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe '#if_false' do
|
100
|
+
it 'evaluates the given block' do
|
101
|
+
expect { false.if_false { puts "I'm false!" } }.to output("I'm false!\n").to_stdout
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'returns nil' do
|
105
|
+
expect(false.if_false { "I'm false!" }).to eq(false)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe 'chaining #if_false and #if_true' do
|
110
|
+
it 'evaluates based on the original object' do
|
111
|
+
expect { nil.if_false { puts "I'm false!"; true }
|
112
|
+
.if_true { puts "I'm true!" } }
|
113
|
+
.to output("I'm false!\n").to_stdout
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
RSpec.configure do |config|
|
2
|
+
config.filter_run :focus
|
3
|
+
config.run_all_when_everything_filtered = true
|
4
|
+
config.disable_monkey_patching!
|
5
|
+
config.warnings = true
|
6
|
+
config.order = :random
|
7
|
+
Kernel.srand config.seed
|
8
|
+
config.default_formatter = 'doc' if config.files_to_run.one?
|
9
|
+
|
10
|
+
config.expect_with :rspec do |expectations|
|
11
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
12
|
+
end
|
13
|
+
|
14
|
+
config.mock_with :rspec do |mocks|
|
15
|
+
mocks.verify_partial_doubles = true
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,27 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: if
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Paul Mucur
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-03-15 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement:
|
17
|
-
none: false
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version: '
|
19
|
+
version: '3.2'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
25
27
|
description: A heavily Smalltalk-inspired implementation of the if conditional without
|
26
28
|
using keywords.
|
27
29
|
email: ruby.if@librelist.com
|
@@ -31,29 +33,30 @@ extra_rdoc_files: []
|
|
31
33
|
files:
|
32
34
|
- lib/if.rb
|
33
35
|
- spec/if_spec.rb
|
34
|
-
|
36
|
+
- spec/spec_helper.rb
|
37
|
+
homepage: https://github.com/mudge/if
|
35
38
|
licenses: []
|
39
|
+
metadata: {}
|
36
40
|
post_install_message:
|
37
41
|
rdoc_options: []
|
38
42
|
require_paths:
|
39
43
|
- lib
|
40
44
|
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
45
|
requirements:
|
43
|
-
- -
|
46
|
+
- - ">="
|
44
47
|
- !ruby/object:Gem::Version
|
45
48
|
version: '0'
|
46
49
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
-
none: false
|
48
50
|
requirements:
|
49
|
-
- -
|
51
|
+
- - ">="
|
50
52
|
- !ruby/object:Gem::Version
|
51
53
|
version: '0'
|
52
54
|
requirements: []
|
53
55
|
rubyforge_project:
|
54
|
-
rubygems_version:
|
56
|
+
rubygems_version: 2.4.5
|
55
57
|
signing_key:
|
56
|
-
specification_version:
|
58
|
+
specification_version: 4
|
57
59
|
summary: A Ruby implementation of the if conditional.
|
58
60
|
test_files:
|
59
61
|
- spec/if_spec.rb
|
62
|
+
- spec/spec_helper.rb
|