uorm 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,39 +1,32 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- uorm (0.0.1)
5
- activesupport
4
+ uorm (0.0.11)
6
5
  em-synchrony
7
6
 
8
7
  GEM
9
8
  remote: https://rubygems.org/
10
9
  specs:
11
- activesupport (3.2.9)
12
- i18n (~> 0.6)
13
- multi_json (~> 1.0)
14
- bson (1.8.0)
15
- activesupport
16
- bson_ext (1.8.0)
17
- bson (~> 1.8.0)
10
+ bson (1.8.2)
11
+ bson_ext (1.8.2)
12
+ bson (~> 1.8.2)
18
13
  columnize (0.3.6)
19
- debugger (1.2.3)
14
+ debugger (1.2.4)
20
15
  columnize (>= 0.3.1)
21
16
  debugger-linecache (~> 1.1.1)
22
- debugger-ruby_core_source (~> 1.1.5)
17
+ debugger-ruby_core_source (~> 1.1.7)
23
18
  debugger-linecache (1.1.2)
24
19
  debugger-ruby_core_source (>= 1.1.1)
25
- debugger-ruby_core_source (1.1.6)
20
+ debugger-ruby_core_source (1.1.7)
26
21
  diff-lcs (1.1.3)
27
22
  em-mongo (0.4.3)
28
23
  bson (>= 1.1.3)
29
24
  eventmachine (>= 0.12.10)
30
- em-synchrony (1.0.2)
25
+ em-synchrony (1.0.3)
31
26
  eventmachine (>= 1.0.0.beta.1)
32
27
  eventmachine (1.0.0)
33
28
  hiredis (0.4.5)
34
- i18n (0.6.1)
35
- multi_json (1.5.0)
36
- oj (2.0.0)
29
+ rake (10.0.3)
37
30
  redis (3.0.2)
38
31
  rspec (2.12.0)
39
32
  rspec-core (~> 2.12.0)
@@ -52,7 +45,7 @@ DEPENDENCIES
52
45
  debugger
53
46
  em-mongo
54
47
  hiredis
55
- oj
48
+ rake
56
49
  redis
57
50
  rspec
58
51
  uorm!
@@ -1,29 +1,32 @@
1
1
  require 'uorm/version'
2
+ require 'time'
2
3
 
3
- require 'active_support/core_ext/hash'
4
+ module Uorm
5
+ autoload :Model, 'uorm/model'
4
6
 
5
- require 'em-synchrony'
7
+ autoload :CRUD, 'uorm/crud'
8
+ module CRUD
9
+ autoload :PersistanceHelper, 'uorm/crud/persistance_helper'
10
+ autoload :Callbacks, 'uorm/crud/callbacks'
11
+ end
6
12
 
7
- module Uorm
8
- autoload :Type, 'uorm/type'
9
- autoload :Field, 'uorm/field'
10
- autoload :FieldCollection, 'uorm/field_collection'
11
- autoload :Attributes, 'uorm/attributes'
12
- autoload :DSL, 'uorm/dsl'
13
- autoload :Model, 'uorm/model'
14
- autoload :Callbacks, 'uorm/callbacks'
13
+ autoload :Attributes, 'uorm/attributes'
14
+ module Attributes
15
+ autoload :Field, 'uorm/attributes/field'
16
+ autoload :FieldCollection, 'uorm/attributes/field_collection'
17
+ autoload :Type, 'uorm/attributes/type'
18
+ end
15
19
 
16
- autoload :Redis, 'uorm/redis'
20
+ autoload :Redis, 'uorm/redis'
17
21
  module Redis
18
- autoload :Persistance, 'uorm/redis/persistance'
19
- autoload :DB, 'uorm/redis/db'
22
+ autoload :Persistance, 'uorm/redis/persistance'
20
23
  end
21
24
 
22
- autoload :Mongo, 'uorm/mongo'
25
+ autoload :Mongo, 'uorm/mongo'
23
26
  module Mongo
24
27
  require 'uorm/mongo/object_id'
25
-
26
- autoload :DB, 'uorm/mongo/db'
27
- autoload :Persistance, 'uorm/mongo/persistance'
28
+ autoload :Persistance, 'uorm/mongo/persistance'
28
29
  end
30
+
31
+ autoload :Hash, 'uorm/hash'
29
32
  end
@@ -1,9 +1,10 @@
1
1
  module Uorm
2
2
  module Attributes
3
+
3
4
  def self.included base
4
5
  base.instance_eval do
5
6
  def field name, options
6
- fields << Field.new(name, options[:type])
7
+ fields << ::Uorm::Attributes::Field.new(name, options[:type])
7
8
 
8
9
  define_method name do
9
10
  get_attribute name
@@ -15,7 +16,7 @@ module Uorm
15
16
  end
16
17
 
17
18
  def fields
18
- @fields ||= FieldCollection.new
19
+ @fields ||= ::Uorm::Attributes::FieldCollection.new
19
20
  end
20
21
  end
21
22
  end
@@ -40,7 +41,7 @@ module Uorm
40
41
  end
41
42
 
42
43
  def initialize_attributes values
43
- @attributes = {}
44
+ @attributes = ::Uorm::Hash.new
44
45
  update_attributes values
45
46
  end
46
47
 
@@ -0,0 +1,30 @@
1
+ module Uorm
2
+ module Attributes
3
+ class Field
4
+ attr_reader :name, :type
5
+
6
+ def initialize name, type
7
+ @name = name.to_sym
8
+ @type = parse_type type
9
+ end
10
+
11
+ def convert value
12
+ type.convert value if value
13
+ end
14
+
15
+ def as_json value
16
+ type.as_json value if value
17
+ end
18
+
19
+ private
20
+
21
+ def parse_type type
22
+ if ::Uorm::Attributes::Type.const_defined? type
23
+ return ::Uorm::Attributes::Type.const_get type
24
+ else
25
+ raise Attributes::Type::NotFound.new "Unknown type: #{type}"
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,9 @@
1
+ module Uorm
2
+ module Attributes
3
+ class FieldCollection < ::Array
4
+ def get name
5
+ self.find { |f| f.name.to_s == name.to_s }
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,68 @@
1
+ module Uorm
2
+ module Attributes
3
+ module Type
4
+
5
+ class Integer
6
+ class << self
7
+ def convert value
8
+ value.to_i
9
+ end
10
+
11
+ def as_json value
12
+ convert value
13
+ end
14
+ end
15
+ end
16
+
17
+ class Boolean
18
+ class << self
19
+ def convert value
20
+ value ? true : false
21
+ end
22
+
23
+ def as_json value
24
+ convert value
25
+ end
26
+ end
27
+ end
28
+
29
+ class Float
30
+ class << self
31
+ def convert value
32
+ value.to_f
33
+ end
34
+
35
+ def as_json value
36
+ convert value
37
+ end
38
+ end
39
+ end
40
+
41
+ class Time
42
+ class << self
43
+ def convert value
44
+ ::Time.parse value.to_s
45
+ rescue
46
+ ::Time.utc(value.to_i)
47
+ end
48
+
49
+ def as_json value
50
+ value.to_i
51
+ end
52
+ end
53
+ end
54
+
55
+ class String
56
+ class << self
57
+ def convert value
58
+ value.to_s
59
+ end
60
+
61
+ def as_json value
62
+ convert value
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,72 @@
1
+ module Uorm
2
+ module CRUD
3
+
4
+ def self.included base
5
+ base.class_eval do
6
+ include ::Uorm::CRUD::PersistanceHelper
7
+ end
8
+
9
+ base.instance_eval do
10
+ def all args = {}, &block
11
+ if block_given?
12
+ persistance.all(args) { |attrs| yield new(attrs) if attrs }
13
+ else
14
+ persistance.all(args).map { |attrs| new(attrs) }
15
+ end
16
+ end
17
+
18
+ def find id, &block
19
+ if block_given?
20
+ persistance.find(id) { |attrs| yield new(attrs) if attrs }
21
+ else
22
+ new persistance.find(id)
23
+ end
24
+ end
25
+
26
+ def create attrs, &block
27
+ obj = new(attrs)
28
+ (block_given?) ? obj.save(&block) : obj.save
29
+ end
30
+ end
31
+ end
32
+
33
+ def save &block
34
+ if id
35
+ if block_given?
36
+ cb = ::Uorm::CRUD::Callbacks::Update.new
37
+ cb.callback(&block)
38
+ persistance.update self, cb
39
+ else
40
+ persistance.update self
41
+ end
42
+ else
43
+ if block_given?
44
+ cb = ::Uorm::CRUD::Callbacks::Create.new
45
+ cb.callback(&block)
46
+ persistance.create self, cb
47
+ else
48
+ persistance.create self
49
+ end
50
+ end
51
+
52
+ self
53
+ end
54
+
55
+ def update attrs, &block
56
+ update_attributes attrs
57
+ if block_given?
58
+ cb = ::Uorm::CRUD::Callbacks::Update.new
59
+ cb.callback(&block) if block_given?
60
+ persistance.update self, cb
61
+ else
62
+ persistance.update self
63
+ end
64
+ self
65
+ end
66
+
67
+ def delete
68
+ persistance.delete self
69
+ self
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,13 @@
1
+ module Uorm
2
+ module CRUD
3
+ module Callbacks
4
+ class Create
5
+ include ::EM::Deferrable
6
+ end
7
+
8
+ class Update
9
+ include ::EM::Deferrable
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ module Uorm
2
+ module CRUD
3
+ module PersistanceHelper
4
+ def self.included base
5
+ base.instance_eval do
6
+ def persistance= value
7
+ @@persistance = value
8
+ end
9
+
10
+ def persistance
11
+ @@persistance
12
+ end
13
+ end
14
+ end
15
+
16
+ def persistance
17
+ self.class.persistance
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module Uorm
2
+ class Hash < ::Hash
3
+
4
+ def except keys
5
+ self.dup.except! keys
6
+ end
7
+
8
+ def except! keys
9
+ Array(keys).each{ |key| self.delete(key) }
10
+ self
11
+ end
12
+ end
13
+ end
@@ -2,8 +2,8 @@ module Uorm
2
2
  module Model
3
3
  def self.included base
4
4
  base.class_eval do
5
- include Uorm::DSL
6
- include Uorm::Attributes
5
+ include ::Uorm::Attributes
6
+ include ::Uorm::CRUD
7
7
  end
8
8
  end
9
9
  end
@@ -1,28 +1,18 @@
1
+ require 'em-mongo'
2
+ require 'em-synchrony'
1
3
  require 'em-synchrony/em-mongo'
2
4
 
3
5
  module Uorm
4
6
  module Mongo
5
7
  def self.included base
6
8
  base.instance_eval do
7
- field :id, type: Type::ObjectId
8
-
9
- def persistance
10
- @persistance ||= Persistance.new self, "#{self.to_s.downcase}s"
11
- end
12
-
13
- def collection
14
- persistance.collection
15
- end
9
+ field :id, type: :ObjectId
16
10
  end
17
11
 
18
12
  def initialize values = {}
19
13
  values[:id] ||= values.delete('_id') || values.delete(:_id)
20
14
  super values
21
15
  end
22
-
23
- def persistance
24
- self.class.persistance
25
- end
26
16
  end
27
17
  end
28
18
  end
@@ -1,13 +1,15 @@
1
1
  module Uorm
2
- module Type
3
- class ObjectId
4
- class << self
5
- def convert value
6
- BSON::ObjectId value.to_s
7
- end
2
+ module Attributes
3
+ module Type
4
+ class ObjectId
5
+ class << self
6
+ def convert value
7
+ ::BSON::ObjectId value.to_s
8
+ end
8
9
 
9
- def as_json value
10
- value.to_s
10
+ def as_json value
11
+ value.to_s
12
+ end
11
13
  end
12
14
  end
13
15
  end
@@ -1,47 +1,48 @@
1
1
  module Uorm
2
2
  module Mongo
3
- class Persistance
4
- attr_reader :collection, :model
5
-
6
- def initialize model, collection_name
7
- @model = model
8
- @collection = DB.collection collection_name
9
- end
10
-
11
- def new? object
12
- object.id ? false : true
13
- end
14
-
3
+ class Persistance < Struct.new :model, :client
15
4
  def all query = {}, &block
16
5
  formatted = format_query(query)
17
-
18
6
  if block_given?
19
- collection.afind(formatted).each &block
7
+ client.afind(formatted).each(&block)
20
8
  else
21
- collection.find formatted
9
+ client.find(formatted)
22
10
  end
23
11
  end
24
12
 
25
13
  def find id, &block
26
- object_id = BSON::ObjectId(id.to_s)
14
+ object_id = ::BSON::ObjectId(id.to_s)
27
15
  if block_given?
28
- collection.afirst(_id: object_id).callback &block
16
+ client.afirst(_id: object_id).callback(&block)
29
17
  else
30
- collection.first(_id: object_id)
18
+ client.first(_id: object_id)
31
19
  end
32
20
  end
33
21
 
34
- def create object, &block
35
- object.id = EM::Synchrony.sync collection.safe_insert(object.attributes.except(:id))
36
- yield object if block_given?
22
+ def create object, cb = nil
23
+ if cb
24
+ safe_insert = client.safe_insert attributes_without_id object
25
+ safe_insert.callback do |object_id|
26
+ object.id = object_id
27
+ cb.succeed object
28
+ end
29
+ else
30
+ object.id = ::EM::Synchrony.sync client.safe_insert attributes_without_id object
31
+ end
37
32
  end
38
33
 
39
- def update object
40
- EventMachine::Synchrony.sync collection.safe_update({_id: object.id}, object.attributes.except(:id))
34
+ def update object, cb = nil
35
+ if cb
36
+ safe_update = client.safe_update({ _id: object.id }, attributes_without_id(object))
37
+ safe_update.callback { cb.succeed object }
38
+ else
39
+ ::EM::Synchrony.sync client.safe_update({ _id: object.id }, attributes_without_id(object))
40
+ end
41
41
  end
42
42
 
43
43
  def delete object
44
- collection.remove _id: object.id
44
+ # wont return a deferrable :/
45
+ client.remove _id: object.id
45
46
  end
46
47
 
47
48
  private
@@ -49,11 +50,14 @@ module Uorm
49
50
  def format_query hash, field = nil
50
51
  hash.inject({}) do |memo, (key, value)|
51
52
  raise "#{key} field unknown" unless current = field || model.fields.get(key)
52
-
53
- memo[key] = value.is_a?(Hash) ? format_query(value, current) : current.convert(value)
53
+ memo[key] = value.respond_to?(:to_hash) ? format_query(value.to_hash, current) : current.convert(value)
54
54
  memo
55
55
  end
56
56
  end
57
+
58
+ def attributes_without_id object
59
+ object.attributes.except(:id)
60
+ end
57
61
  end
58
62
  end
59
63
  end