Class: Squire::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_namespaceObject

Returns the value of attribute base_namespace.



3
4
5
# File 'lib/squire/configuration.rb', line 3

def base_namespace
  @base_namespace
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/squire/configuration.rb', line 3

def type
  @type
end

Instance Method Details

#namespace(namespace = nil, options = {}) ⇒ Object

Sets and returns namespace. If called without parameters, returns namespace.

Possible options:

  • :base - base namespace used for deep merging of values of other namespaces



11
12
13
14
15
16
# File 'lib/squire/configuration.rb', line 11

def namespace(namespace = nil, options = {})
  return @namespace unless namespace

  @namespace      = namespace.to_sym if namespace
  @base_namespace = options[:base].to_sym if options[:base]
end

#reload!Object

Reloads the settings.



88
89
90
# File 'lib/squire/configuration.rb', line 88

def reload!
  @settings = nil
end

#settings(&block) ⇒ Object Also known as: config

Loaded configuration stored in Settings class. Accepts block as parameter.

Examples

settings do |settings|
  settings.a = 1
end

settings do
  a 1
end

settings.a = 1


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/squire/configuration.rb', line 49

def settings(&block)
  @settings ||= setup

  settings = @namespace ? @settings.get_value(@namespace) : @settings

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

  settings
end

#setupObject

Sets up the configuration based on namespace and source. If base_namespace provided, merges it’s values with other namespaces for handling nested overriding of values.

Favours values from namespace over values from base_namespace.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/squire/configuration.rb', line 69

def setup
  return Squire::Settings.new unless @source

  parser = Squire::Parser.of(@type)

  hash = parser.parse(source)

  if base_namespace
    hash.except(base_namespace).each do |key, values|
      # favours value from namespace over value from defaults
      values.deep_merge!(hash[base_namespace]) { |_, value, _| value }
    end
  end

  Squire::Settings.from_hash(hash)
end

#source(source = nil, options = {}) ⇒ Object

Sets source for the configuration If called without parameters, returns source.

Possible options:

  • :type - type of source (optional, based on file extension)

  • :parser - parse of input source (optional, based on :type)



25
26
27
28
29
30
31
32
33
# File 'lib/squire/configuration.rb', line 25

def source(source = nil, options = {})
  return @source unless source

  @source = source
  @parser = options[:parser]
  @type   = options[:type]

  @type ||= source.is_a?(Hash) ? :hash : File.extname(@source)[1..-1].to_sym
end