Module: ParamsReady::Marshaller::DefinitionModule

Defined in:
lib/params_ready/marshaller/definition_module.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#marshallersObject (readonly)

Returns the value of attribute marshallers.



13
14
15
# File 'lib/params_ready/marshaller/definition_module.rb', line 13

def marshallers
  @marshallers
end

Class Method Details

.[](collection) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/params_ready/marshaller/definition_module.rb', line 4

def self.[](collection)
  mod = Module.new
  mod.include self
  mod.define_method :class_marshallers do
    collection
  end
  mod
end

Instance Method Details

#finishObject



48
49
50
51
52
53
54
55
# File 'lib/params_ready/marshaller/definition_module.rb', line 48

def finish
  unless @marshallers.default?
    if class_marshallers.default?
      @marshallers.default = class_marshallers.default
    end
  end
  super
end

#freezeObject



57
58
59
60
# File 'lib/params_ready/marshaller/definition_module.rb', line 57

def freeze
  @marshallers.freeze
  super
end

#initialize(*args, marshaller: nil, **options) ⇒ Object



15
16
17
18
19
20
# File 'lib/params_ready/marshaller/definition_module.rb', line 15

def initialize(*args, marshaller: nil, **options)
  @marshallers = class_marshallers.instance_collection
  set_marshaller(**marshaller) unless marshaller.nil?

  super *args, **options
end

#marshal(parameter, intent, **opts) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/params_ready/marshaller/definition_module.rb', line 36

def marshal(parameter, intent, **opts)
  if intent.marshal?(name_for_formatter)
    @marshallers.marshal(parameter, intent, **opts)
  else
    @marshallers.marshal_canonical(parameter, intent, **opts)
  end
end

#set_marshaller(to: nil, using: nil, **opts) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/params_ready/marshaller/definition_module.rb', line 22

def set_marshaller(to: nil, using: nil, **opts)
  if using.is_a? Symbol
    raise ParamsReadyError, "Expected ':to' argument to be nil, got #{to.class.name}" unless to.nil?
    default_class, instance = class_marshallers.build_instance(using, **opts)
    @marshallers.add_instance(default_class, instance)
    @marshallers.default!(default_class)
  elsif using.nil?
    @marshallers.default!(to)
  else
    @marshallers.add_instance(to, using)
    @marshallers.default!(to)
  end
end

#try_canonicalize(input, context, validator = nil, **opts) ⇒ Object



44
45
46
# File 'lib/params_ready/marshaller/definition_module.rb', line 44

def try_canonicalize(input, context, validator = nil, **opts)
  @marshallers.canonicalize(self, input, context, validator, **opts)
end