Class: Algebrick::Serializers::StrictToHash

Inherits:
AbstractToHash show all
Defined in:
lib/algebrick/serializers/strict_to_hash.rb,
lib/algebrick/serializers2/strict_to_hash.rb

Instance Attribute Summary

Attributes inherited from AbstractToHash

#fields_key, #type_key

Instance Method Summary (collapse)

Methods inherited from AbstractToHash

#initialize

Methods included from TypeCheck

#Child!, #Child?, #Match!, #Match?, #Type!, #Type?, error

Constructor Details

This class inherits a constructor from Algebrick::Serializers::AbstractToHash

Instance Method Details

- (Object) constantize(camel_cased_word)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/algebrick/serializers/strict_to_hash.rb', line 40

def constantize(camel_cased_word)
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?

  parameter = nil
  names.last.tap do |last|
    name, parameter = last.split /\[|\]/
    last.replace name
  end

  constant = Object
  names.each do |name|
    constant = if constant.const_defined?(name)
                 constant.const_get(name)
               else
                 constant.const_missing(name)
               end
  end
  constant = constant[constantize(parameter)] if parameter
  constant
end

- (Object) dump(object, options = {})



29
30
31
32
33
34
35
36
37
38
# File 'lib/algebrick/serializers/strict_to_hash.rb', line 29

def dump(object, options = {})
  case object
  when Value
    generate_value object, options
  when Numeric, String, ::Array, ::Hash, Symbol, TrueClass, FalseClass, NilClass
    object
  else
    generate_other(object, options)
  end
end

- (Object) generate_other(object, options = {}) (private)



111
112
113
114
115
116
117
118
119
120
# File 'lib/algebrick/serializers/strict_to_hash.rb', line 111

def generate_other(object, options = {})
  case
  when object.respond_to?(:to_h)
    object.to_h
  when object.respond_to?(:to_hash)
    object.to_hash
  else
    raise "do not know how to convert (#{object.class}) #{object}"
  end
end

- (Object) generate_value(value, options) (private)



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/algebrick/serializers/strict_to_hash.rb', line 91

def generate_value(value, options)
  { type_key => value.type.name }.
      update(case value
             when Atom
               {}
             when ProductConstructors::Basic
               { fields_key => value.fields.map { |v| dump v, options } }
             when ProductConstructors::Named
               value.type.field_names.inject({}) do |h, name|
                 h.update name => dump(value[name], options)
               end
             else
               raise
             end)
end

- (Object) load(data, options = {})



18
19
20
21
22
23
24
25
26
27
# File 'lib/algebrick/serializers/strict_to_hash.rb', line 18

def load(data, options = {})
  case data
  when ::Hash
    parse_value(data, options)
  when Numeric, String, ::Array, Symbol, TrueClass, FalseClass, NilClass
    data
  else
    parse_other(data, options)
  end
end

- (Object) parse_other(other, options = {}) (private)



107
108
109
# File 'lib/algebrick/serializers/strict_to_hash.rb', line 107

def parse_other(other, options = {})
  other
end

- (Object) parse_value(value, options) (private)



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
# File 'lib/algebrick/serializers/strict_to_hash.rb', line 64

def parse_value(value, options)
  type_name = value[type_key] || value[type_key.to_s]
  if type_name
    type = constantize(type_name)

    fields = value[fields_key] || value[fields_key.to_s] ||
        value.dup.tap { |h| h.delete type_key; h.delete type_key.to_s }
    Type! fields, Hash, Array

    if type.is_a? Atom
      type
    else
      case fields
      when Array
        type[*fields.map { |value| load value, options }]
      when Hash
        type[fields.inject({}) do |h, (name, value)|
          raise ArgumentError unless type.field_names.map(&:to_s).include? name.to_s
          h.update name.to_sym => load(value, options)
        end]
      end
    end
  else
    value
  end
end