Module: SerialAttr::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/serial_attr/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#attributes_to_serializeArray

Generates the list of attribute names for serialization

Returns:

  • (Array)

    list of attribute names to serialize



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_hashHash

Serialize attributes listed in #attributes_to_serialize to a ruby hash

Returns:

  • (Hash)

    serialized attributes



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_jsonString

Serialize attributes listed in #attributes_to_serialize to JSON

Returns:

  • (String)

    attributes serialized to JSON



34
35
36
# File 'lib/serial_attr/model.rb', line 34

def serialized_to_json
  self.serialized_to_hash.to_json
end