Class: ConfigureMe::Setting

Inherits:
Object
  • Object
show all
Defined in:
lib/configure_me/setting.rb

Overview

Setting

There are two methods used to create a setting:

  1. calling the class method setting from within an instance of ConfigureMe::Base

  2. implicitly when a hash is fed to ConfigureMe::Base.load

Constant Summary collapse

VALID_TYPES =
[:string, :integer, :float, :boolean, :unknown]
TRUE_VALUES =
[true, 1, '1', 't', 'T', 'true', 'TRUE']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args) ⇒ Setting

Returns a new instance of Setting.

Raises:



18
19
20
21
22
23
24
25
# File 'lib/configure_me/setting.rb', line 18

def initialize(name, *args)
  options = args.extract_options!

  @name = name.to_s
  @default = options.key?(:default) ? options[:default] : nil
  @type = options.key?(:type) ? options[:type] : infer_type(@default)
  raise UnsupportedType.new("Invalid type: #{@type}") unless VALID_TYPES.include?(@type)
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



13
14
15
# File 'lib/configure_me/setting.rb', line 13

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/configure_me/setting.rb', line 13

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/configure_me/setting.rb', line 13

def type
  @type
end

Instance Method Details

#convert(value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/configure_me/setting.rb', line 27

def convert(value)
  case type
  when :string    then convert_to_string(value)
  when :integer   then value.to_i rescue value ? 1 : 0
  when :float     then value.to_f rescue value ? 1.0 : 0.0
  when :boolean   then convert_to_boolean(value)
  when :unknown
    @type = infer_type(value)
    convert(value)
  end
end