ohm-contrib 0.0.17 → 0.0.18

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.17
1
+ 0.0.18
@@ -66,8 +66,8 @@ module Ohm
66
66
  #
67
67
  # @param [Symbol] method the method type, `:validate`, `:create`, or `:save`
68
68
  # @param [Symbol] callback the name of the method to execute
69
- # @return [Array<callback>, nil] the callback wrapped in an array or nil
70
- # if the callback exists
69
+ # @return [Array] the callback in an array if added e.g. [:timestamp]
70
+ # @return [nil] if the callback already exists
71
71
  def before(method, callback)
72
72
  unless callbacks[:before][method].include? callback
73
73
  callbacks[:before][method] << callback
@@ -97,8 +97,8 @@ module Ohm
97
97
  #
98
98
  # @param [Symbol] method the method type, `:validate`, `:create`, or `:save`
99
99
  # @param [Symbol] callback the name of the method to execute
100
- # @return [Array<callback>, nil] the callback in an array or nil if the
101
- # callback exists
100
+ # @return [Array] the callback in an array if added e.g. [:timestamp]
101
+ # @return [nil] if the callback already exists
102
102
  def after(method, callback)
103
103
  unless callbacks[:after][method].include? callback
104
104
  callbacks[:after][method] << callback
@@ -1,7 +1,6 @@
1
1
  require 'bigdecimal'
2
2
  require 'time'
3
3
  require 'date'
4
- require 'forwardable'
5
4
 
6
5
  module Ohm
7
6
  # Provides all the primitive types. The following are included:
@@ -13,6 +12,15 @@ module Ohm
13
12
  # * Date
14
13
  # * Time
15
14
  module Types
15
+ def self.defined?(type)
16
+ @constants ||= constants.map(&:to_s)
17
+ @constants.include?(type.to_s)
18
+ end
19
+
20
+ def self.[](type)
21
+ const_get(type.to_s.split('::').last)
22
+ end
23
+
16
24
  class Primitive < BasicObject
17
25
  def initialize(value)
18
26
  @raw = value
@@ -69,15 +77,6 @@ module Ohm
69
77
  end
70
78
 
71
79
  class Time < Primitive
72
- class << self
73
- extend Forwardable
74
- def_delegators :Time,
75
- :_load, :apply_offset, :at, :gm, :httpdate, :json_create, :local,
76
- :make_time, :mktime, :month_days, :now, :parse, :rfc2822,
77
- :strptime, :utc, :w3cdtf, :xmlschema, :yaml_new, :zone_offset,
78
- :zone_utc?
79
- end
80
-
81
80
  protected
82
81
  def object
83
82
  ::Time.parse(@raw)
@@ -85,15 +84,6 @@ module Ohm
85
84
  end
86
85
 
87
86
  class Date < Primitive
88
- class << self
89
- extend Forwardable
90
- def_delegators :Date,
91
- :_parse, :_strptime, :civil, :commercial, :gregorian_leap?, :jd,
92
- :json_create, :julian_leap?, :now, :nth_kday, :ordinal, :parse, :s3e,
93
- :strptime, :today, :valid_civil?, :valid_commercial?, :valid_jd?,
94
- :valid_ordinal?, :weeknum
95
- end
96
-
97
87
  protected
98
88
  def object
99
89
  ::Date.parse(@raw)
@@ -153,17 +143,49 @@ module Ohm
153
143
  # item.posted.strftime('%m/%d/%Y')
154
144
  # # => works!!!
155
145
  module Typecast
156
- include Types
157
-
158
146
  def self.included(base)
159
147
  base.extend ClassMethods
160
148
  end
161
149
 
162
150
  module ClassMethods
163
- def attribute(name, type = Ohm::Types::String)
151
+ # Defines a typecasted attribute.
152
+ #
153
+ # @example
154
+ #
155
+ # class User < Ohm::Model
156
+ # include Ohm::Typecast
157
+ #
158
+ # attribute :birthday, Date
159
+ # attribute :last_login, Time
160
+ # attribute :age, Integer
161
+ # attribute :spending, Decimal
162
+ # attribute :score, Float
163
+ # end
164
+ #
165
+ # user = User.new(:birthday => "2000-01-01")
166
+ # user.birthday.month == 1
167
+ # # => true
168
+ #
169
+ # user.birthday.year == 2000
170
+ # # => true
171
+ #
172
+ # user.birthday.day == 1
173
+ # # => true
174
+ #
175
+ # user = User.new(:age => 20)
176
+ # user.age - 1 == 19
177
+ # => true
178
+ #
179
+ # @param [Symbol] name the name of the attribute to define.
180
+ # @param [Class] type (defaults to Ohm::Types::String) a class defined in
181
+ # Ohm::Types. You may define custom types in Ohm::Types if
182
+ # you need to.
183
+ # @return [Array] the array of attributes already defined.
184
+ # @return [nil] if the attribute is already defined.
185
+ def attribute(name, type = Ohm::Types::String, klass = Ohm::Types[type])
164
186
  define_method(name) do
