Module: SimplyStored::SimpleDB::Attributes
- Included in:
- SimplyStored::Simple
- Defined in:
- lib/simply_stored/simpledb/attributes.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #_remove_protected_attributes(attrs) ⇒ Object
- #attributes=(attr) ⇒ Object
- #partition_string_attributes ⇒ Object
- #restore_partitioned_attributes ⇒ Object
Class Method Details
.included(base) ⇒ Object
4 5 6 |
# File 'lib/simply_stored/simpledb/attributes.rb', line 4 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#_remove_protected_attributes(attrs) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/simply_stored/simpledb/attributes.rb', line 127 def _remove_protected_attributes(attrs) return {} if attrs.blank? attrs = attrs.dup.stringify_keys (self.class.instance_variable_get(:@_protected_attributes) || []).map(&:to_s).each do |protected_attribute| attrs.delete(protected_attribute) end accessible_attributes = (self.class.instance_variable_get(:@_accessible_attributes) || []).map(&:to_s) if accessible_attributes.present? attrs.each do |attr_key, attr_value| attrs.delete(attr_key) unless accessible_attributes.include?(attr_key) end end attrs end |
#attributes=(attr) ⇒ Object
122 123 124 125 |
# File 'lib/simply_stored/simpledb/attributes.rb', line 122 def attributes=(attr) attr = _remove_protected_attributes(attr) super(attr) end |
#partition_string_attributes ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/simply_stored/simpledb/attributes.rb', line 145 def partition_string_attributes (self.class.instance_variable_get(:@_partitioned_attributes) || []).each do |attr| next if self[attr].nil? value = self[attr].first if value.present? && value.size > 1024 0.upto(value.size / 1024) do |i| self["#{attr.to_s}_#{i}"] = value[(1024 * i)..(1024 * (i + 1) - 1)] end @_partitioned_attributes ||= {} @_partitioned_attributes[attr] = self[attr] send("#{attr}=", []) end end end |
#restore_partitioned_attributes ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/simply_stored/simpledb/attributes.rb', line 160 def restore_partitioned_attributes (self.class.instance_variable_get(:@_partitioned_attributes) || []).each do |attr| if (partitions = attributes.select{|key, value| key.match(/^#{attr}_([0-9]+)$/)}).any? unpartitioned_value = "" partitions.sort_by{|key, value| key.match(/^#{attr}_([0-9]+)$/); $1}.each do |partition| unpartitioned_value << partition.last.first end self[attr] = unpartitioned_value end end end |