ohm-contrib 1.1.0 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,7 @@ require "bigdecimal"
2
2
  require "date"
3
3
  require "json"
4
4
  require "time"
5
+ require "set"
5
6
 
6
7
  module Ohm
7
8
  module DataTypes
@@ -9,12 +10,14 @@ module Ohm
9
10
  Integer = lambda { |x| x.to_i }
10
11
  Decimal = lambda { |x| BigDecimal(x.to_s) }
11
12
  Float = lambda { |x| x.to_f }
13
+ Symbol = lambda { |x| x.to_sym }
12
14
  Boolean = lambda { |x| Ohm::DataTypes.bool(x) }
13
15
  Time = lambda { |t| t && (t.kind_of?(::Time) ? t : ::Time.parse(t)) }
14
16
  Date = lambda { |d| d && (d.kind_of?(::Date) ? d : ::Date.parse(d)) }
15
17
  Timestamp = lambda { |t| t && UnixTime.at(t.to_i) }
16
18
  Hash = lambda { |h| h && SerializedHash[h.kind_of?(::Hash) ? h : JSON(h)] }
17
19
  Array = lambda { |a| a && SerializedArray.new(a.kind_of?(::Array) ? a : JSON(a)) }
20
+ Set = lambda { |s| s && SerializedSet.new(s.kind_of?(::Set) ? s : JSON(s)) }
18
21
  end
19
22
 
20
23
  def self.bool(val)
@@ -43,5 +46,11 @@ module Ohm
43
46
  JSON.dump(self)
44
47
  end
45
48
  end
49
+
50
+ class SerializedSet < ::Set
51
+ def to_s
52
+ JSON.dump(to_a.sort)
53
+ end
54
+ end
46
55
  end
47
56
  end
@@ -14,16 +14,36 @@ module Ohm
14
14
  self::DefinedScopes.send(:include, scope) if scope
15
15
  end
16
16
  end
17
+ end
17
18
 
18
- module OverloadedSet
19
- def initialize(*args)
20
- super
19
+ # In Ohm v1.2, the Set and MultiSet initialize methods
20
+ # are defined on themselves. Hence the trick of doing a
21
+ # module OverloadedSet with an initialize method doesn't
22
+ # work anymore.
23
+ #
24
+ # The simplest way to solve that as of now is to duplicate
25
+ # and extend the #initialize method for each of these.
26
+ #
27
+ # Granted it's not the _ideal_ way, the drawbacks are
28
+ # outweighed by the simplicity and performance of this
29
+ # approach versus other monkey-patching techniques.
30
+ class Set
31
+ def initialize(key, namespace, model)
32
+ @key = key
33
+ @namespace = namespace
34
+ @model = model
21
35
 
22
- extend model::DefinedScopes if defined?(model::DefinedScopes)
23
- end
36
+ extend model::DefinedScopes if defined?(model::DefinedScopes)
24
37
  end
25
38
  end
26
39
 
27
- Set.send :include, Scope::OverloadedSet
28
- MultiSet.send :include, Scope::OverloadedSet
40
+ class MultiSet
41
+ def initialize(namespace, model, command)
42
+ @namespace = namespace
43
+ @model = model
44
+ @command = command
45
+
46
+ extend model::DefinedScopes if defined?(model::DefinedScopes)
47
+ end
48
+ end
29
49
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "ohm-contrib"
3
- s.version = "1.1.0"
3
+ s.version = "1.2"
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"
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
21
21
  s.require_paths = ["lib"]
22
22
  s.rubyforge_project = "ohm-contrib"
23
23
 
24
- s.add_dependency "ohm", "~> 1.1"
24
+ s.add_dependency "ohm", "~> 1.2"
25
25
 
26
26
  s.add_development_dependency "cutest"
27
27
  s.add_development_dependency "redis"
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require File.expand_path("../helper", __FILE__)
4
+ require 'set'
4
5
 
5
6
  class Comment < Ohm::Model
6
7
  include Ohm::Timestamps
@@ -159,6 +160,8 @@ scope do
159
160
  attribute :sizes, Type::Hash
160
161
  attribute :stores, Type::Array
161
162
  attribute :published, Type::Boolean
163
+ attribute :state, Type::Symbol
164
+ attribute :comments, Type::Set
162
165
  end
163
166
 
164
167
  test "Type::Integer" do
@@ -269,4 +272,28 @@ scope do
269
272
  assert_equal "false", p.key.hget(:published)
270
273
  assert_equal false, p.published
271
274
  end
275
+
276
+ test "Type::Symbol" do
277
+ p = Product.new(:state => 'available')
278
+
279
+ assert_equal :available, p.state
280
+ p.save
281
+
282
+ p = Product[p.id]
283
+ assert_equal :available, p.state
284
+ end
285
+
286
+ test "Type::Set" do
287
+ comments = ::Set.new(["Good product", "Awesome!", "Great."])
288
+ p = Product.new(:comments => comments)
289
+ assert p.comments.kind_of?(::Set)
290
+
291
+ p.save
292
+
293
+ p = Product[p.id]
294
+
295
+ assert_equal comments, p.comments
296
+ assert_equal %Q(["Awesome!","Good product","Great."]), p.key.hget(:comments)
297
+ assert_equal %Q(["Awesome!","Good product","Great."]), p.comments.to_s
298
+ end
272
299
  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.1.0
4
+ version: '1.2'
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-07-24 00:00:00.000000000 Z
12
+ date: 2012-08-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ohm
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '1.1'
21
+ version: '1.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '1.1'
29
+ version: '1.2'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: cutest
32
32
  requirement: !ruby/object:Gem::Requirement