Module: Proxima::Serialization

Included in:
Model
Defined in:
lib/proxima/serialization.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



135
136
137
# File 'lib/proxima/serialization.rb', line 135

def self.included base
  base.extend ClassMethods
end

Instance Method Details

#as_json(opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/proxima/serialization.rb', line 41

def as_json(opts = {})
  hash = self.serializable_hash opts

  json = {}
  self.class.attributes.each do |attribute, params|
    next if (
      !opts.key?(:include_clean) && !send("#{attribute}_changed?") ||
      !opts.key?(:include_nil)   && hash[attribute.to_s] == nil
    )

    json_path = params[:json_path]
    value     = hash[attribute.to_s]

    next if value.nil?

    if params[:klass]
      begin
        value = Proxima.type_to_json params[:klass], value
      rescue Exception => e
        raise "Cannot convert value \"#{value}\" for attribute \"#{attribute}\" from type" +
          " #{params[:klass].name}: #{e.message}"
      end
    end

    if opts.key? :flatten
      json[json_path] = value
      next
    end

    json_path_chunks     = json_path.split '.'
    last_json_path_chunk = json_path_chunks.pop
    json_ctx             = json
    for json_path_chunk in json_path_chunks
      json_ctx[json_path_chunk] = {} unless json_ctx[json_path_chunk].is_a?(Hash)
      json_ctx = json_ctx[json_path_chunk]
    end
    json_ctx[last_json_path_chunk] = value
  end

  root = if opts.key? :root
    opts[:root]
  else
    self.include_root_in_json
  end

  if root
    root = self.class.model_name.element if root == true
    { root => json }
  else
    json
  end
end

#from_json(json, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/proxima/serialization.rb', line 6

def from_json(json, opts = {})
  json = ActiveSupport::JSON.decode(json) if json.is_a?(String)
  json = json.values.first if opts[:include_root] || self.include_root_in_json
  json = json.first        if opts[:single_model_from_array] && json.is_a?(Array)
  hash = {}

  self.class.attributes.each do |attribute, params|
    json_path        = params[:json_path]
    json_path_chunks = json_path.split '.'
    json_ctx         = json
    for json_path_chunk in json_path_chunks
      json_ctx = json_ctx[json_path_chunk]
      break if json_ctx.nil?
    end
    value = json_ctx

    next if value.nil?

    if params[:klass]
      begin
        value = Proxima.type_from_json params[:klass], value
      rescue Exception => e
        raise "Cannot convert value \"#{value}\" for attribute \"#{attribute}\" to type" +
          " #{params[:klass].name}: #{e.message}"
      end
    end

    hash[attribute] = value
  end

  self.attributes = hash
  self.new_record = opts[:new_record] if opts[:new_record] != nil
  self
end

#to_hObject



94
95
96
97
98
99
100
# File 'lib/proxima/serialization.rb', line 94

def to_h
  hash = {}
  self.class.attributes.each do |attribute, params|
    hash[attribute] = self.send attribute
  end
  hash
end