class DirtySeed::Attribute

Represents an Active Record attribute

Constants

TYPE_SYMMETRY

Attributes

enum[W]
model[RW]

@!method initialize(column) @param column [ActiveRecord::ConnectionAdapters::Column] @return [DirtySeed::Attribute]

validators[W]

Public Instance Methods

array?() click to toggle source

Is attribute an array? @return [Boolean] @note When attribute is serialized as array, it raises an error

if value is not an array -> use this to define if it is an array
# File lib/dirty_seed/attribute.rb, line 41
def array?
  return @array unless @array.nil?
  return true if sql_type_metadata.type.to_s.include? '[]'

  begin
    model&.new(name => '')
    # it does not raise error -> it is not a serialized array
    false
  rescue ActiveRecord::SerializationTypeMismatch
    # it raises an error -> it is (certainly) a serialized array
    true
  rescue StandardError
    # if any other error raises, return false
    false
  end
end
assigner() click to toggle source

Returns the attribute assigner @return [DirtySeed::Assigners::Assigner]

# File lib/dirty_seed/attribute.rb, line 26
def assigner
  @assigner ||= DirtySeed::Assigners::Dispatcher.new(self)
end
enum() click to toggle source

Returns enum @return [Array<Object>] array of options

# File lib/dirty_seed/attribute.rb, line 69
def enum
  @enum ||= model&.defined_enums&.dig(name)
end
type() click to toggle source

Returns attribute type @return [Symbol]

# File lib/dirty_seed/attribute.rb, line 32
def type
  sql_type = sql_type_metadata.type.to_s.gsub('[]', '').to_sym
  @type ||= TYPE_SYMMETRY[sql_type] || sql_type
end
validators() click to toggle source

Returns validators @return [Array<Object>] array of validators

# File lib/dirty_seed/attribute.rb, line 60
def validators
  @validators ||=
    model&.validators&.select do |validator|
      validator.attributes.include? name.to_sym
    end
end