dynamic_attributes 1.1.2 → 1.1.3

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 (4) hide show
  1. data/README.rdoc +2 -0
  2. data/VERSION +1 -1
  3. data/lib/dynamic_attributes.rb +15 -8
  4. metadata +2 -2
data/README.rdoc CHANGED
@@ -69,6 +69,8 @@ To add dynamic_attributes to an AR model, take the following steps:
69
69
  Note that a dynamic attribute should be prefixed (by default with 'field_'), see the Options section for more info.
70
70
 
71
71
  * Get Info:
72
+ * dynamic_model.has_dynamic_attribute?(dynamic_attribute)
73
+ * Returns whether a given attribute is a dynamic attribute. Accepts strings and symbols.
72
74
  * dynamic_model.persisting_dynamic_attributes
73
75
  * Returns an array of the dynamic attributes that will be persisted.
74
76
  * DynamicModel.dynamic_attribute_field
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.2
1
+ 1.1.3
@@ -34,15 +34,22 @@ module DynamicAttributes
34
34
  set_dynamic_attributes(dynamic_attributes)
35
35
  end
36
36
 
37
+ # Returns whether a given attribute is a dynamic attribute
37
38
  def has_dynamic_attribute?(dynamic_attribute)
38
39
  return persisting_dynamic_attributes.include?(dynamic_attribute.to_s)
39
40
  end
41
+
42
+ # Reads the value of the given dynamic attribute.
43
+ # Returns nil if the attribute does not exist or if it is not a dynamic attribute.
44
+ def read_dynamic_attribute(dynamic_attribute)
45
+ has_dynamic_attribute?(dynamic_attribute) ? (send(dynamic_attribute) rescue nil) : nil
46
+ end
40
47
 
41
48
  # On saving an AR record, the attributes to be persisted are re-evaluated and written to the serialization field.
42
49
  def evaluate_dynamic_attributes
43
50
  new_dynamic_attributes = {}
44
51
  self.persisting_dynamic_attributes.uniq.each do |dynamic_attribute|
45
- value = send(dynamic_attribute)
52
+ value = read_dynamic_attribute(dynamic_attribute)
46
53
  if value.nil? and destroy_dynamic_attribute_for_nil
47
54
  self.persisting_dynamic_attributes.delete(dynamic_attribute)
48
55
  singleton_class.send(:remove_method, dynamic_attribute + '=')
@@ -86,6 +93,13 @@ module DynamicAttributes
86
93
  object.before_save :evaluate_dynamic_attributes
87
94
  object.serialize object.dynamic_attribute_field
88
95
  end
96
+
97
+ # Gets the object's singleton class. Backported from Rails 2.3.8 to support older versions of Rails.
98
+ def singleton_class
99
+ class << self
100
+ self
101
+ end
102
+ end
89
103
 
90
104
  private
91
105
 
@@ -125,11 +139,4 @@ module DynamicAttributes
125
139
  write_attribute(self.dynamic_attribute_field.to_s, (read_attribute(self.dynamic_attribute_field.to_s) || {}).merge(attribute.to_s => value))
126
140
  end
127
141
 
128
- # Gets the object's singleton class. Backported from Rails 2.3.8 to support older versions of Rails.
129
- def singleton_class
130
- class << self
131
- self
132
- end
133
- end
134
-
135
142
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 1
8
- - 2
9
- version: 1.1.2
8
+ - 3
9
+ version: 1.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Reinier de Lange