Class: Ser::Ializer

Inherits:
Object
  • Object
show all
Defined in:
lib/ser/ializer.rb,
lib/ser/ializer/field.rb

Overview

rubocop:disable Metrics/ClassLength

Direct Known Subclasses

De::Ser::Ializer

Defined Under Namespace

Classes: Field

Constant Summary collapse

@@method_registry =

rubocop:disable Style/ClassVars

{}

Class Method Summary collapse

Class Method Details

.attribute_namesObject



99
100
101
# File 'lib/ser/ializer.rb', line 99

def attribute_names
  attributes.values.map(&:name)
end

.attributesObject



103
104
105
106
107
108
109
110
# File 'lib/ser/ializer.rb', line 103

def attributes
  @attributes ||=
    if equal? Ser::Ializer
      ActiveSupport::HashWithIndifferentAccess.new
    else
      superclass.attributes.dup
    end
end

.configObject



11
12
13
14
15
16
17
18
# File 'lib/ser/ializer.rb', line 11

def config
  @config ||=
    if equal? Ser::Ializer
      ::Ializer.config
    else
      superclass.config
    end
end

.deser_typesObject

Maps a registered deser to the value type that it serializes



95
96
97
# File 'lib/ser/ializer.rb', line 95

def deser_types
  @deser_types ||= {}
end

.nested(name, options = {}, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/ser/ializer.rb', line 39

def nested(name, options = {}, &block)
  if block
    deser = create_anon_class
    deser.class_eval(&block)
    options[:deser] = deser
  end

  add_attribute(Field.new(name, options, config))
end

.property(name, options = {}, &block) ⇒ Object

Public DSL



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ser/ializer.rb', line 27

def property(name, options = {}, &block)
  return add_attribute(Field.new(name, options, config, &block)) if options[:deser]

  return default(name, options, &block) unless options[:type]

  meth = lookup_method(options[:type])

  return meth.call(name, options, &block) if meth

  default(name, options, &block)
end

.register(method_name, deser, *matchers) ⇒ Object

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ser/ializer.rb', line 68

def register(method_name, deser, *matchers)
  raise ArgumentError, 'register should only be called on the Ser::Ializer class' unless self == Ser::Ializer

  define_singleton_method(method_name) do |name, options = {}, &block|
    options[:deser] = deser
    add_attribute Field.new(name, options, config, &block)
  end

  matchers.each do |matcher|
    method_registry[matcher.to_s.to_sym] = method_name
  end

  deser_types[deser.to_s] = method_name.capitalize
end

.register_default(deser) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/ser/ializer.rb', line 83

def register_default(deser)
  define_singleton_method('default') do |name, options = {}, &block|
    raise ArgumentError, warning_message(name) if config.raise_on_default?

    puts warning_message(name) if config.warn_on_default?

    options[:deser] = deser
    add_attribute Field.new(name, options, config, &block)
  end
end

.serialize(object, context = nil) ⇒ Object

End Public DSL



56
57
58
59
60
61
62
# File 'lib/ser/ializer.rb', line 56

def serialize(object, context = nil)
  return serialize_one(object, context) unless valid_enumerable?(object)

  return [] if object.empty?

  object.map { |o| serialize_one(o, context) }
end

.serialize_json(object, context = nil) ⇒ Object



64
65
66
# File 'lib/ser/ializer.rb', line 64

def serialize_json(object, context = nil)
  MultiJson.dump(serialize(object, context))
end

.setup {|@config| ... } ⇒ Object

Yields:



20
21
22
23
24
# File 'lib/ser/ializer.rb', line 20

def setup
  @config = config.dup

  yield @config
end

.with(deser) ⇒ Object



49
50
51
52
53
# File 'lib/ser/ializer.rb', line 49

def with(deser)
  deser.attributes.each_value do |field|
    add_composed_attribute(field, deser)
  end
end