Class: Katapult::Attribute
- Defined in:
- lib/katapult/elements/attribute.rb
Constant Summary collapse
- UnknownTypeError =
Class.new(StandardError)
- MissingOptionError =
Class.new(StandardError)
- TYPES =
%i[string email password url integer money text flag datetime json plain_json foreign_key]
Constants inherited from Element
Element::UnknownFormattingError, Element::UnknownOptionError
Instance Attribute Summary collapse
-
#associated_model ⇒ Object
Returns the value of attribute associated_model.
-
#model ⇒ Object
Returns the value of attribute model.
Attributes inherited from Element
#application_model, #name, #options
Instance Method Summary collapse
- #assignable_values_as_list? ⇒ Boolean
- #for_migration ⇒ Object
- #has_defaults? ⇒ Boolean
-
#initialize(*args) ⇒ Attribute
constructor
A new instance of Attribute.
- #test_value ⇒ Object
Constructor Details
#initialize(*args) ⇒ Attribute
Returns a new instance of Attribute.
19 20 21 22 23 24 25 26 27 |
# File 'lib/katapult/elements/attribute.rb', line 19 def initialize(*args) super self.type ||= :email if name.to_s =~ /email/ self.type ||= :password if name.to_s =~ /password/ self.type ||= :string validate! end |
Instance Attribute Details
#associated_model ⇒ Object
Returns the value of attribute associated_model.
12 13 14 |
# File 'lib/katapult/elements/attribute.rb', line 12 def associated_model @associated_model end |
#model ⇒ Object
Returns the value of attribute model.
12 13 14 |
# File 'lib/katapult/elements/attribute.rb', line 12 def model @model end |
Instance Method Details
#assignable_values_as_list? ⇒ Boolean
70 71 72 |
# File 'lib/katapult/elements/attribute.rb', line 70 def assignable_values_as_list? assignable_values.try(:to_a).present? end |
#for_migration ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/katapult/elements/attribute.rb', line 35 def for_migration db_type = case type when :email, :url, :password then 'string' when :flag then 'boolean' when :money then 'decimal{10,2}' # {precision,scale} = total digits, decimal places when :json then 'jsonb' # Indexable JSON when :plain_json then 'json' # Only use this if you need to when :foreign_key then 'integer' else type end "#{name}:#{db_type}" end |
#has_defaults? ⇒ Boolean
31 32 33 |
# File 'lib/katapult/elements/attribute.rb', line 31 def has_defaults? !default.nil? and not [flag?, assignable_values].any? end |
#test_value ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/katapult/elements/attribute.rb', line 48 def test_value if type == :foreign_key associated_model.label_attr.test_value elsif assignable_values assignable_values.first else case type when :string then "#{name}-string" when :password then "#{name}-password" when :email then "#{name}@example.com" when :url then "#{name}.example.com" when :text then "#{name}-text" # Deterministically generate a value from the attribute's name when :integer then Zlib.crc32(name).modulo(1000) when :money then Zlib.crc32(name).modulo(1000) / 100.0 when :datetime then Time.at(Zlib.crc32(name)) end end end |