class Schemaless::Field
Constants
- VALID_OPTS
Attributes
default[RW]
index[RW]
name[RW]
opts[RW]
type[RW]
Public Class Methods
new(name, type, opts = {})
click to toggle source
Field
- name, type, opts: [:default, :null, :unique]
# File lib/schemaless/field.rb, line 12 def initialize(name, type, opts = {}) fail InvalidArgument, 'opts must be a hash' unless opts.is_a?(Hash) @name = name.to_s @type = map_field type @opts = opts.select { |_k, v| v.present? } @opts[:null] = true unless @opts[:null].present? @opts[:limit] = 255 unless @opts[:limit].present? || @type != :string end
Public Instance Methods
==(other)
click to toggle source
# File lib/schemaless/field.rb, line 21 def ==(other) name == other.name # && type == other.type end
add!(table)
click to toggle source
Add Fields
# File lib/schemaless/field.rb, line 40 def add!(table) puts "++ Adding '#{self}' to '#{table}'" return if Schemaless.sandbox ::ActiveRecord::Migration.add_column(table.name, name, type, opts) end
change!(table)
click to toggle source
Change Fields
# File lib/schemaless/field.rb, line 58 def change!(table) return if Schemaless.sandbox # ::ActiveRecord::Migration.change_column(table, name) # ::ActiveRecord::Migration.change_column_null(table, name) # ::ActiveRecord::Migration.change_column_default(table, name) ::ActiveRecord::Migration.change_column(table.name, name, type, opts) end
map_field(field)
click to toggle source
binary # boolean date # datetime time # timestamp integer # primary_key # references decimal # float string # text hstore json array cidr_address ip_address mac_address
# File lib/schemaless/field.rb, line 80 def map_field(field) # rubocop:disable Metrics/MethodLength return field if field.is_a?(Symbol) case field.to_s when /Integer|Fixnum|Numeric/ then :integer when /BigDecimal|Rational/ then :decimal when /Float/ then :float when /DateTime/ then :datetime when /Date/ then :date when /Time/ then :timestamp else :string end end
opts_text()
click to toggle source
# File lib/schemaless/field.rb, line 28 def opts_text txt = opts.map { |k, v| "#{k}: #{v}" }.join(', ') txt.empty? ? '' : ", #{txt}" end
reference?()
click to toggle source
# File lib/schemaless/field.rb, line 25 def reference? end
remove!(table)
click to toggle source
Delete Fields
# File lib/schemaless/field.rb, line 49 def remove!(table) puts "-- Removing '#{self}' from '#{table}'" return if Schemaless.sandbox ::ActiveRecord::Migration.remove_column(table.name, name) end
to_s()
click to toggle source
# File lib/schemaless/field.rb, line 33 def to_s name end