ohm-contrib 1.0.0 → 1.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.
data/lib/ohm/datatypes.rb CHANGED
@@ -9,12 +9,21 @@ module Ohm
9
9
  Integer = lambda { |x| x.to_i }
10
10
  Decimal = lambda { |x| BigDecimal(x.to_s) }
11
11
  Float = lambda { |x| x.to_f }
12
- Boolean = lambda { |x| !!x }
12
+ Boolean = lambda { |x| Ohm::DataTypes.bool(x) }
13
13
  Time = lambda { |t| t && (t.kind_of?(::Time) ? t : ::Time.parse(t)) }
14
14
  Date = lambda { |d| d && (d.kind_of?(::Date) ? d : ::Date.parse(d)) }
15
15
  Timestamp = lambda { |t| t && UnixTime.at(t.to_i) }
16
- Hash = lambda { |h| h && (h.kind_of?(::Hash) ? SerializedHash[h] : JSON(h)) }
17
- Array = lambda { |a| a && (a.kind_of?(::Array) ? SerializedArray.new(a) : JSON(a)) }
16
+ Hash = lambda { |h| h && SerializedHash[h.kind_of?(::Hash) ? h : JSON(h)] }
17
+ Array = lambda { |a| a && SerializedArray.new(a.kind_of?(::Array) ? a : JSON(a)) }
18
+ end
19
+
20
+ def self.bool(val)
21
+ case val
22
+ when "false", "0" then false
23
+ when "true", "1" then true
24
+ else
25
+ !! val
26
+ end
18
27
  end
19
28
 
20
29
  class UnixTime < Time
@@ -35,4 +44,4 @@ module Ohm
35
44
  end
36
45
  end
37
46
  end
38
- end
47
+ end
data/ohm-contrib.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "ohm-contrib"
3
- s.version = "1.0.0"
3
+ s.version = "1.0.1"
4
4
  s.summary = %{A collection of decoupled drop-in modules for Ohm.}
5
5
  s.description = %{Includes a couple of core functions such as callbacks, timestamping, typecasting and lots of generic validation routines.}
6
6
  s.author = "Cyril David"
data/test/plugin.rb CHANGED
@@ -45,166 +45,228 @@ test "class-level, instance level callbacks" do
45
45
  assert_equal 1, a.comments.size
46
46
  end
47
47
 
48
- class Post < Ohm::Model
49
- attribute :title
48
+ # slugging
49
+ scope do
50
+ class Post < Ohm::Model
51
+ attribute :title
50
52
 
51
- include Ohm::Slug
53
+ include Ohm::Slug
52
54
 
53
- def to_s
54
- title
55
+ def to_s
56
+ title
57
+ end
55
58
  end
56
- end
57
59
 
58
- test "slugging" do
59
- post = Post.create(:title => "Foo Bar Baz")
60
- assert_equal "1-foo-bar-baz", post.to_param
60
+ test "slugging" do
61
+ post = Post.create(:title => "Foo Bar Baz")
62
+ assert_equal "1-foo-bar-baz", post.to_param
61
63
 
62
- post = Post.create(:title => "Décor")
63
- assert_equal "2-decor", post.to_param
64
+ post = Post.create(:title => "Décor")
65
+ assert_equal "2-decor", post.to_param
66
+ end
64
67
  end
65
68
 
66
- class Order < Ohm::Model
67
- include Ohm::Scope
69
+ # Ohm::Scope
70
+ scope do
71
+ class Order < Ohm::Model
72
+ include Ohm::Scope
68
73
 
69
- attribute :state
70
- index :state
74
+ attribute :state
75
+ index :state
71
76
 
72
- attribute :deleted
73
- index :deleted
77
+ attribute :deleted
78
+ index :deleted
74
79
 
75
- scope do
76
- def paid
77
- find(:state => "paid")
78
- end
80
+ scope do
81
+ def paid
82
+ find(:state => "paid")
83
+ end
79
84
 
80
- def deleted
81
- find(:deleted => 1)
85
+ def deleted
86
+ find(:deleted => 1)
87
+ end
82
88
  end
83
89
  end
84
- end
85
90
 
86
- test "scope" do
87
- paid = Order.create(:state => "paid", :deleted => nil)
91
+ test "scoping" do
92
+ paid = Order.create(:state => "paid", :deleted => nil)
88
93
 
89
- assert Order.all.paid.include?(paid)
90
- assert_equal 0, Order.all.paid.deleted.size
94
+ assert Order.all.paid.include?(paid)
95
+ assert_equal 0, Order.all.paid.deleted.size
91
96
 
