class EfoNelfo::Property
Constants
- VALID_OPTIONS
- VALID_TYPES
Attributes
name[R]
options[R]
value[R]
Public Class Methods
new(name, defaults={})
click to toggle source
# File lib/efo_nelfo/property.rb, line 17 def initialize(name, defaults={}) options = { type: :string, required: false, default: nil, read_only: false, decimals: nil, limit: 100 } options.update(defaults) if defaults.is_a?(Hash) self.class.validate_options! options @name = name @options = options @value = options[:default] end
validate_options!(options)
click to toggle source
# File lib/efo_nelfo/property.rb, line 11 def self.validate_options!(options) invalid_options = options.keys - VALID_OPTIONS raise EfoNelfo::UnknownPropertyOption.new("Invalid options: #{invalid_options.join(',')}") if invalid_options.any? raise EfoNelfo::InvalidPropertyType.new("Valid types are #{VALID_TYPES.join(',')}") unless VALID_TYPES.include?(options[:type]) end
Public Instance Methods
boolean?()
click to toggle source
# File lib/efo_nelfo/property.rb, line 75 def boolean?; type == :boolean; end
date?()
click to toggle source
# File lib/efo_nelfo/property.rb, line 76 def date?; type == :date; end
integer?()
click to toggle source
# File lib/efo_nelfo/property.rb, line 77 def integer?; type == :integer; end
method_missing(*args)
click to toggle source
Calls superclass method
# File lib/efo_nelfo/property.rb, line 80 def method_missing(*args) options.has_key?(args.first) ? options[args.first] : super end
readonly?()
click to toggle source
Returns true if the property is read only
# File lib/efo_nelfo/property.rb, line 65 def readonly? options[:read_only] == true end
required?()
click to toggle source
Returns true if the property is required Note: this is not in use yet
# File lib/efo_nelfo/property.rb, line 71 def required? options[:required] == true end
string?()
click to toggle source
# File lib/efo_nelfo/property.rb, line 78 def string?; type == :string; end
to_csv()
click to toggle source
returns formatted value suitable for csv output
# File lib/efo_nelfo/property.rb, line 50 def to_csv value.to_csv end
to_f()
click to toggle source
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
# File lib/efo_nelfo/property.rb, line 58 def to_f return nil if value.nil? value.to_f.with_decimals decimals end
Also aliased as: to_decimal
value=(new_value)
click to toggle source
Assign a value to the property The value is converted to whatever specified by options Examples:
(boolean) value = 'J' # => true (boolean) value = false # => false (date) value = '20120101' # => Date.new(2012,1,1) (integer) value = '2' # => 2 (string) value = 'foo' # => 'foo'
# File lib/efo_nelfo/property.rb, line 44 def value=(new_value) return nil if readonly? @value = send("sanitize_#{options[:type]}", new_value) end
Private Instance Methods
sanitize_boolean(value)
click to toggle source
# File lib/efo_nelfo/property.rb, line 90 def sanitize_boolean(value) value.nil? || value == true || value == 'J' || value == 'j' || value == '' ? true : false end
sanitize_date(value)
click to toggle source
# File lib/efo_nelfo/property.rb, line 94 def sanitize_date(value) value.is_a?(Date) ? value : Date.parse(value) rescue nil end
sanitize_integer(value)
click to toggle source
# File lib/efo_nelfo/property.rb, line 86 def sanitize_integer(value) value.nil? && !required? ? nil : value.to_i end
sanitize_string(value)
click to toggle source
# File lib/efo_nelfo/property.rb, line 98 def sanitize_string(value) return value unless value.is_a?(String) return nil if /\A[[:space:]]*\z/ === value string = value.to_s if EfoNelfo.strict_mode? && (options[:limit] && string.length > options[:limit]) raise ArgumentError.new("Value exceeds limit") else string.slice(0, options[:limit]) end end