Class: Protip::Transformers::PrimitivesTransformer

Inherits:
DelegatingTransformer show all
Defined in:
lib/protip/transformers/primitives_transformer.rb

Overview

Transforms ints, strings, booleans, floats, and bytes to/from their well-known types (for scalars) and Protip repeated types.

Constant Summary collapse

TRUE_VALUES =
[true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON']
FALSE_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF']

Instance Method Summary collapse

Methods inherited from DelegatingTransformer

#merge!, #to_message, #to_object

Methods included from Protip::Transformer

#to_message, #to_object

Constructor Details

#initializePrimitivesTransformer

Returns a new instance of PrimitivesTransformer.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/protip/transformers/primitives_transformer.rb', line 13

def initialize
  super
  {
    Int64:   :to_i.to_proc,
    Int32:   :to_i.to_proc,
    UInt64:  :to_i.to_proc,
    UInt32:  :to_i.to_proc,
    Double:  :to_f.to_proc,
    Float:   :to_f.to_proc,
    String:  :to_s.to_proc,
    Bool:    ->(object) { to_boolean(object) },
    Bytes:   ->(object) { object },
  }.each do |type, transform|
    self["google.protobuf.#{type}Value"] = ScalarTransformer.new(transform)
    self["protip.messages.Repeated#{type}"] = ArrayTransformer.new(transform)
  end          
end