Class: Settings::Setting

Inherits:
Object show all
Defined in:
lib/more/facets/settings.rb

Overview

A datastructure to store Settings metadata.

Please note the difference between :default and :value, :default does NOT override :value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, name, options) ⇒ Setting

Returns a new instance of Setting.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/more/facets/settings.rb', line 68

def initialize(owner, name, options)
  if options.key? :value
    @value = options[:value]
  elsif options.key? :default
    @value = options[:default]
  else
    raise ArgumentError.new('A value is required')
  end

  @owner, @name = owner, name
  @options = options
  @type = options[:type] = options[:type] || @value.class
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



66
67
68
# File 'lib/more/facets/settings.rb', line 66

def name
  @name
end

#optionsObject

Returns the value of attribute options.



66
67
68
# File 'lib/more/facets/settings.rb', line 66

def options
  @options
end

#ownerObject

Returns the value of attribute owner.



66
67
68
# File 'lib/more/facets/settings.rb', line 66

def owner
  @owner
end

#typeObject

Returns the value of attribute type.



66
67
68
# File 'lib/more/facets/settings.rb', line 66

def type
  @type
end

#valueObject

Returns the value of attribute value.



66
67
68
# File 'lib/more/facets/settings.rb', line 66

def value
  @value
end

Instance Method Details

#to_sObject

Text representation of this setting.



100
101
102
# File 'lib/more/facets/settings.rb', line 100

def to_s
  @value.to_s
end

#update(hash) ⇒ Object

Update the setting from an options hash. The update does NOT take default values into account!



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/more/facets/settings.rb', line 85

def update(hash)
  if hash.key? :value
    @value = hash[:value]
    @type = @value.class
  end

  if hash.key? :type
    @type = hash[:type]
  end

  @options.update(hash)
end