Class: DirtySeed::Attribute

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/dirty_seed/attribute.rb

Overview

Represents an Active Record attribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column) ⇒ DirtySeed::Attribute

Parameters:

  • column (ActiveRecord::ConnectionAdapters::Column)


10
# File 'lib/dirty_seed/attribute.rb', line 10

attr_accessor :model

Instance Attribute Details

#enumArray<Object>

Returns enum

Returns:

  • (Array<Object>)

    array of options



69
70
71
# File 'lib/dirty_seed/attribute.rb', line 69

def enum
  @enum ||= model&.defined_enums&.dig(name)
end

#modelDirtySeed::Attribute

Parameters:

  • column (ActiveRecord::ConnectionAdapters::Column)

Returns:



10
11
12
# File 'lib/dirty_seed/attribute.rb', line 10

def model
  @model
end

#validatorsArray<Object>

Returns validators

Returns:

  • (Array<Object>)

    array of validators



60
61
62
63
64
65
# File 'lib/dirty_seed/attribute.rb', line 60

def validators
  @validators ||=
    model&.validators&.select do |validator|
      validator.attributes.include? name.to_sym
    end
end

Instance Method Details

#array?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

Is attribute an array?

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dirty_seed/attribute.rb', line 41

def array?
  return @array unless @array.nil?
  return true if .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

#assignerDirtySeed::Assigners::Assigner

Returns the attribute assigner



26
27
28
# File 'lib/dirty_seed/attribute.rb', line 26

def assigner
  @assigner ||= DirtySeed::Assigners::Dispatcher.new(self)
end

#typeSymbol

Returns attribute type

Returns:

  • (Symbol)


32
33
34
35
# File 'lib/dirty_seed/attribute.rb', line 32

def type
  sql_type = .type.to_s.gsub('[]', '').to_sym
  @type ||= TYPE_SYMMETRY[sql_type] || sql_type
end