Class: Schema2type::SchemaConverter

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

Constant Summary collapse

COLUMN_METHODS =
YAML.load_file(File.expand_path(__dir__) + '/conversion_table.yml').to_a
ID_PROPERTY_LINE_TEXT =
"  id: number;".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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



21
22
23
24
25
# File 'lib/schema2type/schema_converter.rb', line 21

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject



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

def method_missing(*)
  # To exclude unnecessary methods
  # TODO: add error handling
end

Instance Attribute Details

#is_snake_caseObject (readonly)

Returns the value of attribute is_snake_case.



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

def is_snake_case
  @is_snake_case
end

#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

#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



11
12
13
14
15
16
17
# File 'lib/schema2type/schema_converter.rb', line 11

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

Instance Method Details

#converted_type_linesObject



27
28
29
# File 'lib/schema2type/schema_converter.rb', line 27

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