Module: SerialAttr::Model
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/serial_attr/model.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#attributes_to_serialize ⇒ Array
Generates the list of attribute names for serialization.
-
#serialized_to_hash ⇒ Hash
Serialize attributes listed in #attributes_to_serialize to a ruby hash.
-
#serialized_to_json ⇒ String
Serialize attributes listed in #attributes_to_serialize to JSON.
Instance Method Details
#attributes_to_serialize ⇒ Array
Generates the list of attribute names for serialization
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/serial_attr/model.rb', line 10 def attributes_to_serialize attrs = [] attrs |= self.attributes.keys.collect(&:to_sym) if respond_to?(:attributes) attrs |= self.class.serial_attr_whitelist attrs -= self.class.serial_attr_blacklist attrs.sort end |
#serialized_to_hash ⇒ Hash
Serialize attributes listed in #attributes_to_serialize to a ruby hash
24 25 26 27 28 29 |
# File 'lib/serial_attr/model.rb', line 24 def serialized_to_hash serialized = HashWithIndifferentAccess.new self.attributes_to_serialize.each { |key| serialized[key] = send(key) } serialized end |
#serialized_to_json ⇒ String
Serialize attributes listed in #attributes_to_serialize to JSON
34 35 36 |
# File 'lib/serial_attr/model.rb', line 34 def serialized_to_json self.serialized_to_hash.to_json end |