Class: Crudboy::Column

Inherits:
Object show all
Defined in:
lib/crudboy/column.rb

Constant Summary collapse

JAVA_TYPES =
{
  "varchar" => 'String',
  "char" => 'String',
  "text" => 'String',
  "longtext" => 'String',
  "int" => 'Integer',
  "smallint" => 'Integer',
  "bigint" => 'Long',
  "tinyint" => 'Byte',
  "double" => 'Double',
  "date" => 'LocalDate',
  "datetime" => 'LocalDateTime',
  "timestamp" => 'LocalDateTime',
  "time" => 'LocalTime',
  "blob" => 'byte[]',
  "decimal" => 'BigDecimal'
}
JDBC_TYPES =
{
  "varchar" => 'VARCHAR',
  "char" => 'CHAR',
  "text" => 'VARCHAR',
  "longtext" => 'VARCHAR',
  "int" => 'INTEGER',
  "smallint" => 'INTEGER',
  "bigint" => 'BIGINT',
  "tinyint" => 'TINYINT',
  "double" => 'DOUBLE',
  "date" => 'TIMESTAMP',
  "datetime" => 'TIMESTAMP',
  "timestamp" => 'TIMESTAMP',
  "time" => 'TIME',
  "blob" => 'BLOB',
  "decimal" => 'DECIMAL'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, primary) ⇒ Column

Returns a new instance of Column.



42
43
44
45
# File 'lib/crudboy/column.rb', line 42

def initialize(column, primary)
  @active_record_column = column
  @primary = primary
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **options, &block) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/crudboy/column.rb', line 90

def method_missing(method, *args, **options, &block)
  if active_record_column.respond_to?(method)
    active_record_column.send(method, *args, **options, &block)
  else
    super
  end
end

Instance Attribute Details

#active_record_columnObject

Returns the value of attribute active_record_column.



40
41
42
# File 'lib/crudboy/column.rb', line 40

def active_record_column
  @active_record_column
end

#primaryObject

Returns the value of attribute primary.



40
41
42
# File 'lib/crudboy/column.rb', line 40

def primary
  @primary
end

Instance Method Details

#java_docObject



47
48
49
50
51
52
53
# File 'lib/crudboy/column.rb', line 47

def java_doc
  <<-EOF.lstrip.chomp
  /**
 * #{comment}
 */
  EOF
end

#java_typeObject



79
80
81
82
83
# File 'lib/crudboy/column.rb', line 79

def java_type
  return 'Boolean' if sql_type == 'tinyint(1)'
  raw_type = sql_type.scan(/^\w+/).first
  JAVA_TYPES[raw_type]
end

#jdbc_typeObject



85
86
87
88
# File 'lib/crudboy/column.rb', line 85

def jdbc_type
  raw_type = sql_type.scan(/^\w+/).first
  JDBC_TYPES[raw_type]
end

#lower_camel_nameObject



71
72
73
# File 'lib/crudboy/column.rb', line 71

def lower_camel_name
  name.camelcase(:lower)
end

#mybatis_equationObject



59
60
61
# File 'lib/crudboy/column.rb', line 59

def mybatis_equation
  format('`%s` = %s', name, mybatis_value_expression)
end

#mybatis_result_mapObject



63
64
65
66
67
68
69
# File 'lib/crudboy/column.rb', line 63

def mybatis_result_map
  if @primary
    format('<id column="%s" jdbcType="%s" property="%s" />', name, jdbc_type, lower_camel_name)
  else
    format('<result column="%s" jdbcType="%s" property="%s" />', name, jdbc_type, lower_camel_name)
  end
end

#mybatis_value_expressionObject



55
56
57
# File 'lib/crudboy/column.rb', line 55

def mybatis_value_expression
  format('#{%s,jdbcType=%s}', lower_camel_name, jdbc_type)
end

#upper_camel_nameObject



75
76
77
# File 'lib/crudboy/column.rb', line 75

def upper_camel_name
  name.camelcase(:upper)
end