ripple 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ripple.rb +17 -1
- data/lib/ripple/associations.rb +157 -0
- data/lib/ripple/associations/embedded.rb +42 -0
- data/lib/ripple/associations/instantiators.rb +39 -0
- data/lib/ripple/{document/associations → associations}/linked.rb +9 -11
- data/lib/ripple/{document/associations/one_embedded_proxy.rb → associations/many.rb} +30 -21
- data/lib/ripple/{document/associations/embedded.rb → associations/many_embedded_proxy.rb} +26 -23
- data/lib/ripple/{embedded_document/conversion.rb → associations/one.rb} +8 -13
- data/lib/ripple/{document/associations/instantiators.rb → associations/one_embedded_proxy.rb} +17 -19
- data/lib/ripple/associations/proxy.rb +123 -0
- data/lib/ripple/attribute_methods.rb +116 -0
- data/lib/ripple/{document/attribute_methods/write.rb → attribute_methods/dirty.rb} +26 -16
- data/lib/ripple/attribute_methods/query.rb +48 -0
- data/lib/ripple/{document/attribute_methods → attribute_methods}/read.rb +16 -18
- data/lib/ripple/attribute_methods/write.rb +38 -0
- data/lib/ripple/callbacks.rb +73 -0
- data/lib/ripple/{document/associations/one.rb → conversion.rb} +18 -13
- data/lib/ripple/document.rb +19 -9
- data/lib/ripple/embedded_document.rb +10 -10
- data/lib/ripple/embedded_document/persistence.rb +12 -53
- data/lib/ripple/properties.rb +83 -0
- data/lib/ripple/timestamps.rb +34 -0
- data/lib/ripple/{document/validations.rb → validations.rb} +31 -33
- data/lib/ripple/{document/validations → validations}/associated_validator.rb +9 -10
- data/spec/integration/ripple/associations_spec.rb +1 -2
- data/spec/integration/ripple/persistence_spec.rb +2 -3
- data/spec/ripple/associations/many_embedded_proxy_spec.rb +2 -2
- data/spec/ripple/associations/one_embedded_proxy_spec.rb +2 -2
- data/spec/ripple/associations/proxy_spec.rb +1 -1
- data/spec/ripple/associations_spec.rb +15 -20
- data/spec/ripple/attribute_methods_spec.rb +3 -6
- data/spec/ripple/callbacks_spec.rb +1 -1
- data/spec/ripple/{embedded_document/conversion_spec.rb → conversion_spec.rb} +4 -4
- data/spec/ripple/embedded_document/persistence_spec.rb +4 -16
- data/spec/ripple/properties_spec.rb +17 -18
- data/spec/ripple/timestamps_spec.rb +1 -1
- data/spec/ripple/validations_spec.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- data/spec/support/associations/proxies.rb +4 -4
- metadata +27 -29
- data/lib/ripple/document/associations.rb +0 -154
- data/lib/ripple/document/associations/many.rb +0 -52
- data/lib/ripple/document/associations/many_embedded_proxy.rb +0 -49
- data/lib/ripple/document/associations/proxy.rb +0 -125
- data/lib/ripple/document/attribute_methods.rb +0 -118
- data/lib/ripple/document/attribute_methods/dirty.rb +0 -52
- data/lib/ripple/document/attribute_methods/query.rb +0 -49
- data/lib/ripple/document/callbacks.rb +0 -75
- data/lib/ripple/document/properties.rb +0 -85
- data/lib/ripple/document/timestamps.rb +0 -22
- data/spec/support/integration.rb +0 -4
@@ -1,118 +0,0 @@
|
|
1
|
-
# Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc.
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
require 'ripple'
|
15
|
-
|
16
|
-
module Ripple
|
17
|
-
module Document
|
18
|
-
# Makes ActiveRecord-like attribute accessors based on your
|
19
|
-
# {Document}'s properties.
|
20
|
-
module AttributeMethods
|
21
|
-
extend ActiveSupport::Concern
|
22
|
-
extend ActiveSupport::Autoload
|
23
|
-
include ActiveModel::AttributeMethods
|
24
|
-
|
25
|
-
autoload :Read
|
26
|
-
autoload :Write
|
27
|
-
autoload :Query
|
28
|
-
autoload :Dirty
|
29
|
-
|
30
|
-
included do
|
31
|
-
include Read
|
32
|
-
include Write
|
33
|
-
include Query
|
34
|
-
include Dirty
|
35
|
-
end
|
36
|
-
|
37
|
-
module ClassMethods
|
38
|
-
# @private
|
39
|
-
def property(key, type, options={})
|
40
|
-
undefine_attribute_methods
|
41
|
-
super
|
42
|
-
end
|
43
|
-
|
44
|
-
# Generates all the attribute-related methods for properties defined
|
45
|
-
# on the document, including accessors, mutators and query methods.
|
46
|
-
def define_attribute_methods
|
47
|
-
super(properties.keys)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
module InstanceMethods
|
52
|
-
attr_accessor :key
|
53
|
-
|
54
|
-
# A copy of the values of all attributes in the Document. The result
|
55
|
-
# is not memoized, so use sparingly. This does not include associated objects,
|
56
|
-
# nor embedded documents.
|
57
|
-
# @return [Hash] all document attributes, by key
|
58
|
-
def attributes
|
59
|
-
self.class.properties.values.inject({}) do |hash, prop|
|
60
|
-
hash[prop.key] = attribute(prop.key)
|
61
|
-
hash
|
62
|
-
end.with_indifferent_access
|
63
|
-
end
|
64
|
-
|
65
|
-
# Mass assign the document's attributes.
|
66
|
-
# @param [Hash] attrs the attributes to assign
|
67
|
-
def attributes=(attrs)
|
68
|
-
raise ArgumentError, t('attribute_hash') unless Hash === attrs
|
69
|
-
attrs.each do |k,v|
|
70
|
-
next if k.to_sym == :key
|
71
|
-
if respond_to?("#{k}=")
|
72
|
-
__send__("#{k}=",v)
|
73
|
-
else
|
74
|
-
__send__(:attribute=,k,v)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
def key=(value)
|
80
|
-
@key = value.to_s
|
81
|
-
end
|
82
|
-
|
83
|
-
# @private
|
84
|
-
def initialize(attrs={})
|
85
|
-
super()
|
86
|
-
@attributes = attributes_from_property_defaults
|
87
|
-
self.attributes = attrs
|
88
|
-
yield self if block_given?
|
89
|
-
end
|
90
|
-
|
91
|
-
# @private
|
92
|
-
def method_missing(method, *args, &block)
|
93
|
-
self.class.define_attribute_methods
|
94
|
-
super
|
95
|
-
end
|
96
|
-
|
97
|
-
# @private
|
98
|
-
def respond_to?(*args)
|
99
|
-
self.class.define_attribute_methods
|
100
|
-
super
|
101
|
-
end
|
102
|
-
|
103
|
-
protected
|
104
|
-
# @private
|
105
|
-
def attribute_method?(attr_name)
|
106
|
-
self.class.properties.include?(attr_name)
|
107
|
-
end
|
108
|
-
|
109
|
-
def attributes_from_property_defaults
|
110
|
-
self.class.properties.values.inject({}) do |hash, prop|
|
111
|
-
hash[prop.key] = prop.default if prop.default
|
112
|
-
hash
|
113
|
-
end.with_indifferent_access
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
# Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc.
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
require 'ripple'
|
15
|
-
|
16
|
-
module Ripple
|
17
|
-
module Document
|
18
|
-
module AttributeMethods
|
19
|
-
module Dirty
|
20
|
-
extend ActiveSupport::Concern
|
21
|
-
include ActiveModel::Dirty
|
22
|
-
|
23
|
-
# @private
|
24
|
-
def save
|
25
|
-
if result = super
|
26
|
-
changed_attributes.clear
|
27
|
-
end
|
28
|
-
result
|
29
|
-
end
|
30
|
-
|
31
|
-
# @private
|
32
|
-
def reload
|
33
|
-
returning super do
|
34
|
-
changed_attributes.clear
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
# @private
|
39
|
-
def initialize(attrs={})
|
40
|
-
super(attrs)
|
41
|
-
changed_attributes.clear
|
42
|
-
end
|
43
|
-
|
44
|
-
private
|
45
|
-
def attribute=(attr_name, value)
|
46
|
-
attribute_will_change!(attr_name)
|
47
|
-
super
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
# Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc.
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
require 'ripple'
|
15
|
-
|
16
|
-
module Ripple
|
17
|
-
module Document
|
18
|
-
module AttributeMethods
|
19
|
-
module Query
|
20
|
-
extend ActiveSupport::Concern
|
21
|
-
|
22
|
-
included do
|
23
|
-
attribute_method_suffix "?"
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
# Based on code from ActiveRecord
|
28
|
-
def attribute?(attr_name)
|
29
|
-
unless value = attribute(attr_name)
|
30
|
-
false
|
31
|
-
else
|
32
|
-
prop = self.class.properties[attr_name]
|
33
|
-
if prop.nil?
|
34
|
-
if Numeric === value || value !~ /[^0-9]/
|
35
|
-
!value.to_i.zero?
|
36
|
-
else
|
37
|
-
Boolean.ripple_cast(value) || value.present?
|
38
|
-
end
|
39
|
-
elsif prop.type <= Numeric
|
40
|
-
!value.zero?
|
41
|
-
else
|
42
|
-
value.present?
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,75 +0,0 @@
|
|
1
|
-
# Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc.
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
require 'ripple'
|
15
|
-
|
16
|
-
module Ripple
|
17
|
-
module Document
|
18
|
-
module Callbacks
|
19
|
-
extend ActiveSupport::Concern
|
20
|
-
|
21
|
-
included do
|
22
|
-
extend ActiveModel::Callbacks
|
23
|
-
define_model_callbacks :create, :update, :save, :destroy
|
24
|
-
define_callbacks :validation, :terminator => "result == false", :scope => [:kind, :name]
|
25
|
-
end
|
26
|
-
|
27
|
-
module ClassMethods
|
28
|
-
def before_validation(*args, &block)
|
29
|
-
options = args.last
|
30
|
-
if options.is_a?(Hash) && options[:on]
|
31
|
-
options[:if] = Array(options[:if])
|
32
|
-
options[:if] << "@_on_validate == :#{options[:on]}"
|
33
|
-
end
|
34
|
-
set_callback(:validation, :before, *args, &block)
|
35
|
-
end
|
36
|
-
|
37
|
-
def after_validation(*args, &block)
|
38
|
-
options = args.extract_options!
|
39
|
-
options[:prepend] = true
|
40
|
-
options[:if] = Array(options[:if])
|
41
|
-
options[:if] << "!halted && value != false"
|
42
|
-
options[:if] << "@_on_validate == :#{options[:on]}" if options[:on]
|
43
|
-
set_callback(:validation, :after, *(args << options), &block)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
module InstanceMethods
|
48
|
-
# @private
|
49
|
-
def save(*args, &block)
|
50
|
-
state = new? ? :create : :update
|
51
|
-
run_callbacks(:save) do
|
52
|
-
run_callbacks(state) do
|
53
|
-
super
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
# @private
|
59
|
-
def destroy(*args, &block)
|
60
|
-
run_callbacks(:destroy) do
|
61
|
-
super
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
# @private
|
66
|
-
def valid?(*args, &block)
|
67
|
-
@_on_validate = new? ? :create : :update
|
68
|
-
run_callbacks(:validation) do
|
69
|
-
super
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
@@ -1,85 +0,0 @@
|
|
1
|
-
# Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc.
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
require 'ripple'
|
15
|
-
require 'ripple/core_ext/casting'
|
16
|
-
|
17
|
-
module Ripple
|
18
|
-
module Document
|
19
|
-
# Adds the ability to declare properties on your Ripple::Document class.
|
20
|
-
# Properties will automatically generate accessor (get/set/query) methods and
|
21
|
-
# handle type-casting between your Ruby type and JSON-compatible types.
|
22
|
-
module Properties
|
23
|
-
# @private
|
24
|
-
def inherited(subclass)
|
25
|
-
super
|
26
|
-
subclass.properties.merge!(properties)
|
27
|
-
end
|
28
|
-
|
29
|
-
# @return [Hash] the properties defined on the document
|
30
|
-
def properties
|
31
|
-
@properties ||= {}.with_indifferent_access
|
32
|
-
end
|
33
|
-
|
34
|
-
def property(key, type, options={})
|
35
|
-
prop = Property.new(key, type, options)
|
36
|
-
properties[prop.key] = prop
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
# Encapsulates a single property on your Ripple::Document class.
|
41
|
-
class Property
|
42
|
-
# @return [Symbol] the key of this property in the Document
|
43
|
-
attr_reader :key
|
44
|
-
# @return [Class] the Ruby type of property.
|
45
|
-
attr_reader :type
|
46
|
-
# @return [Hash] configuration options
|
47
|
-
attr_reader :options
|
48
|
-
|
49
|
-
# Create a new document property.
|
50
|
-
# @param [String, Symbol] key the key of the property
|
51
|
-
# @param [Class] type the Ruby type of the property. Use {Boolean} for true or false types.
|
52
|
-
# @param [Hash] options configuration options
|
53
|
-
# @option options [Object, Proc] :default (nil) a default value for the property, or a lambda to evaluate when providing the default.
|
54
|
-
def initialize(key, type, options={})
|
55
|
-
@options = options.to_options
|
56
|
-
@key = key.to_sym
|
57
|
-
@type = type
|
58
|
-
end
|
59
|
-
|
60
|
-
# @return [Object] The default value for this property if defined, or nil.
|
61
|
-
def default
|
62
|
-
if default = options[:default]
|
63
|
-
type_cast(default.respond_to?(:call) ? default.call : default)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
# @return [Hash] options appropriate for the validates class method
|
68
|
-
def validation_options
|
69
|
-
@options.dup.except(:default)
|
70
|
-
end
|
71
|
-
|
72
|
-
# Attempt to coerce the passed value into this property's type
|
73
|
-
# @param [Object] value the value to coerce
|
74
|
-
# @return [Object] the value coerced into this property's type
|
75
|
-
# @raise [PropertyTypeMismatch] if the value cannot be coerced into the property's type
|
76
|
-
def type_cast(value)
|
77
|
-
if @type.respond_to?(:ripple_cast)
|
78
|
-
@type.ripple_cast(value)
|
79
|
-
else
|
80
|
-
value
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
module Ripple
|
2
|
-
module Document
|
3
|
-
module Timestamps
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
module ClassMethods
|
7
|
-
def timestamps!
|
8
|
-
property :created_at, Time, :default => proc { Time.now.utc }
|
9
|
-
property :updated_at, Time
|
10
|
-
before_save :touch
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
module InstanceMethods
|
15
|
-
def touch
|
16
|
-
self.updated_at = Time.now.utc
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
data/spec/support/integration.rb
DELETED