Class: DirtySeed::Attribute
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- DirtySeed::Attribute
- Defined in:
- lib/dirty_seed/attribute.rb
Overview
Represents an Active Record attribute
Instance Attribute Summary collapse
-
#enum ⇒ Array<Object>
Returns enum.
- #model ⇒ DirtySeed::Attribute
-
#validators ⇒ Array<Object>
Returns validators.
Instance Method Summary collapse
-
#array? ⇒ Boolean
Is attribute an array?.
-
#assigner ⇒ DirtySeed::Assigners::Assigner
Returns the attribute assigner.
- #initialize(column) ⇒ DirtySeed::Attribute constructor
-
#type ⇒ Symbol
Returns attribute type.
Constructor Details
#initialize(column) ⇒ DirtySeed::Attribute
10 |
# File 'lib/dirty_seed/attribute.rb', line 10 attr_accessor :model |
Instance Attribute Details
#enum ⇒ Array<Object>
Returns enum
69 70 71 |
# File 'lib/dirty_seed/attribute.rb', line 69 def enum @enum ||= model&.defined_enums&.dig(name) end |
#model ⇒ DirtySeed::Attribute
10 11 12 |
# File 'lib/dirty_seed/attribute.rb', line 10 def model @model end |
#validators ⇒ Array<Object>
Returns 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?
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 |
#assigner ⇒ DirtySeed::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 |
#type ⇒ Symbol
Returns attribute type
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 |