Class: Pgai::Resources::Attributes::Attribute
- Inherits:
-
Object
- Object
- Pgai::Resources::Attributes::Attribute
- Defined in:
- lib/pgai/resources/attributes.rb
Constant Summary collapse
- FALSE_VALUES =
[ "0", "f", "F", "false", "FALSE", "off", "OFF", "NO", "no" ].to_set.freeze
- TYPES =
{ string: ->(value) { value.to_s }, integer: ->(value) { value.to_i }, decimal: ->(value) { value.to_f }, boolean: ->(value) { !FALSE_VALUES.include?(value.to_s) }, datetime: ->(value) { ::Time.at(::Time.new(value.to_s, in: "UTC")) } }
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #cast(value) ⇒ Object
- #cast_or_default(user_value) ⇒ Object
-
#initialize(name, type, default) {|_self| ... } ⇒ Attribute
constructor
A new instance of Attribute.
Constructor Details
#initialize(name, type, default) {|_self| ... } ⇒ Attribute
Returns a new instance of Attribute.
29 30 31 32 33 34 35 |
# File 'lib/pgai/resources/attributes.rb', line 29 def initialize(name, type, default) @name = name @type = type @default = default yield self if block_given? end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
27 28 29 |
# File 'lib/pgai/resources/attributes.rb', line 27 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
25 26 27 |
# File 'lib/pgai/resources/attributes.rb', line 25 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
26 27 28 |
# File 'lib/pgai/resources/attributes.rb', line 26 def type @type end |
Instance Method Details
#cast(value) ⇒ Object
37 38 39 |
# File 'lib/pgai/resources/attributes.rb', line 37 def cast(value) TYPES.fetch(type).call(value) end |
#cast_or_default(user_value) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/pgai/resources/attributes.rb', line 41 def cast_or_default(user_value) value = default value = cast(user_value) unless user_value.nil? value = value.call if value.respond_to?(:call) value end |