hexx 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fb67d554082720fe92957b12996f0a313b3b3ae8
4
- data.tar.gz: 197971ca631d755c770543cf8393d173bff65ff3
3
+ metadata.gz: c3e5d03628aadfc3fbfda454f834e2f573658bf6
4
+ data.tar.gz: 0ddf399a8ff32c8d78e3aa2a245e4e4f2434fc73
5
5
  SHA512:
6
- metadata.gz: 2616e0e816e014aba36151690e5d5546ace6adcd2e1ec08bef86191526bf594576359ed0b3eace7ce4380eb629c00d675cd0bb1aef3e1ab5eb6918a6b8e018ef
7
- data.tar.gz: b811fd9b51612ea91d71c75b0c87fedfe2146a10f78b4327953390fc7cd430dec07f9475e5d72b7aa5f00660b1309b81a859805fd37c7b8ead676f666e11c3f8
6
+ metadata.gz: 9c686b91c627deacdadd1102ea9742d4dea28a5842bec1536a58ffe448939c9d0bd3876a54e59dd9bb27c2cf7324140e20f060c3f63507980c6d1c8bade31367
7
+ data.tar.gz: 95cadea3c85273d9a0d5431508d7c3c1d98f2dfcd17b563fe06e22560f248f1f35b066d6f81eb55507d800aa9f7a4bf994197dee358adae25438b5daa7daf21e
@@ -0,0 +1,210 @@
1
+ # encoding: utf-8
2
+
3
+ module Hexx
4
+
5
+ # The Null object class.
6
+ # @see http://robots.thoughtbot.com/rails-refactoring-example-introduce-null-object
7
+ # The intro to null object pattern from Dan Croak.
8
+ # @see http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness/
9
+ # The post of Avdi Grimm on null object falsiness problem.
10
+ #
11
+ # Except for some special cases (see examples below) calling any method
12
+ # returns the +Null+ itself.
13
+ #
14
+ # @note (see Hexx::Null.!)
15
+ # @example (see Hexx::Null.method_missing)
16
+ # @example (see Hexx::Null.new)
17
+ # @example (see Hexx::Null.nil?)
18
+ # @example (see Hexx::Null.eq?)
19
+ # @example (see Hexx::Null.<=>)
20
+ # @example (see Hexx::Null.false?)
21
+ # @example (see Hexx::Null.true?)
22
+ # @example (see Hexx::Null.!)
23
+ # @example (see Hexx::Null.to_a)
24
+ # @example (see Hexx::Null.to_h)
25
+ # @example (see Hexx::Null.to_s)
26
+ # @example (see Hexx::Null.to_i)
27
+ # @example (see Hexx::Null.to_f)
28
+ # @example (see Hexx::Null.to_c)
29
+ # @example (see Hexx::Null.to_r)
30
+ # @example (see Hexx::Null.to_nil)
31
+ class Null
32
+ class << self
33
+ include Comparable
34
+
35
+ # The constructor constructs itself.
36
+ #
37
+ # @example The +Null+ object constructs itself.
38
+ # Null.new
39
+ # # => Null
40
+ #
41
+ # @return [Class] +Null+.
42
+ def new
43
+ self
44
+ end
45
+
46
+ # Checks if +Null+ is equal to other object.
47
+ #
48
+ # @example The +Null+ is equal to +nil+.
49
+ # Null.eq? nil # => true
50
+ # Null.eq? Null # => true
51
+ #
52
+ # @example The +Null+ differs from non-nil
53
+ # Null.eq? 0 # => false
54
+ # Null.eq? false # => false
55
+ #
56
+ # @param [Object] other The other object to compare to Null.
57
+ # @return [Boolean] The result of comparison.
58
+ def eq?(other)
59
+ other.nil?
60
+ end
61
+
62
+ # Compares the +Null+ to other object.
63
+ #
64
+ # @example The +Null+ is less than non-nil object:
65
+ # Null < 0 # => true
66
+ # Null < "" # => true
67
+ # Null < false # => true
68
+ #
69
+ # @param [Object] other The other object to compare to Null.
70
+ # @return [-1, 0] The result of comparison.
71
+ def <=>(other)
72
+ other.nil? ? 0 : -1
73
+ end
74
+
75
+ # Checks if the +Null+ is nil.
76
+ #
77
+ # @example The +Null+ is nil.
78
+ # Null.nil? # => true
79
+ #
80
+ # @return [Boolean] +true+.
81
+ def nil?
82
+ true
83
+ end
84
+
85
+ # Checks if the +Null+ is falseу.
86
+ #
87
+ # @example The +Null+ is falseу.
88
+ # Null.false? # => true
89
+ #
90
+ # @example The +Null+ isn't false in logical operations
91
+ # false || Null # => Null
92
+ #
93
+ # @return [Boolean] +true+.
94
+ def false?
95
+ true
96
+ end
97
+
98
+ # Converts nil to true.
99
+ #
100
+ # @note In Ruby the only objects that are +false+ are +nil+ and +false+.
101
+ # Use the double bang before any object that may be the +Null+.
102
+ #
103
+ # @example The +Null+ is convertible to +true+.
104
+ # !Null # => true
105
+ #
106
+ # @example Use a double bang before objects that may be the +Null+.
107
+ # Null && true # => true
108
+ # !!Null && true # => false
109
+ #
110
+ # @return [Boolean] +true+.
111
+ def !
112
+ true
113
+ end
114
+
115
+ # Checks if the +Null+ is truthy.
116
+ #
117
+ # @example The +Null+ isn't truthy.
118
+ # Null.true? # => false
119
+ #
120
+ # @example The +Null+ is truthy in logical operations.
121
+ # true && Null # => Null
122
+ #
123
+ # @return [Boolean] +false+.
124
+ def true?
125
+ false
126
+ end
127
+
128
+ # Converts the +Null+ to the empty string.
129
+ #
130
+ # @example The +Null+ is convertible to the empty string.
131
+ # Null.to_s # => ""
132
+ #
133
+ # @return [String] The empty string.
134
+ def to_s
135
+ ""
136
+ end
137
+
138
+ # Converts the +Null+ to +nil+.
139
+ #
140
+ # @example The +Null+ is convertible to +nil+.
141
+ # Null.to_nil # => nil
142
+ #
143
+ # @return [NilClass] +nil+.
144
+ def to_nil
145
+ nil
146
+ end
147
+
148
+ # @!method to_a
149
+ # Converts the +Null+ to the empty array.
150
+ # @example The +Null+ is convertible to the empty array.
151
+ # Null.to_a # => []
152
+ # @return [Array] the empty array.
153
+
154
+ # @!method to_i
155
+ # Converts the +Null+ to zero as an integer.
156
+ # @example The +Null+ is convertible to zero as an integer.
157
+ # Null.to_i # => 0
158
+ # @return [Integer] zero.
159
+
160
+ # @!method to_f
161
+ # Converts the +Null+ to zero as a float.
162
+ # @example The +Null+ is convertible to zero as a float.
163
+ # Null.to_f # => 0.0
164
+ # @return [Float] zero.
165
+
166
+ # @!method to_c
167
+ # Converts the +Null+ to zero as a complex.
168
+ # @example The +Null+ is convertible to zero as a complex.
169
+ # Null.to_c # => (0+0i)
170
+ # @return [Complex] zero.
171
+
172
+ # @!method to_r
173
+ # Converts the +Null+ to zero as a rational.
174
+ # @example The +Null+ is convertible to zero as a rational.
175
+ # Null.to_c # => (0/1)
176
+ # @return [Rational] zero.
177
+
178
+ # Checks if a method defined.
179
+ #
180
+ # @example The +Null+ responds to any object.
181
+ # Null.respond_to? arbitrary_method
182
+ # # => true
183
+ #
184
+ # @return [Boolean] +true+.
185
+ def respond_to?(*)
186
+ true
187
+ end
188
+
189
+ private
190
+
191
+ # @api hide
192
+ # @example An arbitrary method returns the +Null+ itself.
193
+ # Null.is_an_eagle_owl? # => Null
194
+ # Null.is_an_elk? # => Null
195
+ # Null.fly # => Null
196
+ # # ... etc.
197
+ #
198
+ # @example An arbitrary method called with a block yields the block.
199
+ # Null.when_fishing { p "I've got it!" }
200
+ # # => "I've got it!"
201
+ # # => Null
202
+ #
203
+ def method_missing(name, *args, &block)
204
+ return nil.send(name, *args, &block) if nil.respond_to?(name)
205
+ block.call if block_given?
206
+ self
207
+ end
208
+ end
209
+ end
210
+ end
@@ -2,5 +2,5 @@
2
2
  module Hexx
