Class: Schema2type::SchemaConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/schema2type/schema_converter.rb

Constant Summary collapse

TYPE_STRING =
'string'.freeze
TYPE_NUMBER =
'number'.freeze
TYPE_BOOLEAN =
'boolean'.freeze
TYPE_DATE =
'Date'.freeze
COLUMN_METHODS =
YAML.load_file(File.expand_path(__dir__) + '/conversion_table.yml').to_a

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name:, snake_case: false) ⇒ SchemaConverter

Returns a new instance of SchemaConverter.



24
25
26
27
28
# File 'lib/schema2type/schema_converter.rb', line 24

def initialize(table_name:, snake_case: false)
  @property_lines = []
  @table_name = table_name.singularize.camelize
  @snake_case = snake_case
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject



34
35
36
# File 'lib/schema2type/schema_converter.rb', line 34

def method_missing(*)
  # To exclude unnecessary methods
end

Instance Attribute Details

#property_linesObject (readonly)

Returns the value of attribute property_lines.



6
7
8
# File 'lib/schema2type/schema_converter.rb', line 6

def property_lines
  @property_lines
end

#snake_caseObject (readonly)

Returns the value of attribute snake_case.



6
7
8
# File 'lib/schema2type/schema_converter.rb', line 6

def snake_case
  @snake_case
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



6
7
8
# File 'lib/schema2type/schema_converter.rb', line 6

def table_name
  @table_name
end

Class Method Details

.define_convert_methods(methods) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/schema2type/schema_converter.rb', line 14

def self.define_convert_methods(methods)
  methods.each do |m|
    define_method(m[0]) do |name, *options|
      push_property_line name: name, type: m[1], options: options
    end
  end
end

Instance Method Details

#resultObject



30
31
32
# File 'lib/schema2type/schema_converter.rb', line 30

def result
  ["type #{table_name} = {", property_lines, "}\n"].flatten
end