Class: ConfigureMe::Setting
- Inherits:
-
Object
- Object
- ConfigureMe::Setting
- Defined in:
- lib/configure_me/setting.rb
Overview
Setting
There are two methods used to create a setting:
-
calling the class method
setting
from within an instance of ConfigureMe::Base -
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
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #convert(value) ⇒ Object
-
#initialize(name, *args) ⇒ Setting
constructor
A new instance of Setting.
Constructor Details
#initialize(name, *args) ⇒ Setting
Returns a new instance of Setting.
18 19 20 21 22 23 24 25 |
# File 'lib/configure_me/setting.rb', line 18 def initialize(name, *args) = args. @name = name.to_s @default = .key?(:default) ? [:default] : nil @type = .key?(:type) ? [:type] : infer_type(@default) raise UnsupportedType.new("Invalid type: #{@type}") unless VALID_TYPES.include?(@type) end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
13 14 15 |
# File 'lib/configure_me/setting.rb', line 13 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/configure_me/setting.rb', line 13 def name @name end |
#type ⇒ Object (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 |