Module: ModelPack::Serialization

Defined in:
lib/model_pack/serialization.rb

Instance Method Summary collapse

Instance Method Details

#copyObject



62
63
64
# File 'lib/model_pack/serialization.rb', line 62

def copy
  self.class.new(self.serializable_hash)
end

#serializable_attribute_names(attributes, options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/model_pack/serialization.rb', line 4

def serializable_attribute_names(attributes, options)
  names = attributes.keys.sort
  case options
    when Hash
      names &= Array.wrap(options.keys).map(&:to_s)
    when true
      # do nothing
    when false
      names = {}
    when nil
      names = {}
  else
    names &= Array.wrap(options).map(&:to_s)
  end

  names
end

#serializable_hash(options = nil, global = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/model_pack/serialization.rb', line 22

def serializable_hash(options = nil, global = nil)
  serializable_value =
    ->(value, *a) { value.respond_to?(:serializable_hash) ? value.serializable_hash(*a[0...value.method(:serializable_hash).arity]) : value }
  enumerable_value = ->(value, *a) {
    value.is_a?(Hash) ? value.inject({}) { |h, e| h[e.first] = serializable_value[e.last, *a]; h } :
    (value.is_a?(Enumerable) ? value.map{ |v| serializable_value[v, *a] } : serializable_value[value, *a])
  }

  hash = {}
  options = Hash[*options.map{|v| [v, true]}.flatten(2)] if options.is_a?(Array)
  self.class.attribute_names.each do |n|
    opts = options ? options[n] : nil
    empty = options.nil? || options.empty?
    n_hash = "#{n}_hash"
    if (opts || empty) && respond_to?(n_hash)
      value = send(n_hash, *(method(n_hash).arity.zero? ? [] : [opts, global]))
      hash[n] = value if value
      next
    end

    value =  send(n)
    data = case opts
    when Symbol
      value.send(opts)
    when true
      enumerable_value[value, nil, global]
    when false
      nil
    when nil
      # auto detect value type if options nil too
      empty ? enumerable_value[value, nil, global] : nil
    when Hash
      enumerable_value[value, opts, global]
    end

    hash[n] = data unless data.nil?
  end
  hash
end