Module: SmoothOperator::Serialization

Included in:
OpenStruct
Defined in:
lib/smooth_operator/serialization.rb

Defined Under Namespace

Modules: HelperMethods

Instance Method Summary collapse

Instance Method Details

#attributesObject



9
# File 'lib/smooth_operator/serialization.rb', line 9

def attributes; to_hash; end

#read_attribute_for_serialization(attribute) ⇒ Object



17
18
19
# File 'lib/smooth_operator/serialization.rb', line 17

def read_attribute_for_serialization(attribute)
  send(attribute)
end

#serializable_hash(options = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/smooth_operator/serialization.rb', line 21

def serializable_hash(options = nil)
  hash = {}
  options ||= {}
  method_names = HelperMethods.method_names(self, options)
  attribute_names = HelperMethods.attribute_names(self, options)

  attribute_names.each do |attribute_name|
    attribute_name, attribute_value = HelperMethods
      .serialize_normal_or_rails_way(self, attribute_name, options)

    hash[attribute_name] = attribute_value
  end

  method_names.each do |method_name|
    hash[method_name.to_s] = HelperMethods.serialize_normal_attribute(send(method_name), method_name, options[method_name])
  end

  hash
end

#to_hash(options = nil) ⇒ Object



5
6
7
# File 'lib/smooth_operator/serialization.rb', line 5

def to_hash(options = nil)
  Helpers.symbolyze_keys(serializable_hash(options) || {})
end

#to_json(options = nil) ⇒ Object



11
12
13
14
15
# File 'lib/smooth_operator/serialization.rb', line 11

def to_json(options = nil)
  require 'json' unless defined? JSON

  JSON(serializable_hash(options) || {})
end