Module: SmoothOperator::Serialization

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

Instance Method Summary collapse

Instance Method Details

#attributesObject

alias :attributes :to_hash



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

def attributes; to_hash; end

#read_attribute_for_serialization(attribute) ⇒ Object



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

def read_attribute_for_serialization(attribute)
  send(attribute)
end

#serializable_hash(options = nil) ⇒ Object



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

def serializable_hash(options = nil)
  hash = {}
  options ||= {}

  attribute_names(options).each do |attribute_name|
    hash[attribute_name] = read_attribute_for_hashing(attribute_name, options)
  end

  method_names(options).each do |method_name|
    hash[method_name.to_s] = send(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



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

def to_json(options = nil)
  require 'json' unless defined? JSON
  
  JSON(serializable_hash(options) || {})
end