Class: EfoNelfo::Property

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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={})

  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

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)
  options.has_key?(args.first) ? options[args.first] : super
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/efo_nelfo/property.rb', line 9

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/efo_nelfo/property.rb', line 9

def options
  @options
end

#valueObject

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.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

Instance Method Details

#boolean?Boolean

Returns:

  • (Boolean)


75
# File 'lib/efo_nelfo/property.rb', line 75

def boolean?; type == :boolean; end

#date?Boolean

Returns:

  • (Boolean)


76
# File 'lib/efo_nelfo/property.rb', line 76

def date?;    type == :date;    end

#integer?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


65
66
67
# File 'lib/efo_nelfo/property.rb', line 65

def readonly?
  options[:read_only] == true
end

#required?Boolean

Returns true if the property is required Note: this is not in use yet

Returns:

  • (Boolean)


71
72
73
# File 'lib/efo_nelfo/property.rb', line 71

def required?
  options[:required] == true
end

#string?Boolean

Returns:

  • (Boolean)


78
# File 'lib/efo_nelfo/property.rb', line 78

def string?;  type == :string;  end

#to_csvObject

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_fObject 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