if 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/if.rb +22 -0
  2. data/spec/if_spec.rb +38 -0
  3. metadata +59 -0
data/lib/if.rb ADDED
@@ -0,0 +1,22 @@
1
+ class BasicObject
2
+ def if(if_true, options = {})
3
+ if_true.call
4
+ end
5
+ end
6
+
7
+ module Falsey
8
+ def if(if_true, options = {})
9
+ if_false = options.fetch(:else, ->{})
10
+
11
+ if_false.call
12
+ end
13
+ end
14
+
15
+ class NilClass
16
+ include Falsey
17
+ end
18
+
19
+ class FalseClass
20
+ include Falsey
21
+ end
22
+
data/spec/if_spec.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "minitest/autorun"
2
+ require_relative "../lib/if"
3
+
4
+ describe BasicObject do
5
+ let(:object) { BasicObject.new }
6
+
7
+ it "always evaluates the first block to if" do
8
+ object.if(->{ "I'm true!" }).must_equal "I'm true!"
9
+ end
10
+
11
+ it "ignores any else blocks" do
12
+ object.if(->{ "I'm true!" },
13
+ else: ->{ "Not false!" }).must_equal "I'm true!"
14
+ end
15
+ end
16
+
17
+ describe NilClass do
18
+ it "always returns the else block" do
19
+ nil.if(-> { "I'm true!" },
20
+ else: ->{ "I'm false!" }).must_equal "I'm false!"
21
+ end
22
+
23
+ it "does nothing by default" do
24
+ nil.if(-> { "I'm true!" }).must_be_nil
25
+ end
26
+ end
27
+
28
+ describe FalseClass do
29
+ it "always returns the else block" do
30
+ false.if(-> { "I'm true!" },
31
+ else: ->{ "I'm false!" }).must_equal "I'm false!"
32
+ end
33
+
34
+ it "does nothing by default" do
35
+ false.if(-> { "I'm true!" }).must_be_nil
36
+ end
37
+ end
38
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: if
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul Mucur
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: &70173403583520 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70173403583520
25
+ description: A heavily Smalltalk-inspired implementation of the if conditional without
26
+ using keywords.
27
+ email: ruby.if@librelist.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/if.rb
33
+ - spec/if_spec.rb
34
+ homepage: http://github.com/mudge/if
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.11
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: A Ruby implementation of the if conditional.
58
+ test_files:
59
+ - spec/if_spec.rb