Module: JsonSchematize::FieldTransformations

Included in:
Field
Defined in:
lib/json_schematize/field_transformations.rb

Constant Summary collapse

DEFAULT_CONVERTERS =
{
  Float => ->(val) { val.to_f },
  Integer => ->(val) { val.to_i },
  String => ->(val) { val.to_s },
  Symbol => ->(val) { val.to_sym },
}

Instance Method Summary collapse

Instance Method Details

#transform_converter_type!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/json_schematize/field_transformations.rb', line 16

def transform_converter_type!
  return unless @converter.nil?

  # validations have already happened -- We know @acceptable types is a single klass

  @converter = DEFAULT_CONVERTERS[@acceptable_types[0]]
  if @converter.nil?
    @converter = Proc.new do |val|
      if @acceptable_types[0] < JsonSchematize::Generator && @acceptable_types[0] === val
        val
      else
        @acceptable_types[0].new(val)
      end
    end
  end
end

#transform_dig_type!Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/json_schematize/field_transformations.rb', line 33

def transform_dig_type!
  case @dig_type
  when JsonSchematize::Field::DIG_SYMBOL
    @dig = @dig.map(&:to_sym)
  when JsonSchematize::Field::DIG_STRING
    @dig = @dig.map(&:to_s)
  when JsonSchematize::Field::DIG_NONE
    @dig
  end
end

#transformations!Object



11
12
13
14
# File 'lib/json_schematize/field_transformations.rb', line 11

def transformations!
  transform_converter_type!
  transform_dig_type!
end