Class: Configurations::Configuration

Inherits:
BlankObject show all
Defined in:
lib/configurations/configuration.rb

Overview

Configuration is a blank object in order to allow configuration of various properties including keywords

Constant Summary

Constants inherited from BlankObject

BlankObject::ALIAS_KERNEL_METHODS, BlankObject::KEEP_KERNEL_METHODS, BlankObject::KEEP_METHODS

Instance Method Summary collapse

Methods inherited from BlankObject

blank_kernel

Constructor Details

#initialize(options = {}, &block) ⇒ Configuration

Initialize a new configuration

Parameters:

  • options (Hash) (defaults to: {})

    The options to initialize a configuration with

Options Hash (options):

  • methods (Hash)

    a hash of method names pointing to procs

  • not_configured (Proc)

    a proc to evaluate for not_configured properties



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/configurations/configuration.rb', line 21

def initialize(options = {}, &block)
  @data = Data.new(__configuration_hash__)
  @path = options.fetch(:path) { Path.new }
  @data_map = options.fetch(:data) { Maps::Data.new }

  @methods = options.fetch(:methods) { ::Hash.new }
  @method_blocks = options.fetch(:method_blocks) { Maps::Blocks.new }
  @not_configured_blocks = options.fetch(:not_configured_blocks) { Maps::Blocks.new }

  @reserved_method_validator = Validators::ReservedMethods.new
  @key_ambiguity_validator = Validators::Ambiguity.new

  __instance_eval__(&options[:defaults]) if options[:defaults]
  __instance_eval__(&block) if block

  __install_configuration_methods__
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Method missing gives access to Kernel methods



41
42
43
44
45
46
47
# File 'lib/configurations/configuration.rb', line 41

def method_missing(method, *args, &block)
  if __can_delegate_to_kernel?(method)
    ::Kernel.__send__(method, *args, &block)
  else
    super
  end
end

Instance Method Details

#__configurable?(property) ⇒ Boolean

Returns whether the given property is configurable.

Parameters:

  • property (Symbol)

    The property to test for configurability

Returns:

  • (Boolean)

    whether the given property is configurable



106
107
108
109
110
111
112
# File 'lib/configurations/configuration.rb', line 106

def __configurable?(property)
  if defined?(@configurable_properties) && @configurable_properties
    @configurable_properties.configurable?(@path.add(property))
  else
    true
  end
end

#__configured?(property) ⇒ Boolean

Returns whether the given property has been configured.

Parameters:

  • property (Symbol)

    The property to test for

Returns:

  • (Boolean)

    whether the given property has been configured



117
118
119
# File 'lib/configurations/configuration.rb', line 117

def __configured?(property)
  @data.key?(property)
end

#__empty?Boolean

Returns whether this configuration is empty.

Returns:

  • (Boolean)

    whether this configuration is empty



122
123
124
# File 'lib/configurations/configuration.rb', line 122

def __empty?
  @data.empty?
end

#from_h(h) ⇒ Configuration

A convenience accessor to instantiate a configuration from a hash

Parameters:

  • h (Hash)

    the hash to read into the configuration

Returns:

Raises:

  • (ConfigurationError)

    if the given hash ambiguous values

    • string and symbol keys with the same string value pointing to

    different values



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/configurations/configuration.rb', line 74

def from_h(h)
  @key_ambiguity_validator.validate!(h)

  h.each do |property, value|
    p = property.to_sym
    if value.is_a?(::Hash) && __nested?(p)
      @data[p].from_h(value)
    elsif __configurable?(p)
      __assign!(p, value)
    end
  end

  self
end

#inspect(debug = false) ⇒ String

Inspect a configuration. Implements inspect without exposing internally used instance variables.

Parameters:

  • debug (TrueClass, FalseClass) (defaults to: false)

    whether to show internals, defaults to false

Returns:

  • (String)

    The inspect output for this instance



95
96
97
98
99
100
101
# File 'lib/configurations/configuration.rb', line 95

def inspect(debug = false)
  unless debug
    '#<%s:0x00%x @data=%s>' % [__class__, object_id << 1, @data.inspect]
  else
    super()
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Respond to missing according to the method_missing implementation

Returns:

  • (Boolean)


51
52
53
# File 'lib/configurations/configuration.rb', line 51

def respond_to_missing?(method, include_private = false)
  __can_delegate_to_kernel?(method) || super
end

#to_hHash

A convenience accessor to get a hash representation of the current state of the configuration

Returns:

  • (Hash)

    the configuration in hash form



59
60
61
62
63
64
65
# File 'lib/configurations/configuration.rb', line 59

def to_h
  @data.reduce({}) do |h, (k, v)|
    h[k] = v.is_a?(__class__) ? v.to_h : v

    h
  end
end