Class: Daifuku::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/daifuku/models.rb

Constant Summary collapse

AVAILABLE_BUILTIN_TYPES =
%w(
  smallint
  integer
  bigint
  real
  double
  boolean
  string
  date
  timestamp
  timestamptz
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, optional, custom, str_length = nil) ⇒ Type

Returns a new instance of Type.



129
130
131
132
133
134
135
136
137
# File 'lib/daifuku/models.rb', line 129

def initialize(name, optional, custom, str_length = nil)
  @name = name
  @is_optional = optional
  @is_custom = custom
  @str_length = str_length
  unless custom
    raise "Invalid built-in type #{name}" unless AVAILABLE_BUILTIN_TYPES.include?(name)
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns String.

Returns:

  • String



123
124
125
# File 'lib/daifuku/models.rb', line 123

def name
  @name
end

#str_lengthObject (readonly)

Length for built-in String types This parameter returns nil when type is not String

Returns:

  • Integer?



127
128
129
# File 'lib/daifuku/models.rb', line 127

def str_length
  @str_length
end

Class Method Details

.parse(descriptor) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/daifuku/models.rb', line 150

def self.parse(descriptor)
  type_name, str_length = descriptor.split
  is_optional = type_name.end_with?('?')
  type_without_optional = type_name.gsub(/\?$/, '')
  custom = !type_name.start_with?('!')
  if custom
    raise "Doesn't support str_length for custom types #{type_name}" unless str_length.nil?
    Type.new(type_without_optional, is_optional, custom, nil)
  else
    built_in_type_name = type_without_optional.gsub(/^!/, '')
    raise "type_name '#{built_in_type_name}' is not allowed" unless AVAILABLE_BUILTIN_TYPES.include?(built_in_type_name)
    raise "length for '#{built_in_type_name}' is not supported" if built_in_type_name != 'string' && !str_length.nil?

    Type.new(built_in_type_name, is_optional, custom, str_length&.to_i)
  end
end

Instance Method Details

#custom?Boolean

Boolean value which indicates whether this type is user defined types or not.

Returns:

  • (Boolean)

    Boolean



146
147
148
# File 'lib/daifuku/models.rb', line 146

def custom?
  @is_custom
end

#dumpObject



167
168
169
# File 'lib/daifuku/models.rb', line 167

def dump
  {'name' => name, 'optional' => optional?, 'is_custom' => custom?, 'str_length' => str_length}
end

#optional?Boolean

Returns Boolean.

Returns:

  • (Boolean)

    Boolean



140
141
142
# File 'lib/daifuku/models.rb', line 140

def optional?
  @is_optional
end