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 collapse

RESERVED_METHODS =

Reserved methods are not assignable. They define behaviour needed for the configuration object to work properly.

[
  :initialize,
  :inspect,
  :method_missing,
  :object_id,
  :singleton_class, # needed by rbx
  :to_h,
  :to_s # needed by rbx / 1.9.3 for inspect
]

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



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/configurations/configuration.rb', line 33

def initialize(options = {}, &block)
  @__methods__ = options.fetch(:methods) { ::Hash.new }
  @__not_configured__ = options.fetch(:not_configured) { ::Hash.new }

  @data = __configuration_hash__

  __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



47
48
49
50
51
52
53
# File 'lib/configurations/configuration.rb', line 47

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



111
112
113
# File 'lib/configurations/configuration.rb', line 111

def __configurable?(_property)
  fail NotImplementedError, 'must be implemented in subclass'
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



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

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

#__empty?Boolean

Returns whether this configuration is empty.

Returns:

  • (Boolean)

    whether this configuration is empty



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

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



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/configurations/configuration.rb', line 80

def from_h(h)
  __test_ambiguity!(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



100
101
102
103
104
105
106
# File 'lib/configurations/configuration.rb', line 100

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)


57
58
59
# File 'lib/configurations/configuration.rb', line 57

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



65
66
67
68
69
70
71
# File 'lib/configurations/configuration.rb', line 65

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

    h
  end
end