Class: Fluent::Json::Schema::Terms::Reflect

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/fluent/json/schema/terms/reflect.rb

Constant Summary collapse

MAPPINGS =
{
  string:   Fluent::Json::Schema::Terms::Str,
  datetime: Fluent::Json::Schema::Terms::Date,
  date:     Fluent::Json::Schema::Terms::Date,
  time:     Fluent::Json::Schema::Terms::Date,
  integer:  Fluent::Json::Schema::Terms::Int,
  decimal:  Fluent::Json::Schema::Terms::Num,
  boolean:  Fluent::Json::Schema::Terms::Bool
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, klass) ⇒ Reflect

Returns a new instance of Reflect.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fluent/json/schema/terms/reflect.rb', line 18

def initialize(name, klass)
  @klass = klass
  # TODO, fk
  
  column = klass.columns_hash[name.to_s]
  raise "Column with name '#{name}' not found in '#{klass.name}'" if column.nil?

  enum = klass.defined_enums[name.to_s]
  instance_klass = MAPPINGS[column.type.to_sym]
  instance_klass = MAPPINGS[:string] if enum.present? and enum.keys[0].is_a?(String)

  raise "Unsupported type '#{column.type}' for '#{name}'" if instance_klass.nil?
  
  @instance = instance_klass.new(name)
  @instance.set(enum: enum.keys) if enum.present?
  @instance.mandate if not column.null

  super(@instance)
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



16
17
18
# File 'lib/fluent/json/schema/terms/reflect.rb', line 16

def instance
  @instance
end

#klassObject (readonly)

Returns the value of attribute klass.



16
17
18
# File 'lib/fluent/json/schema/terms/reflect.rb', line 16

def klass
  @klass
end