Class: Bumblebee::Mutator

Inherits:
Object
  • Object
show all
Includes:
Types
Defined in:
lib/bumblebee/mutator.rb

Overview

A Mutator is a composition of a converter with hash value setting. It can be a straight converter, or it can be new types which are not directly defined as ‘converters.’

Defined Under Namespace

Modules: Types

Constant Summary

Constants included from Types

Types::IGNORE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg, resolver: Objectable.resolver) ⇒ Mutator

Returns a new instance of Mutator.



21
22
23
24
25
26
27
# File 'lib/bumblebee/mutator.rb', line 21

def initialize(arg, resolver: Objectable.resolver)
  @resolver  = resolver
  @converter = arg.nil? || mutator?(arg) ? NullConverter.new : SimpleConverter.new(arg)
  @type      = mutator?(arg) ? Types.const_get(arg.to_s.upcase.to_sym) : nil

  freeze
end

Instance Attribute Details

#converterObject (readonly)

Returns the value of attribute converter.



19
20
21
# File 'lib/bumblebee/mutator.rb', line 19

def converter
  @converter
end

#typeObject (readonly)

Returns the value of attribute type.



19
20
21
# File 'lib/bumblebee/mutator.rb', line 19

def type
  @type
end

Instance Method Details

#set(object, key, val) ⇒ Object



29
30
31
32
33
# File 'lib/bumblebee/mutator.rb', line 29

def set(object, key, val)
  return object if ignore?

  resolver.set(object, key, converter.convert(val))
end