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



90
91
92
# File 'lib/efo_nelfo/property.rb', line 90

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)


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

def boolean?; type == :boolean; end

#date?Boolean

Returns:

  • (Boolean)


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

def date?;    type == :date;    end

#integer?Boolean

Returns:

  • (Boolean)


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

def integer?; type == :integer; end

#readonly?Boolean

Returns true if the property is read only

Returns:

  • (Boolean)


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

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)


81
82
83
# File 'lib/efo_nelfo/property.rb', line 81

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

#string?Boolean

Returns:

  • (Boolean)


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

def string?;  type == :string;  end

#to_csvObject

returns formatted value suitable for csv output



60
61
62
# File 'lib/efo_nelfo/property.rb', line 60

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


68
69
70
71
# File 'lib/efo_nelfo/property.rb', line 68

def to_f
  return nil if value.nil?
  value.to_f.with_decimals decimals
end