Class: EfoNelfo::Property
- Inherits:
-
Object
- Object
- EfoNelfo::Property
- Defined in:
- lib/efo_nelfo/property.rb
Constant Summary collapse
- VALID_OPTIONS =
[:type, :required, :limit, :read_only, :default, :decimals]
- VALID_TYPES =
[:string, :integer, :boolean, :date]
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #boolean? ⇒ Boolean
- #date? ⇒ Boolean
-
#initialize(name, defaults = {}) ⇒ Property
constructor
A new instance of Property.
- #integer? ⇒ Boolean
- #method_missing(*args) ⇒ Object
-
#readonly? ⇒ Boolean
Returns true if the property is read only.
-
#required? ⇒ Boolean
Returns true if the property is required Note: this is not in use yet.
- #string? ⇒ Boolean
-
#to_csv ⇒ Object
returns formatted value suitable for csv output.
-
#to_f ⇒ Object
(also: #to_decimal)
Returns integer to floating point based on specified decimals Example: If decimal is set to 4, and the value is 4000 then to_f returns 0.4.
Constructor Details
#initialize(name, defaults = {}) ⇒ Property
Returns a new instance of Property.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/efo_nelfo/property.rb', line 17 def initialize(name, defaults={}) = { type: :string, required: false, default: nil, read_only: false, decimals: nil, limit: 100 } .update(defaults) if defaults.is_a?(Hash) self.class. @name = name @options = @value = [:default] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
80 81 82 |
# File 'lib/efo_nelfo/property.rb', line 80 def method_missing(*args) .has_key?(args.first) ? [args.first] : super end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/efo_nelfo/property.rb', line 9 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/efo_nelfo/property.rb', line 9 def @options end |
#value ⇒ Object
Returns the value of attribute value.
9 10 11 |
# File 'lib/efo_nelfo/property.rb', line 9 def value @value end |
Class Method Details
.validate_options!(options) ⇒ Object
11 12 13 14 15 |
# File 'lib/efo_nelfo/property.rb', line 11 def self.() = .keys - VALID_OPTIONS raise EfoNelfo::UnknownPropertyOption.new("Invalid options: #{.join(',')}") if .any? raise EfoNelfo::InvalidPropertyType.new("Valid types are #{VALID_TYPES.join(',')}") unless VALID_TYPES.include?([:type]) end |
Instance Method Details
#boolean? ⇒ Boolean
75 |
# File 'lib/efo_nelfo/property.rb', line 75 def boolean?; type == :boolean; end |
#date? ⇒ Boolean
76 |
# File 'lib/efo_nelfo/property.rb', line 76 def date?; type == :date; end |
#integer? ⇒ Boolean
77 |
# File 'lib/efo_nelfo/property.rb', line 77 def integer?; type == :integer; end |
#readonly? ⇒ Boolean
Returns true if the property is read only
65 66 67 |
# File 'lib/efo_nelfo/property.rb', line 65 def readonly? [:read_only] == true end |
#required? ⇒ Boolean
Returns true if the property is required Note: this is not in use yet
71 72 73 |
# File 'lib/efo_nelfo/property.rb', line 71 def required? [:required] == true end |
#string? ⇒ Boolean
78 |
# File 'lib/efo_nelfo/property.rb', line 78 def string?; type == :string; end |
#to_csv ⇒ Object
returns formatted value suitable for csv output
50 51 52 |
# File 'lib/efo_nelfo/property.rb', line 50 def to_csv value.to_csv end |
#to_f ⇒ Object Also known as: to_decimal
Returns integer to floating point based on specified decimals Example:
If decimal is set to 4, and the value is 4000
then to_f returns 0.4
58 59 60 61 |
# File 'lib/efo_nelfo/property.rb', line 58 def to_f return nil if value.nil? value.to_f.with_decimals decimals end |