Class: TableStructure::Schema::Definition::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/table_structure/schema/definition/compiler.rb

Constant Summary collapse

DEFAULT_ATTRS =
{
  name: nil,
  key: nil,
  value: nil,
  size: nil,
  omitted: false
}.freeze
DEFAULT_SIZE =
1

Instance Method Summary collapse

Constructor Details

#initialize(name, definitions, options) ⇒ Compiler

Returns a new instance of Compiler.



17
18
19
20
21
# File 'lib/table_structure/schema/definition/compiler.rb', line 17

def initialize(name, definitions, options)
  @name = name
  @definitions = definitions
  @options = options
end

Instance Method Details

#compile(context = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/table_structure/schema/definition/compiler.rb', line 23

def compile(context = nil)
  @definitions
    .map { |definition| Utils.evaluate_callable(definition, context) }
    .map.with_index do |definition, i|
      validator = Validator.new(@name, i)

      [definition]
        .flatten
        .map do |definition|
          if definition.is_a?(Hash)
            definition = DEFAULT_ATTRS.merge(definition)
            omitted = definition.delete(:omitted)
            next if Utils.evaluate_callable(omitted, context)

            definition = evaluate_attrs(definition, context)
            validator.validate(definition)
            definition[:size] = determine_size(definition)
            definition
          elsif Utils.schema_instance?(definition)
            definition
          elsif Utils.schema_class?(definition)
            definition.new(context: context)
          elsif definition.nil? && @options[:nil_definitions_ignored]
            next
          else
            raise Error.new('Invalid definition.', @name, i)
          end
        end
    end
    .flatten
    .compact
end