ohm-contrib 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +12 -8
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/ohm/contrib.rb +1 -1
- data/lib/ohm/contrib/boundaries.rb +18 -2
- data/lib/ohm/contrib/callbacks.rb +1 -1
- data/lib/ohm/contrib/extra_validations.rb +30 -0
- data/lib/ohm/contrib/locking.rb +4 -2
- data/lib/ohm/contrib/number_validations.rb +4 -1
- data/lib/ohm/contrib/timestamping.rb +17 -1
- data/lib/ohm/contrib/to_hash.rb +27 -1
- data/lib/ohm/contrib/typecast.rb +61 -2
- data/lib/ohm/contrib/web_validations.rb +1 -1
- data/ohm-contrib.gemspec +2 -2
- metadata +3 -3
data/README.markdown
CHANGED
@@ -1,16 +1,20 @@
|
|
1
1
|
ohm-contrib
|
2
2
|
===========
|
3
3
|
|
4
|
-
A collection of drop-in modules for Ohm.
|
4
|
+
A collection of drop-in modules for Ohm. Read the full documentation at
|
5
|
+
[http://labs.sinefunc.com/ohm-contrib](http://labs.sinefunc.com/ohm-contrib).
|
5
6
|
|
6
7
|
List of modules
|
7
8
|
---------------
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
1. [`Ohm::Boundaries`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/Boundaries.html)
|
10
|
+
2. [`Ohm::Callbacks`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/Callbacks.html)
|
11
|
+
3. [`Ohm::Timestamping`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/Timestamping.html)
|
12
|
+
4. [`Ohm::ToHash`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/ToHash.html)
|
13
|
+
5. [`Ohm::WebValidations`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/WebValidations.html)
|
14
|
+
6. [`Ohm::NumberValidations`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/NumberValidations.html)
|
15
|
+
7. [`Ohm::ExtraValidations`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/ExtraValidations.html)
|
16
|
+
8. [`Ohm::Typecast`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/Typecast.html)
|
17
|
+
9. [`Ohm::Locking`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/Locking.html)
|
14
18
|
|
15
19
|
Example usage
|
16
20
|
-------------
|
@@ -102,4 +106,4 @@ Note on Patches/Pull Requests
|
|
102
106
|
|
103
107
|
Copyright
|
104
108
|
---------
|
105
|
-
Copyright (c) 2010 Cyril David. See LICENSE for details.
|
109
|
+
Copyright (c) 2010 Cyril David. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -8,7 +8,7 @@ begin
|
|
8
8
|
gem.summary = %Q{A collection of ohm related modules}
|
9
9
|
gem.description = %Q{Highly decoupled drop-in functionality for Ohm models}
|
10
10
|
gem.email = "cyx.ucron@gmail.com"
|
11
|
-
gem.homepage = "http://
|
11
|
+
gem.homepage = "http://labs.sinefunc.com/ohm-contrib"
|
12
12
|
gem.authors = ["Cyril David"]
|
13
13
|
gem.add_development_dependency "contest", ">= 0"
|
14
14
|
gem.add_development_dependency "redis", ">= 0"
|
@@ -53,4 +53,4 @@ Rake::RDocTask.new do |rdoc|
|
|
53
53
|
rdoc.title = "ohm-contrib #{version}"
|
54
54
|
rdoc.rdoc_files.include('README*')
|
55
55
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
-
end
|
56
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.15
|
data/lib/ohm/contrib.rb
CHANGED
@@ -1,4 +1,19 @@
|
|
1
1
|
module Ohm
|
2
|
+
# Dirt cheap ::first and ::last support.
|
3
|
+
#
|
4
|
+
# @example
|
5
|
+
#
|
6
|
+
# class Post < Ohm::Model
|
7
|
+
# include Ohm::Boundaries
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# post1 = Post.create
|
11
|
+
# post2 = Post.create
|
12
|
+
# post1 == Post.first
|
13
|
+
# # => true
|
14
|
+
#
|
15
|
+
# post2 == Post.last
|
16
|
+
# # => true
|
2
17
|
module Boundaries
|
3
18
|
def self.included(base)
|
4
19
|
base.extend ClassMethods
|
@@ -8,10 +23,11 @@ module Ohm
|
|
8
23
|
def first
|
9
24
|
all.first
|
10
25
|
end
|
11
|
-
|
26
|
+
|
27
|
+
# @todo Add support for passing in conditions
|
12
28
|
def last
|
13
29
|
self[db.get(key(:id))]
|
14
30
|
end
|
15
31
|
end
|
16
32
|
end
|
17
|
-
end
|
33
|
+
end
|
@@ -1,4 +1,34 @@
|
|
1
1
|
module Ohm
|
2
|
+
# Includes Ohm::NumberValidations and Ohm::WebValidations.
|
3
|
+
#
|
4
|
+
# @example
|
5
|
+
#
|
6
|
+
# class Post < Ohm::Model
|
7
|
+
# include Ohm::ExtraValidations
|
8
|
+
#
|
9
|
+
# attribute :price
|
10
|
+
# attribute :state
|
11
|
+
# attribute :slug
|
12
|
+
# attribute :author_email
|
13
|
+
# attribute :url
|
14
|
+
# attribute :ipaddr
|
15
|
+
#
|
16
|
+
# def validate
|
17
|
+
# super
|
18
|
+
#
|
19
|
+
# assert_decimal :price
|
20
|
+
# assert_member :state, ['published', 'unpublished']
|
21
|
+
# assert_ipaddr :ipaddr
|
22
|
+
# assert_url :url
|
23
|
+
# assert_email :author_email
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# post = Post.new
|
28
|
+
# post.valid?
|
29
|
+
# post.errors
|
30
|
+
# # [[:price, :not_decimal], [:state, :not_member], [:ipaddr, :not_ipaddr],
|
31
|
+
# # [:url, :not_url], [:author_email, :not_email]]
|
2
32
|
module ExtraValidations
|
3
33
|
include NumberValidations
|
4
34
|
include WebValidations
|
data/lib/ohm/contrib/locking.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
module Ohm
|
2
2
|
# This module is a straight extraction from Ohm. The only difference is
|
3
3
|
# that this allows for a custom sleep value.
|
4
|
+
#
|
4
5
|
# In addition, since future ohm versions might drop mutexes, I thought it
|
5
6
|
# might be a good idea to preseve this feature as a drop-in module.
|
6
7
|
module Locking
|
7
|
-
#
|
8
|
+
# Lock the object before executing the block, and release it once the block
|
9
|
+
# is done.
|
8
10
|
#
|
9
|
-
#
|
11
|
+
# @example
|
10
12
|
#
|
11
13
|
# post = Order.create(:customer => Customer.create)
|
12
14
|
# post.mutex(0.01) do
|
@@ -1,4 +1,7 @@
|
|
1
1
|
module Ohm
|
2
|
+
# This module will include all numeric validation needs.
|
3
|
+
# As of VERSION 0.0.15, Ohm::NumberValidations#assert_decimal
|
4
|
+
# is the only method provided.
|
2
5
|
module NumberValidations
|
3
6
|
|
4
7
|
protected
|
@@ -6,4 +9,4 @@ module Ohm
|
|
6
9
|
assert_format(att, /^(\d+)?(\.\d+)?$/, error)
|
7
10
|
end
|
8
11
|
end
|
9
|
-
end
|
12
|
+
end
|
@@ -1,4 +1,20 @@
|
|
1
1
|
module Ohm
|
2
|
+
# Provides created_at / updated_at timestamps.
|
3
|
+
#
|
4
|
+
# @example
|
5
|
+
#
|
6
|
+
# class Post < Ohm::Model
|
7
|
+
# include Ohm::Timestamping
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# post = Post.create
|
11
|
+
# post.created_at.to_s == Time.now.utc.to_s
|
12
|
+
# # => true
|
13
|
+
#
|
14
|
+
# post = Post[post.id]
|
15
|
+
# post.save
|
16
|
+
# post.updated_at.to_s == Time.now.utc.to_s
|
17
|
+
# # => true
|
2
18
|
module Timestamping
|
3
19
|
def self.included(base)
|
4
20
|
base.attribute :created_at
|
@@ -18,4 +34,4 @@ module Ohm
|
|
18
34
|
super
|
19
35
|
end
|
20
36
|
end
|
21
|
-
end
|
37
|
+
end
|
data/lib/ohm/contrib/to_hash.rb
CHANGED
@@ -1,4 +1,30 @@
|
|
1
1
|
module Ohm
|
2
|
+
# Ohm has already added its own `to_hash` method. The difference
|
3
|
+
# is that it chose the albeit better whitelisted approach.
|
4
|
+
#
|
5
|
+
# @example
|
6
|
+
#
|
7
|
+
# # this is the core Ohm#to_hash implementation
|
8
|
+
# class Post < Ohm::Model
|
9
|
+
# attribute :body
|
10
|
+
# def validate
|
11
|
+
# assert_present :body
|
12
|
+
# end
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# Post.create.to_hash == { :errors => [[:body, :not_present]] }
|
16
|
+
# # => true
|
17
|
+
#
|
18
|
+
# Post.create(:body => "The body").to_hash == { :id => 1 }
|
19
|
+
# # => true
|
20
|
+
#
|
21
|
+
# # now this is the all-in-one (kinda Railsy) method.
|
22
|
+
# class Post < Ohm::Model
|
23
|
+
# attribute :body
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# Post.create(:body => "Body").to_hash == { :id => 1, :body => "Body" }
|
27
|
+
# # => true
|
2
28
|
module ToHash
|
3
29
|
def to_hash
|
4
30
|
atts = attributes + counters
|
@@ -8,4 +34,4 @@ module Ohm
|
|
8
34
|
end
|
9
35
|
alias :to_h :to_hash
|
10
36
|
end
|
11
|
-
end
|
37
|
+
end
|
data/lib/ohm/contrib/typecast.rb
CHANGED
@@ -3,6 +3,14 @@ require 'time'
|
|
3
3
|
require 'date'
|
4
4
|
|
5
5
|
module Ohm
|
6
|
+
# Provides all the primitive types. The following are included:
|
7
|
+
#
|
8
|
+
# * String
|
9
|
+
# * Decimal
|
10
|
+
# * Integer
|
11
|
+
# * Float
|
12
|
+
# * Date
|
13
|
+
# * Time
|
6
14
|
module Types
|
7
15
|
class Primitive < BasicObject
|
8
16
|
def initialize(value)
|
@@ -73,7 +81,58 @@ module Ohm
|
|
73
81
|
end
|
74
82
|
end
|
75
83
|
end
|
76
|
-
|
84
|
+
|
85
|
+
# Provides unobtrusive, non-explosive typecasting.Instead of exploding on set
|
86
|
+
# of an invalid value, this module takes the approach of just taking in
|
87
|
+
# parameters and letting you do validation yourself. The only thing this
|
88
|
+
# module does for you is the boilerplate casting you might need to do.
|
89
|
+
#
|
90
|
+
# @example
|
91
|
+
#
|
92
|
+
# # without typecasting
|
93
|
+
# class Item < Ohm::Model
|
94
|
+
# attribute :price
|
95
|
+
# attribute :posted
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# item = Item.create(:price => 299, :posted => Time.now.utc)
|
99
|
+
# item = Item[item.id]
|
100
|
+
#
|
101
|
+
# # now when you try and grab `item.price`, its a string.
|
102
|
+
# "299" == item.price
|
103
|
+
# # => true
|
104
|
+
#
|
105
|
+
# # you can opt to manually cast everytime, or do it in the model, i.e.
|
106
|
+
#
|
107
|
+
# class Item
|
108
|
+
# def price
|
109
|
+
# BigDecimal(read_local(:price))
|
110
|
+
# end
|
111
|
+
# end
|
112
|
+
#
|
113
|
+
# The Typecasted way
|
114
|
+
# ------------------
|
115
|
+
#
|
116
|
+
# class Item < Ohm::Model
|
117
|
+
# include Ohm::Typecast
|
118
|
+
#
|
119
|
+
# attribute :price, Decimal
|
120
|
+
# attribute :posted, Time
|
121
|
+
# end
|
122
|
+
#
|
123
|
+
# item = Item.create(:price => "299", :posted => Time.now.utc)
|
124
|
+
# item = Item[item.id]
|
125
|
+
# item.price.class == Ohm::Types::Decimal
|
126
|
+
# # => true
|
127
|
+
#
|
128
|
+
# item.price.to_s == "299"
|
129
|
+
# # => true
|
130
|
+
#
|
131
|
+
# item.price * 2 == 598
|
132
|
+
# # => true
|
133
|
+
#
|
134
|
+
# item.posted.strftime('%m/%d/%Y')
|
135
|
+
# # => works!!!
|
77
136
|
module Typecast
|
78
137
|
include Types
|
79
138
|
|
@@ -96,4 +155,4 @@ module Ohm
|
|
96
155
|
end
|
97
156
|
end
|
98
157
|
end
|
99
|
-
end
|
158
|
+
end
|
data/ohm-contrib.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ohm-contrib}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.15"
|
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"]
|
@@ -45,7 +45,7 @@ Gem::Specification.new do |s|
|
|
45
45
|
"test/test_ohm_typecast.rb",
|
46
46
|
"test/test_ohm_web_validations.rb"
|
47
47
|
]
|
48
|
-
s.homepage = %q{http://
|
48
|
+
s.homepage = %q{http://labs.sinefunc.com/ohm-contrib}
|
49
49
|
s.rdoc_options = ["--charset=UTF-8"]
|
50
50
|
s.require_paths = ["lib"]
|
51
51
|
s.rubygems_version = %q{1.3.6}
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 15
|
9
|
+
version: 0.0.15
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Cyril David
|
@@ -115,7 +115,7 @@ files:
|
|
115
115
|
- test/test_ohm_typecast.rb
|
116
116
|
- test/test_ohm_web_validations.rb
|
117
117
|
has_rdoc: true
|
118
|
-
homepage: http://
|
118
|
+
homepage: http://labs.sinefunc.com/ohm-contrib
|
119
119
|
licenses: []
|
120
120
|
|
121
121
|
post_install_message:
|