Class: Squire::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/squire/settings.rb

Constant Summary collapse

RESERVED =
[:get_value, :set_value, :define_key_accessor, :to_hash, :to_s, :[]]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, parent = nil) ⇒ Settings

Creates new settings with path and parent. For top level settings (usually namespace), the is no parent or path specified.



8
9
10
11
12
13
# File 'lib/squire/settings.rb', line 8

def initialize(path = nil, parent = nil)
  @path     = path
  @table    = ::Hash.new
  @parent   = parent
  @children = ::Array.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Handles settting of values.

Examples

Setting a value:

.value = 2
.value   2

Getting a value:

.value # => 2

Checking a value:

.value? # => true

If block provided, it yields key as parent.

.value do |value|
  ...
end

.value do
  ...
end


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/squire/settings.rb', line 38

def method_missing(method, *args, &block)
  _, key, type = *method.to_s.match(/(?<key>\w+)(?<type>[?=]{0,1})/)

  key = key.to_sym

  if block_given?
    if @table[key]
      settings = @table[key]
    else
      settings = Settings.new(@path ? "#{@path}.#{key}" : key, self)

      set_value(key, settings)

      @children << settings
    end

    get_value(key, &block)
  elsif args.count == 1
    set_value(key, args.pop)
  elsif type == '?'
    !!get_value(key)
  else
    value = get_value(key)

    if value.nil?
      raise MissingSettingError.new("Missing setting '#{key}' in '#{@path}'.")
    end

    value
  end
end

Class Method Details

.from_hash(hash, parent = nil) ⇒ Object

Loads new settings from provided hash.



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/squire/settings.rb', line 135

def self.from_hash(hash, parent = nil)
  result = Settings.new(parent)

  hash.each_pair do |key, value|
    if value.is_a? ::Hash
      value = from_hash(value, key)
    end

    result.set_value(key.to_sym, value)
  end

  result
end

Instance Method Details

#[](key) ⇒ Object

Access key from table directly



129
130
131
# File 'lib/squire/settings.rb', line 129

def [](key)
  get_value(key)
end

#get_value(key, &block) ⇒ Object

Returns a value for key from settings table. Yields value of key if block provided.

Examples:

.key do |key|
  ...
end

# or

.key do
  ...
end


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

def get_value(key, &block)
  key   = key.to_sym
  value = @table[key]

  if block_given?
    block.arity == 0 ? value.instance_eval(&block) : block.call(value)
  end

  value
end

#set_value(key, value) ⇒ Object

Sets a value for key



98
99
100
101
102
# File 'lib/squire/settings.rb', line 98

def set_value(key, value)
  @table[key] = value

  define_key_accessor(key)
end

#to_hashObject Also known as: to_h

Dumps settings as hash.



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/squire/settings.rb', line 106

def to_hash
  result = ::Hash.new

  @table.each do |key, value|
    if value.is_a? Settings
      value = value.to_hash
    end

    result[key] = value
  end

  result
end

#to_sObject

Shows string representation of settings



123
124
125
# File 'lib/squire/settings.rb', line 123

def to_s
  "#<#{self.class.name} #{@table.map { |key, value| "#{key}=#{value}"}.join(', ')}>"
end