Class: Algebrick::Serializer

Inherits:
Object
  • Object
show all
Includes:
TypeCheck
Defined in:
lib/algebrick/serializer.rb

Constant Summary collapse

TYPE_KEY =
:algebrick_type
FIELD_KEY =
:algebrick_fields

Instance Method Summary collapse

Methods included from TypeCheck

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

Instance Method Details

#constantize(camel_cased_word) ⇒ Object



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

#dump(object, options = {}) ⇒ Object



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

#load(data, options = {}) ⇒ Object



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