165
187
  value = read_local(name)
166
- value && type.new(value)
188
+ value && klass.new(value)
167
189
  end
168
190
 
169
191
  define_method(:"#{name}=") do |value|
@@ -172,6 +194,15 @@ module Ohm
172
194
 
173
195
  attributes << name unless attributes.include?(name)
174
196
  end
197
+
198
+ private
199
+ def const_missing(name)
200
+ if Ohm::Types.defined?(name)
201
+ Ohm::Types[name]
202
+ else
203
+ super
204
+ end
205
+ end
175
206
  end
176
207
  end
177
- end
208
+ end
data/lib/ohm/contrib.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Ohm
2
2
  module Contrib
3
- VERSION = '0.0.17'
3
+ VERSION = '0.0.18'
4
4
  end
5
5
 
6
6
  autoload :Boundaries, "ohm/contrib/boundaries"
data/ohm-contrib.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ohm-contrib}
8
- s.version = "0.0.17"
8
+ s.version = "0.0.18"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Cyril David"]
12
- s.date = %q{2010-05-25}
12
+ s.date = %q{2010-05-26}
13
13
  s.description = %q{Highly decoupled drop-in functionality for Ohm models}
14
14
  s.email = %q{cyx.ucron@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -205,28 +205,17 @@ class TestOhmTypecast < Test::Unit::TestCase
205
205
  Time.now
206
206
  end
207
207
 
208
- def time
209
- Time
208
+ def new_time
209
+ Time.new
210
210
  end
211
211
  end
212
212
 
213
213
  test "still able to access top level Time" do
214
- post = Post.new
215
- assert_equal post.now.to_s, Time.now.to_s
214
+ assert_equal Post.new.now.to_s, Time.now.to_s
216
215
  end
217
216
 
218
- test "responds to all ::Time class methods" do
219
- post = Post.new
220
- methods = [
221
- :_load, :apply_offset, :at, :gm, :httpdate, :json_create, :local,
222
- :make_time, :mktime, :month_days, :new, :now, :parse, :rfc2822,
223
- :strptime, :utc, :w3cdtf, :xmlschema, :yaml_new, :zone_offset,
224
- :zone_utc?
225
- ]
226
-
227
- methods.each do |m|
228
- assert_respond_to post.time, m
229
- end
217
+ test "should be able to use Time.new inside the class" do
218
+ assert_equal Post.new.new_time.to_s, Time.new.to_s
230
219
  end
231
220
 
232
221
  test "handles nil case correctly" do
@@ -277,6 +266,10 @@ class TestOhmTypecast < Test::Unit::TestCase
277
266
  Date
278
267
  end
279
268
 
269
+ def may_5
270
+ Date.new(2010, 05, 05)
271
+ end
272
+
280
273
  def base_today
281
274
  Date.today
282
275
  end
@@ -286,18 +279,8 @@ class TestOhmTypecast < Test::Unit::TestCase
286
279
  assert_equal Date.today, Post.new.base_today
287
280
  end
288
281
 
289
- test "responds to all the class methods" do
290
- post = Post.new
291
-
292
- methods = [
293
- :_parse, :_strptime, :civil, :commercial, :gregorian_leap?, :jd,
294
- :json_create, :julian_leap?, :now, :nth_kday, :ordinal, :parse, :s3e,
295
- :strptime, :today, :valid_civil?, :valid_commercial?, :valid_jd?,
296
- :valid_ordinal?, :weeknum
297
- ]
298
- methods.each do |m|
299
- assert_respond_to post.date, m
300
- end
282
+ test "allows instantiation of dates" do
283
+ assert_equal Date.new(2010, 05, 05), Post.new.may_5
301
284
  end
302
285
 
303
286
  test "handles nil case correctly" do
@@ -335,4 +318,4 @@ class TestOhmTypecast < Test::Unit::TestCase
335
318
  assert_equal Date.today, Post.new.today
336
319
  end
337
320
  end
338
- end
321
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 17
9
- version: 0.0.17
8
+ - 18
9
+ version: 0.0.18
10
10
  platform: ruby
11
11
  authors:
12
12
  - Cyril David
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-25 00:00:00 +08:00
17
+ date: 2010-05-26 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency