splash 0.0.1.alpha → 0.0.1.beta

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.
Files changed (55) hide show
  1. data/Gemfile +5 -0
  2. data/lib/splash.rb +22 -26
  3. data/lib/splash/acts_as_collection.rb +3 -2
  4. data/lib/splash/acts_as_scope.rb +97 -33
  5. data/lib/splash/acts_as_scope_root.rb +2 -1
  6. data/lib/splash/annotated.rb +2 -1
  7. data/lib/splash/application.rb +2 -1
  8. data/lib/splash/attribute.rb +39 -21
  9. data/lib/splash/attributed_struct.rb +2 -1
  10. data/lib/splash/callbacks.rb +37 -0
  11. data/lib/splash/collection.rb +14 -11
  12. data/lib/splash/connection.rb +2 -1
  13. data/lib/splash/constraint.rb +3 -5
  14. data/lib/splash/constraint/all.rb +2 -1
  15. data/lib/splash/constraint/any.rb +2 -1
  16. data/lib/splash/constraint/in.rb +2 -1
  17. data/lib/splash/constraint/not_nil.rb +2 -1
  18. data/lib/splash/created_at.rb +16 -0
  19. data/lib/splash/document.rb +43 -15
  20. data/lib/splash/embed.rb +29 -9
  21. data/lib/splash/has_attributes.rb +68 -37
  22. data/lib/splash/has_collection.rb +79 -0
  23. data/lib/splash/has_constraint.rb +2 -1
  24. data/lib/splash/map_reduce.rb +124 -0
  25. data/lib/splash/matcher.rb +160 -0
  26. data/lib/splash/namespace.rb +149 -15
  27. data/lib/splash/password.rb +2 -1
  28. data/lib/splash/persister.rb +22 -62
  29. data/lib/splash/query_interface.rb +28 -6
  30. data/lib/splash/saveable.rb +5 -104
  31. data/lib/splash/scope.rb +16 -3
  32. data/lib/splash/scope/map_reduce_interface.rb +33 -0
  33. data/lib/splash/scope/options.rb +16 -6
  34. data/lib/splash/updated_at.rb +15 -0
  35. data/lib/splash/validates.rb +3 -1
  36. data/lib/splash/validator.rb +2 -1
  37. data/lib/splash/writeback.rb +46 -0
  38. data/lib/standart_extensions/array.rb +9 -0
  39. data/lib/standart_extensions/bson.rb +122 -0
  40. data/lib/standart_extensions/class.rb +11 -0
  41. data/lib/{splash/standart_extensions → standart_extensions}/hash.rb +6 -1
  42. data/lib/{splash/standart_extensions → standart_extensions}/module.rb +17 -3
  43. data/lib/standart_extensions/object.rb +13 -0
  44. data/splash.gemspec +17 -0
  45. metadata +20 -16
  46. data/lib/splash/scope_delegator.rb +0 -14
  47. data/lib/splash/scope_options.rb +0 -31
  48. data/lib/splash/standart_extensions/object.rb +0 -11
  49. data/lib/splash/standart_extensions/string.rb +0 -5
  50. data/spec/helper.rb +0 -15
  51. data/spec/lib/annotated_spec.rb +0 -87
  52. data/spec/lib/collection_spec.rb +0 -64
  53. data/spec/lib/has_attributes_spec.rb +0 -43
  54. data/spec/lib/has_constraints_spec.rb +0 -53
  55. data/spec/lib/inheritance_spec.rb +0 -69
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  class Splash::Connection
2
3
  def initialize(host,db)
3
4
  @host=host
@@ -23,4 +24,4 @@ class Splash::Connection
23
24
 
24
25
  end
25
26
 
26
- end
27
+ end
@@ -1,11 +1,9 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module Splash::Constraint
2
3
 
3
4
  attr_reader :description
4
5
 
5
- autoload :Any, File.join( File.dirname(__FILE__), 'constraint/any' )
6
- autoload :All, File.join( File.dirname(__FILE__), 'constraint/all' )
7
- autoload :NotNil, File.join( File.dirname(__FILE__), 'constraint/not_nil' )
8
- autoload :In, File.join( File.dirname(__FILE__), 'constraint/in' )
6
+ autoload_all File.join(File.dirname(__FILE__),'constraint')
9
7
 
10
8
  class Simple
11
9
 
@@ -59,4 +57,4 @@ module Splash::Constraint
59
57
 
60
58
  alias_method :to_s, :description
61
59
 
62
- end
60
+ end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  class Splash::Constraint::All < Set
2
3
 
3
4
  include Splash::Constraint
@@ -21,4 +22,4 @@ class Splash::Constraint::All < Set
21
22
  super("all!")
22
23
  end
23
24
 
24
- end
25
+ end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  class Splash::Constraint::Any < Set
2
3
 
3
4
  include Splash::Constraint
@@ -10,4 +11,4 @@ class Splash::Constraint::Any < Set
10
11
  end
11
12
 
12
13
 
13
- end
14
+ end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  class Splash::Constraint::In
2
3
 
3
4
  include Splash::Constraint
@@ -10,4 +11,4 @@ class Splash::Constraint::In
10
11
  def accept?(value)
11
12
  return value.nil? || collection.include?(value)
12
13
  end
13
- end
14
+ end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  class Splash::Constraint::NotNil
2
3
 
3
4
  include Splash::Constraint
@@ -6,4 +7,4 @@ class Splash::Constraint::NotNil
6
7
  !value.nil?
7
8
  end
8
9
 
9
- end
10
+ end
@@ -0,0 +1,16 @@
1
+ module Splash
2
+
3
+
4
+
5
+ class CreatedAt < Time
6
+
7
+ def self.initial_value
8
+ return Time.now
9
+ end
10
+
11
+ end
12
+
13
+
14
+
15
+
16
+ end
@@ -1,38 +1,66 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module Splash::Document
2
3
 
4
+ class Persister
5
+ def to_saveable(value)
6
+ return nil if value.nil?
7
+ return BSON::DBRef.new( value.class.collection.name , value._id )
8
+ end
9
+
10
+ def from_saveable(value)
11
+ return nil if value.nil?
12
+
13
+ if value.kind_of? BSON::DBRef
14
+ found_class = @namespace.class_for(value.namespace)
15
+ unless found_class <= @class
16
+ warn "Trying to fetch an object of type #{found_class} from #{@class}."
17
+ return nil
18
+ end
19
+ return found_class.conditions('_id'=>value.object_id).first
20
+ end
21
+ raise "No idea how to fetch #{value}."
22
+ end
23
+
24
+ def initialize(ns,klass = Object )
25
+ @namespace, @class = ns, klass
26
+ end
27
+ end
28
+
29
+
3
30
  include Splash::Saveable
4
31
  include Splash::HasAttributes
32
+ include Splash::HasCollection
5
33
  include Splash::Validates
6
34
 
7
35
  class << self
8
36
  def included(base)
37
+
9
38
  included_modules.each do |mod|
10
- mod.included(base)
39
+ begin
40
+ mod.included(base)
41
+ rescue NoMethodError
42
+ end
11
43
  end
12
44
 
13
- dd = self
45
+ super(base)
14
46
 
15
47
  base.instance_eval do
16
- include Splash::ActsAsCollection.of(base)
48
+ #include Splash::ActsAsCollection.of(base)
17
49
  extend Splash::ActsAsScopeRoot
18
50
 
19
51
  def included(base)
20
52
  Splash::Document.included(base)
21
53
  super(base)
22
54
  end
55
+
56
+ def persister
57
+ Splash::Document::Persister.new(self.namespace,self)
58
+ end
59
+
60
+ extend_scoped! Splash::ActsAsScope::ArraylikeAccess
61
+
23
62
  end
24
63
  end
25
-
26
- def persister
27
- Splash::Saveable::MultiPersister
28
- end
29
64
  end
30
65
 
31
- def initialize(args={})
32
- self.attributes.load(args)
33
- end
34
-
35
- def to_saveable
36
- attributes.raw
37
- end
38
- end
66
+ end
data/lib/splash/embed.rb CHANGED
@@ -1,13 +1,35 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module Splash::Embed
2
3
 
4
+ class Persister
5
+ def to_saveable(value)
6
+ return nil if value.nil?
7
+ return Splash::Saveable.wrap(value)
8
+ end
9
+
10
+ def from_saveable(value)
11
+ return nil if value.nil?
12
+ return Splash::Saveable.load(value,@base_class)
13
+ end
14
+
15
+ def initialize(klass)
16
+ @base_class = klass
17
+ end
18
+
19
+ end
20
+
3
21
  include Splash::HasAttributes
22
+ include Splash::Saveable
4
23
  include Splash::Validates
5
24
 
6
25
  class << self
7
26
  def included(base)
8
27
  base.extend(ClassMethods)
9
28
  included_modules.each do |mod|
10
- mod.included(base)
29
+ begin
30
+ mod.included(base)
31
+ rescue NoMethodError
32
+ end
11
33
  end
12
34
  end
13
35
 
@@ -20,18 +42,16 @@ module Splash::Embed
20
42
  c.class_eval(&block)
21
43
  c
22
44
  end
45
+
46
+ def persister
47
+ Splash::Embed::Persister.new(self.define{})
48
+ end
23
49
  end
24
50
 
25
51
  module ClassMethods
26
52
  def persister
27
- Splash::Saveable::EmbedPersister
53
+ Splash::Embed::Persister.new(self)
28
54
  end
29
55
  end
30
56
 
31
- def initialize(args={})
32
- self.attributes.load(args)
33
- end
34
- def to_saveable
35
- attributes.raw
36
- end
37
- end
57
+ end
@@ -1,26 +1,21 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module Splash
2
3
  module HasAttributes
3
4
 
4
5
  class Attributes < Hash
6
+
7
+ alias_method :write, :[]=
8
+
5
9
  def load(raw)
6
10
  raw.each do |key,value|
7
- key=key.to_s
8
-
9
-
10
- if value.kind_of? type(key).persisted_class
11
- self[key]=value
12
- elsif Splash::Persister.raw? value
13
- @raw[key]=value
14
- else
15
-
16
- puts value.class
17
- puts type(key).persisted_class
18
- raise "Don't know what to do with #{key}= #{value.inspect}"
19
- end
11
+ self[key]=value
20
12
  end
21
13
  end
22
14
 
23
15
  def load_raw(raw)
16
+ raw.keys.each do |k|
17
+ self.delete k
18
+ end
24
19
  @raw.update(raw)
25
20
  end
26
21
 
@@ -32,13 +27,28 @@ module Splash
32
27
  else
33
28
  value = t.default
34
29
  end
35
- return self[key]=value
30
+ self.write(key,value)
31
+ return value
32
+ end
33
+
34
+ def []=(key,value)
35
+ return if ::NotGiven == value
36
+ key=key.to_s
37
+ if value.kind_of? type(key).type
38
+ self.write(key,value)
39
+ elsif Splash::Persister.raw? value
40
+ @raw[key]=value
41
+ self.delete(key)
42
+ else
43
+ raise "Don't know what to do with #{key}= #{value.inspect}"
44
+ end
36
45
  end
37
46
 
38
47
  def initialize(klass)
39
48
  super()
40
49
  @class = klass
41
50
  @raw = {}
51
+ complete!
42
52
  end
43
53
 
44
54
  def raw
@@ -51,6 +61,20 @@ module Splash
51
61
  end
52
62
 
53
63
  protected
64
+ def complete!
65
+ keys = Set.new
66
+ @class.each_attributes do |attrs|
67
+ attrs.each do |k,v|
68
+ unless keys.include?(k)
69
+ keys << k
70
+ self[k]=v.initial_value
71
+ end
72
+
73
+ end
74
+ end
75
+ return self
76
+ end
77
+
54
78
  def write_back!
55
79
  self.each do |key,value|
56
80
  @raw[key]=type(key).write(value)
@@ -65,19 +89,6 @@ module Splash
65
89
  merged_inheritable_attr :attributes,{}
66
90
  end
67
91
  end
68
-
69
- def get_persister(*args,&block)
70
- klass = Object
71
- if args.length > 0
72
- if args.first.respond_to? :persister
73
- klass = args.shift
74
- end
75
- end
76
- type = (klass).persister
77
- result=type.new(*args,&block)
78
- result.persist_class(klass)
79
- return result
80
- end
81
92
  end
82
93
 
83
94
  def attributes
@@ -95,10 +106,10 @@ module Splash
95
106
  end
96
107
 
97
108
  def inspect
98
- "#{self.class.to_s}{#{attributes.inspect}}"
109
+ "#{self.class.to_s}{#{attributes.raw.inspect}}"
99
110
  end
100
111
 
101
- def to_saveable
112
+ def to_raw
102
113
  attributes.raw
103
114
  end
104
115
 
@@ -114,6 +125,11 @@ module Splash
114
125
  super
115
126
  end
116
127
 
128
+ def initialize(attr={})
129
+ self.attributes.load(attr)
130
+ super()
131
+ end
132
+
117
133
  module ClassMethods
118
134
 
119
135
  def attribute_accessor(name)
@@ -123,24 +139,39 @@ def #{name.to_s}=(value) return attributes[#{name.to_s.inspect}] = value end
123
139
  CODE
124
140
  end
125
141
 
126
- def def_attribute(name,*args,&block)
142
+ def attribute(name,*args,&block)
127
143
  name = name.to_s
144
+ each_attributes do |attrs|
145
+ if attrs.key? name
146
+ if args.size > 0 || block
147
+ warn "trying to add the existing attribute #{name} of #{self}"
148
+ break
149
+ end
150
+ return attrs[name]
151
+ end
152
+ end
128
153
  attr=attributes[name]=Splash::Attribute.new(*args, &block)
129
154
  attribute_accessor(name)
130
155
  return attr
131
156
  end
132
157
 
133
- def attribute(name,create = true)
158
+ alias def_attribute attribute
159
+
160
+ def attribute?(name)
134
161
  name = name.to_s
135
162
  each_attributes do |attr|
136
- return attr[name] if attr.key? name
137
- end
138
- if create
139
- return def_attribute(name)
163
+ return true if attr.key? name
140
164
  end
141
- return nil
165
+ return false
166
+ end
167
+
168
+ def from_raw(data)
169
+ c = self.new()
170
+ c
171
+ c.attributes.load_raw(data)
172
+ return c
142
173
  end
143
174
 
144
175
  end
145
176
  end
146
- end
177
+ end
@@ -0,0 +1,79 @@
1
+ module Splash
2
+
3
+ module HasCollection
4
+
5
+ def self.included(base)
6
+ super(base)
7
+ base.extend(ClassMethods)
8
+ base.instance_variable_set('@is_collection_base',true)
9
+ end
10
+
11
+ def store!
12
+ self._id=self.class.store!(self)
13
+ return self
14
+ end
15
+
16
+ def remove!
17
+ return self.class.collection.remove('_id'=>self._id)
18
+ end
19
+
20
+ def initialize(*args)
21
+ self._id = self.class.collection.pk_factory.new
22
+ super
23
+ end
24
+
25
+ def find_self
26
+ self.class.with_id(self._id)
27
+ end
28
+
29
+ def ==(other)
30
+ return ( other.kind_of?(Splash::HasCollection) and self.class.namespace == other.class.namespace and self._id == other._id )
31
+ end
32
+
33
+ alias eq? ==
34
+
35
+ module ClassMethods
36
+
37
+ def <<(obj)
38
+ obj.store!
39
+ end
40
+
41
+ def store!(object)
42
+ return self.collection.save(
43
+ Saveable.wrap(object)
44
+ );
45
+ end
46
+
47
+ def namespace(*args)
48
+ if args.any?
49
+ self.namespace=args.first
50
+ end
51
+ return (@namespace || Splash::Namespace.default)
52
+ end
53
+
54
+ def namespace=(arg)
55
+ if arg.kind_of? Splash::Namespace
56
+ @namespace = arg
57
+ @collection = nil
58
+ else
59
+ @namespace = Splash::Namespace::NAMESPACES[arg]
60
+ @collection = nil
61
+ end
62
+ end
63
+
64
+ def collection(*args)
65
+ if args.any?
66
+ self.collection= args.first
67
+ end
68
+ return (@collection ||= namespace.collection_for(self))
69
+ end
70
+
71
+ def collection=(arg)
72
+ @collection = arg
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+
79
+ end