Module: CouchResource::Struct::InstanceMethods
- Defined in:
- lib/couch_resource/struct.rb
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #get_attribute(name) ⇒ Object
- #get_attribute_before_type_cast(name) ⇒ Object
- #get_attribute_option(attr_name) ⇒ Object
- #set_attribute(name, value) ⇒ Object
- #to_hash ⇒ Object
Instance Method Details
#[](name) ⇒ Object
173 174 175 |
# File 'lib/couch_resource/struct.rb', line 173 def [](name) get_attribute(name) end |
#[]=(name, value) ⇒ Object
177 178 179 |
# File 'lib/couch_resource/struct.rb', line 177 def []=(name, value) set_attribute(name, value) end |
#get_attribute(name) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/couch_resource/struct.rb', line 215 def get_attribute(name) value = get_attribute_before_type_cast(name) attribute_members = self.class.read_inheritable_attribute(:attribute_members) || {} if attribute_members.has_key?(name) value = @attributes[name] option = attribute_members[name] value #if value.nil? # nil #else # klass = option[:is_a] # case klass # when :string, :number, :boolean, :array, :hash, :datetime # self.send("type_cast_for_#{klass}_attributes", value) # when :collection # self.send("type_cast_for_collection_attributes", value, option[:each]) # else # self.send("type_cast_for_object_attributes", value, klass) # end #end else nil end end |
#get_attribute_before_type_cast(name) ⇒ Object
240 241 242 243 |
# File 'lib/couch_resource/struct.rb', line 240 def get_attribute_before_type_cast(name) @attributes ||= HashWithIndifferentAccess.new({}) @attributes[name] end |
#get_attribute_option(attr_name) ⇒ Object
181 182 183 |
# File 'lib/couch_resource/struct.rb', line 181 def get_attribute_option(attr_name) (self.class.read_inheritable_attribute(:attribute_members) || {})[attr_name] end |
#set_attribute(name, value) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/couch_resource/struct.rb', line 185 def set_attribute(name, value) @attributes ||= HashWithIndifferentAccess.new({}) # inplicit type cast attribute_members = self.class.read_inheritable_attribute(:attribute_members) || {} if attribute_members.has_key?(name) option = attribute_members[name] if value.nil? @attributes[name] = nil else if option[:allow_nil] && value.blank? @attributes[name] = nil else klass = option[:is_a] @attributes[name] = case klass when :string, :number, :boolean, :array, :hash, :datetime self.send("type_cast_for_#{klass}_attributes", value) when :collection self.send("type_cast_for_collection_attributes", value, option[:each]) else self.send("type_cast_for_object_attributes", value, klass) end end end else @attributes[name] = nil end value end |
#to_hash ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/couch_resource/struct.rb', line 245 def to_hash hash = HashWithIndifferentAccess.new({ :class => self.class.name }) (self.class.read_inheritable_attribute(:attribute_members) || {}).each do |name, option| klass = option[:is_a] value = get_attribute(name) case klass when :string, :number, :boolean, :array, :hash, :datetime hash[name] = value when :collection hash[name] = value.map(&:to_hash) else if value hash[name] = value.to_hash else hash[name] = nil end end end hash end |