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.
31 32 33 34 35 36 37 |
# File 'lib/pgai/resources/attributes.rb', line 31 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.
29 30 31 |
# File 'lib/pgai/resources/attributes.rb', line 29 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
27 28 29 |
# File 'lib/pgai/resources/attributes.rb', line 27 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
28 29 30 |
# File 'lib/pgai/resources/attributes.rb', line 28 def type @type end |
Instance Method Details
#cast(value) ⇒ Object
39 40 41 |
# File 'lib/pgai/resources/attributes.rb', line 39 def cast(value) TYPES.fetch(type).call(value) end |
#cast_or_default(user_value) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/pgai/resources/attributes.rb', line 43 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 |