92
- paid.update(:deleted => 1)
93
- assert Order.all.paid.deleted.include?(paid)
97
+ paid.update(:deleted => 1)
98
+ assert Order.all.paid.deleted.include?(paid)
99
+ end
94
100
  end
95
101
 
96
- class User < Ohm::Model
97
- include Ohm::SoftDelete
102
+ # soft delete
103
+ scope do
104
+ class User < Ohm::Model
105
+ include Ohm::SoftDelete
98
106
 
99
- attribute :email
100
- index :email
101
- end
107
+ attribute :email
108
+ index :email
109
+ end
102
110
 
103
- test "soft delete" do
104
- user = User.create(:email => "a@a.com")
105
- user.delete
111
+ setup do
112
+ user = User.create(:email => "a@a.com")
113
+ user.delete
106
114
 
107
- assert User.all.empty?
108
- assert User.deleted.include?(user)
109
- assert User.find(:email => "a@a.com").include?(user)
115
+ user
116
+ end
117
+
118
+ test "removes from User.all" do |user|
119
+ assert User.all.empty?
120
+ end
110
121
 
111
- assert user.deleted?
112
- assert User[user.id] == user
122
+ test "adds to User.deleted" do |user|
123
+ assert User.deleted.include?(user)
124
+ end
113
125
 
114
- user.restore
126
+ test "doesn't remove from indices" do |user|
127
+ assert User.find(:email => "a@a.com").include?(user)
128
+ end
115
129
 
116
- assert User.all.include?(user)
117
- assert User.deleted.empty?
130
+ test "makes it deleted?" do |user|
131
+ assert user.deleted?
132
+ end
118
133
 
119
- assert ! user.deleted?
120
- end
134
+ test "is still retrievable" do |user|
135
+ assert_equal user, User[user.id]
136
+ end
121
137
 
122
- class Product < Ohm::Model
123
- include Ohm::DataTypes
124
-
125
- attribute :name
126
- attribute :stock, Type::Integer
127
- attribute :price, Type::Decimal
128
- attribute :rating, Type::Float
129
- attribute :bought_at, Type::Time
130
- attribute :date_released, Type::Date
131
- attribute :sizes, Type::Hash
132
- attribute :stores, Type::Array
133
- attribute :published, Type::Boolean
138
+ test "restore" do |user|
139
+ user.restore
140
+
141
+ assert User.all.include?(user)
142
+ assert User.deleted.empty?
143
+
144
+ assert ! user.deleted?
145
+ end
134
146
  end
135
147
 
136
- test "datatypes" do
137
- p = Product.new(:stock => "1")
148
+ # datatypes
149
+ scope do
150
+ class Product < Ohm::Model
151
+ include Ohm::DataTypes
152
+
153
+ attribute :name
154
+ attribute :stock, Type::Integer
155
+ attribute :price, Type::Decimal
156
+ attribute :rating, Type::Float
157
+ attribute :bought_at, Type::Time
158
+ attribute :date_released, Type::Date
159
+ attribute :sizes, Type::Hash
160
+ attribute :stores, Type::Array
161
+ attribute :published, Type::Boolean
162
+ end
163
+
164
+ test "Type::Integer" do
165
+ p = Product.new(:stock => "1")
166
+
167
+ assert_equal 1, p.stock
168
+ p.save
169
+
170
+ p = Product[p.id]
171
+ assert_equal 1, p.stock
172
+ end
173
+
174
+ test "Type::Time" do
175
+ time = Time.utc(2011, 11, 22)
176
+ p = Product.new(:bought_at => time)
177
+ assert p.bought_at.kind_of?(Time)
138
178
 
139
- assert_equal 1, p.stock
140
- p.save
179
+ p.save
141
180
 
142
- p = Product[p.id]
143
- assert_equal 1, p.stock
181
+ p = Product[p.id]
182
+ assert p.bought_at.kind_of?(Time)
183
+ assert_equal time, p.bought_at
144
184
 
145
- time = Time.now.utc
146
- p = Product.new(:bought_at => time)
147
- assert p.bought_at.kind_of?(Time)
185
+ assert_equal "2011-11-22 00:00:00 UTC", p.key.hget(:bought_at)
186
+ assert_equal "2011-11-22 00:00:00 UTC", p.bought_at.to_s
187
+ end
148
188
 
149
- p.save
189
+ test "Type::Date" do
190
+ p = Product.new(:date_released => Date.today)
150
191
 
151
- p = Product[p.id]
152
- assert p.bought_at.kind_of?(Time)
153
- assert_equal time, p.bought_at
192
+ assert p.date_released.kind_of?(Date)
154
193
 
