ohm-contrib 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ohm/datatypes.rb +13 -4
- data/ohm-contrib.gemspec +1 -1
- data/test/plugin.rb +171 -109
- metadata +2 -2
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|
|
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 &&
|
17
|
-
Array = lambda { |a| a && (a.kind_of?(::Array) ?
|
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.
|
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
|
-
|
49
|
-
|
48
|
+
# slugging
|
49
|
+
scope do
|
50
|
+
class Post < Ohm::Model
|
51
|
+
attribute :title
|
50
52
|
|
51
|
-
|
53
|
+
include Ohm::Slug
|
52
54
|
|
53
|
-
|
54
|
-
|
55
|
+
def to_s
|
56
|
+
title
|
57
|
+
end
|
55
58
|
end
|
56
|
-
end
|
57
59
|
|
58
|
-
test "slugging" do
|
59
|
-
|
60
|
-
|
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
|
-
|
63
|
-
|
64
|
+
post = Post.create(:title => "Décor")
|
65
|
+
assert_equal "2-decor", post.to_param
|
66
|
+
end
|
64
67
|
end
|
65
68
|
|
66
|
-
|
67
|
-
|
69
|
+
# Ohm::Scope
|
70
|
+
scope do
|
71
|
+
class Order < Ohm::Model
|
72
|
+
include Ohm::Scope
|
68
73
|
|
69
|
-
|
70
|
-
|
74
|
+
attribute :state
|
75
|
+
index :state
|
71
76
|
|
72
|
-
|
73
|
-
|
77
|
+
attribute :deleted
|
78
|
+
index :deleted
|
74
79
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
80
|
+
scope do
|
81
|
+
def paid
|
82
|
+
find(:state => "paid")
|
83
|
+
end
|
79
84
|
|
80
|
-
|
81
|
-
|
85
|
+
def deleted
|
86
|
+
find(:deleted => 1)
|
87
|
+
end
|
82
88
|
end
|
83
89
|
end
|
84
|
-
end
|
85
90
|
|
86
|
-
test "
|
87
|
-
|
91
|
+
test "scoping" do
|
92
|
+
paid = Order.create(:state => "paid", :deleted => nil)
|
88
93
|
|
89
|
-
|
90
|
-
|
94
|
+
assert Order.all.paid.include?(paid)
|
95
|
+
assert_equal 0, Order.all.paid.deleted.size
|
91
96
|
|
92
|
-
|
93
|
-
|
97
|
+
paid.update(:deleted => 1)
|
98
|
+
assert Order.all.paid.deleted.include?(paid)
|
99
|
+
end
|
94
100
|
end
|
95
101
|
|
96
|
-
|
97
|
-
|
102
|
+
# soft delete
|
103
|
+
scope do
|
104
|
+
class User < Ohm::Model
|
105
|
+
include Ohm::SoftDelete
|
98
106
|
|
99
|
-
|
100
|
-
|
101
|
-
end
|
107
|
+
attribute :email
|
108
|
+
index :email
|
109
|
+
end
|
102
110
|
|
103
|
-
|
104
|
-
|
105
|
-
|
111
|
+
setup do
|
112
|
+
user = User.create(:email => "a@a.com")
|
113
|
+
user.delete
|
106
114
|
|
107
|
-
|
108
|
-
|
109
|
-
|
115
|
+
user
|
116
|
+
end
|
117
|
+
|
118
|
+
test "removes from User.all" do |user|
|
119
|
+
assert User.all.empty?
|
120
|
+
end
|
110
121
|
|
111
|
-
|
112
|
-
|
122
|
+
test "adds to User.deleted" do |user|
|
123
|
+
assert User.deleted.include?(user)
|
124
|
+
end
|
113
125
|
|
114
|
-
user
|
126
|
+
test "doesn't remove from indices" do |user|
|
127
|
+
assert User.find(:email => "a@a.com").include?(user)
|
128
|
+
end
|
115
129
|
|
116
|
-
|
117
|
-
|
130
|
+
test "makes it deleted?" do |user|
|
131
|
+
assert user.deleted?
|
132
|
+
end
|
118
133
|
|
119
|
-
|
120
|
-
|
134
|
+
test "is still retrievable" do |user|
|
135
|
+
assert_equal user, User[user.id]
|
136
|
+
end
|
121
137
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
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
|
-
|
137
|
-
|
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
|
-
|
140
|
-
p.save
|
179
|
+
p.save
|
141
180
|
|
142
|
-
|
143
|
-
|
181
|
+
p = Product[p.id]
|
182
|
+
assert p.bought_at.kind_of?(Time)
|
183
|
+
assert_equal time, p.bought_at
|
144
184
|
|
145
|
-
|
146
|
-
|
147
|
-
|
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
|
-
|
189
|
+
test "Type::Date" do
|
190
|
+
p = Product.new(:date_released => Date.today)
|
150
191
|
|
151
|
-
|
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
|
-
|
194
|
+
p = Product.new(:date_released => "2011-11-22")
|
195
|
+
assert p.date_released.kind_of?(Date)
|
156
196
|
|
157
|
-
|
197
|
+
p.save
|
158
198
|
|
159
|
-
|
160
|
-
|
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
|
-
|
205
|
+
test "Type::Hash" do
|
206
|
+
sizes = { "XS" => 1, "S" => 2, "L" => 3 }
|
163
207
|
|
164
|
-
|
165
|
-
|
208
|
+
p = Product.new(:sizes => sizes)
|
209
|
+
assert p.sizes.kind_of?(Hash)
|
210
|
+
p.save
|
166
211
|
|
167
|
-
|
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
|
-
|
170
|
-
|
171
|
-
|
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
|
-
|
174
|
-
assert_equal sizes, p.sizes
|
223
|
+
p.save
|
175
224
|
|
176
|
-
|
177
|
-
|
178
|
-
|
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
|
-
|
231
|
+
test "Type::Decimal" do
|
232
|
+
p = Product.new(:price => 0.001)
|
181
233
|
|
182
|
-
|
183
|
-
|
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
|
-
|
239
|
+
p.save
|
186
240
|
|
187
|
-
|
188
|
-
|
189
|
-
|
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
|
-
|
246
|
+
test "Type::Float" do
|
247
|
+
p = Product.new(:rating => 4.5)
|
248
|
+
assert p.rating.kind_of?(Float)
|
192
249
|
|
193
|
-
|
194
|
-
|
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
|
-
|
197
|
-
|
256
|
+
test "Type::Boolean" do
|
257
|
+
p = Product.new(:published => 1)
|
258
|
+
assert_equal true, p.published
|
198
259
|
|
199
|
-
|
200
|
-
p = Product[p.id]
|
201
|
-
assert_equal 4.5, p.rating
|
260
|
+
p.save
|
202
261
|
|
203
|
-
|
204
|
-
|
262
|
+
p = Product[p.id]
|
263
|
+
assert_equal true, p.published
|
205
264
|
|
206
|
-
|
265
|
+
p.published = false
|
266
|
+
p.save
|
207
267
|
|
208
|
-
|
209
|
-
|
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.
|
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-
|
12
|
+
date: 2012-05-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ohm
|