3
3
 
4
4
  # Current release.
5
- VERSION = "3.0.0"
5
+ VERSION = "3.1.0"
6
6
  end
@@ -0,0 +1,104 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ module Hexx
5
+ describe Null do
6
+
7
+ subject { Null }
8
+
9
+ describe ".new" do
10
+
11
+ it "constructs itself" do
12
+ expect(Null.new).to eq Null
13
+ end
14
+ end
15
+
16
+ describe "arbitrary method" do
17
+
18
+ it "is defined" do
19
+ expect(Null).to respond_to :drag_and_drop
20
+ end
21
+
22
+ it "yields a block" do
23
+ test = double :test, check: nil
24
+ expect(test).to receive :check
25
+ Null.when_fishing { test.check }
26
+ end
27
+
28
+ it "returns itself" do
29
+ expect(Null.is_an_eagle_owl?).to eq Null
30
+ end
31
+ end
32
+
33
+ it "equals nil" do
34
+ expect(Null).to eq nil
35
+ expect(Null).to be_nil
36
+ end
37
+
38
+ it "equals Null" do
39
+ expect(Null.eq? Null).to be_truthy
40
+ end
41
+
42
+ it "is less than any other object" do
43
+ expect(Null).to be < -1
44
+ expect(Null).to be < false
45
+ expect(Null).to be < true
46
+ expect(Null).to be < ""
47
+ end
48
+
49
+ it "is falsey" do
50
+ expect(Null).to be_falsey
51
+ expect(Null).not_to be_truthy
52
+ expect(!Null).to eq true
53
+ end
54
+
55
+ describe "#to_a" do
56
+
57
+ it "returns the empty array" do
58
+ expect(Null.to_a).to eq []
59
+ end
60
+ end
61
+
62
+ describe "#to_h" do
63
+
64
+ it "returns the empty hash" do
65
+ expect(Null.to_h).to eq({})
66
+ end
67
+ end
68
+
69
+ describe "#to_s" do
70
+
71
+ it "returns the empty string" do
72
+ expect(Null.to_s).to eq ""
73
+ end
74
+ end
75
+
76
+ describe "#to_i" do
77
+
78
+ it "returns zero as an integer" do
79
+ expect(Null.to_i).to eq 0
80
+ end
81
+ end
82
+
83
+ describe "#to_f" do
84
+
85
+ it "returns zero as a float" do
86
+ expect(Null.to_f).to eq 0.0
87
+ end
88
+ end
89
+
90
+ describe "#to_c" do
91
+
92
+ it "returns zero as a complex" do
93
+ expect(Null.to_c).to eq 0.to_c
94
+ end
95
+ end
96
+
97
+ describe "#to_r" do
98
+
99
+ it "returns zero as a rational" do
100
+ expect(Null.to_r).to eq 0.to_r
101
+ end
102
+ end
103
+ end
104
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hexx
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kozin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-01 00:00:00.000000000 Z
11
+ date: 2014-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -151,6 +151,7 @@ files:
151
151
  - lib/hexx.rb
152
152
  - lib/hexx/models.rb
153
153
  - lib/hexx/models/base_coercer.rb
154
+ - lib/hexx/null.rb
154
155
  - lib/hexx/service.rb
155
156
  - lib/hexx/service/invalid.rb
156
157
  - lib/hexx/service/message.rb
@@ -158,6 +159,7 @@ files:
158
159
  - lib/hexx/service/with_callbacks.rb
159
160
  - lib/hexx/version.rb
160
161
  - spec/hexx/models_spec.rb
162
+ - spec/hexx/null_spec.rb
161
163
  - spec/hexx/service/invalid_spec.rb
162
164
  - spec/hexx/service/message_spec.rb
163
165
  - spec/hexx/service_spec.rb
@@ -194,6 +196,7 @@ summary: Service objects for Rails.
194
196
  test_files:
195
197
  - spec/spec_helper.rb
196
198
  - spec/hexx/service_spec.rb
199
+ - spec/hexx/null_spec.rb
197
200
  - spec/hexx/service/message_spec.rb
198
201
  - spec/hexx/service/invalid_spec.rb
199
202
  - spec/hexx/models_spec.rb