155
- p = Product.new(:date_released => Date.today)
194
+ p = Product.new(:date_released => "2011-11-22")
195
+ assert p.date_released.kind_of?(Date)
156
196
 
157
- assert p.date_released.kind_of?(Date)
197
+ p.save
158
198
 
159
- p = Product.new(:date_released => "2011-11-22")
160
- assert p.date_released.kind_of?(Date)
199
+ p = Product[p.id]
200
+ assert_equal Date.new(2011, 11, 22), p.date_released
201
+ assert_equal "2011-11-22", p.key.hget(:date_released)
202
+ assert_equal "2011-11-22", p.date_released.to_s
203
+ end
161
204
 
162
- p.save
205
+ test "Type::Hash" do
206
+ sizes = { "XS" => 1, "S" => 2, "L" => 3 }
163
207
 
164
- p = Product[p.id]
165
- assert_equal Date.new(2011, 11, 22), p.date_released
208
+ p = Product.new(:sizes => sizes)
209
+ assert p.sizes.kind_of?(Hash)
210
+ p.save
166
211
 
167
- sizes = { "XS" => 1, "S" => 2, "L" => 3 }
212
+ p = Product[p.id]
213
+ assert_equal sizes, p.sizes
214
+ assert_equal %Q[{"XS":1,"S":2,"L":3}], p.key.hget(:sizes)
215
+ assert_equal %Q[{"XS":1,"S":2,"L":3}], p.sizes.to_s
216
+ end
168
217
 
169
- p = Product.new(:sizes => sizes)
170
- assert p.sizes.kind_of?(Hash)
171
- p.save
218
+ test "Type::Array" do
219
+ stores = ["walmart", "marshalls", "jcpenny"]
220
+ p = Product.new(:stores => stores)
221
+ assert p.stores.kind_of?(Array)
172
222
 
173
- p = Product[p.id]
174
- assert_equal sizes, p.sizes
223
+ p.save
175
224
 
176
- stores = ["walmart", "marshalls", "jcpenny"]
177
- p = Product.new(:stores => stores)
178
- assert p.stores.kind_of?(Array)
225
+ p = Product[p.id]
226
+ assert_equal stores, p.stores
227
+ assert_equal %Q(["walmart","marshalls","jcpenny"]), p.key.hget(:stores)
228
+ assert_equal %Q(["walmart","marshalls","jcpenny"]), p.stores.to_s
229
+ end
179
230
 
180
- p.save
231
+ test "Type::Decimal" do
232
+ p = Product.new(:price => 0.001)
181
233
 
182
- p = Product[p.id]
183
- assert_equal stores, p.stores
234
+ # This fails if 0.001 is a float.
235
+ x = 0
236
+ 1000.times { x += p.price }
237
+ assert_equal 1, x
184
238
 
185
- p = Product.new(:price => 0.001)
239
+ p.save
186
240
 
187
- x = 0
188
- 1000.times { x += p.price }
189
- assert_equal 1, x
241
+ p = Product[p.id]
242
+ assert_equal 0.001, p.price
243
+ assert_equal "0.1E-2", p.key.hget(:price)
244
+ end
190
245
 
191
- p.save
246
+ test "Type::Float" do
247
+ p = Product.new(:rating => 4.5)
248
+ assert p.rating.kind_of?(Float)
192
249
 
193
- p = Product[p.id]
194
- assert_equal 0.001, p.price
250
+ p.save
251
+ p = Product[p.id]
252
+ assert_equal 4.5, p.rating
253
+ assert_equal "4.5", p.key.hget(:rating)
254
+ end
195
255
 
196
- p = Product.new(:rating => 4.5)
197
- assert p.rating.kind_of?(Float)
256
+ test "Type::Boolean" do
257
+ p = Product.new(:published => 1)
258
+ assert_equal true, p.published
198
259
 
199
- p.save
200
- p = Product[p.id]
201
- assert_equal 4.5, p.rating
260
+ p.save
202
261
 
203
- p = Product.new(:published => 1)
204
- assert_equal true, p.published
262
+ p = Product[p.id]
263
+ assert_equal true, p.published
205
264
 
206
- p.save
265
+ p.published = false
266
+ p.save
207
267
 
208
- p = Product[p.id]
209
- assert_equal true, p.published
268
+ p = Product[p.id]
269
+ assert_equal "false", p.key.hget(:published)
270
+ assert_equal false, p.published
271
+ end
210
272
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm-contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-25 00:00:00.000000000 Z
12
+ date: 2012-05-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ohm