Class: Algebrick::Serializer
- Inherits:
-
Object
show all
- Includes:
- TypeCheck
- Defined in:
- lib/algebrick/serializer.rb
Constant Summary
- TYPE_KEY =
:algebrick_type
- FIELD_KEY =
:algebrick_fields
Instance Method Summary
(collapse)
Methods included from TypeCheck
#Child!, #Child?, #Match!, #Match?, #Type!, #Type?, error
Instance Method Details
- (Object) constantize(camel_cased_word)
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/algebrick/serializer.rb', line 45
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 = {})
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/algebrick/serializer.rb', line 34
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 = {})
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/algebrick/serializer.rb', line 73
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)
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/algebrick/serializer.rb', line 113
def generate_value(value, options)
{ TYPE_KEY => value.type.name }.
update(case value
when Atom
{}
when ProductConstructors::Basic
{ FIELD_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 = {})
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/algebrick/serializer.rb', line 23
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 = {})
69
70
71
|
# File 'lib/algebrick/serializer.rb', line 69
def parse_other(other, options = {})
other
end
|
- (Object) parse_value(value, options)
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/algebrick/serializer.rb', line 86
def parse_value(value, options)
type_name = value[TYPE_KEY] || value[TYPE_KEY.to_s]
if type_name
type = constantize(type_name)
fields = value[FIELD_KEY] || value[FIELD_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
parse_other value, options
end
end
|