Module: Configurations::Arbitrary

Defined in:
lib/configurations/arbitrary.rb

Overview

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Method missing gives access for reading and writing to the underlying configuration hash via dot notation



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/configurations/arbitrary.rb', line 27

def method_missing(method, *args, &block)
  if __respond_to_writer?(method)
    __assign!(method.to_s[0..-2].to_sym, args.first)
  elsif __respond_to_method_for_write?(method)
    @data[method]
  elsif __respond_to_method_for_read?(method, *args, &block)
    @data.fetch(method) do
      @not_configured_blocks.evaluate!(@path.add(method), method)
    end
  else
    super
  end
end

Instance Method Details

#__writeable__=(data) ⇒ Object

Set the configuration to writeable or read only. Access to writer methods is only allowed within the configure block, this method is used to invoke writeability for subconfigurations.

Parameters:

  • data (Boolean)

    true if the configuration should be writeable, false otherwise



72
73
74
75
76
77
78
79
# File 'lib/configurations/arbitrary.rb', line 72

def __writeable__=(data)
  @writeable = data
  return unless defined?(@data) && @data

  @data.each do |_k, v|
    v.__writeable__ = data if v.is_a?(__class__)
  end
end

#from_h(h) ⇒ Configuration

Note:

can only be accessed during writeable state (in configure block). Unassignable values are ignored

A convenience accessor to instantiate a configuration from a hash

Parameters:

  • h (Hash)

    the hash to read into the configuration

Returns:

Raises:

  • (ArgumentError)

    unless used in writeable state (in configure block)



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

def from_h(h)
  unless @writeable
    fail ::ArgumentError, 'can not dynamically assign values from a hash'
  end

  super
end

#initialize(options = {}, &block) {|HostModule::Configuration| ... } ⇒ HostModule::Configuration

Note:

An arbitrary configuration has to control its writeable state, therefore configuration is only possible in the initialization block

Initialize a new configuration

Parameters:

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

    The options to initialize a configuration with

  • block (Proc)

    a block to configure this 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

Yields:

  • (HostModule::Configuration)

    a configuration

Returns:

  • (HostModule::Configuration)

    a configuration



18
19
20
21
22
# File 'lib/configurations/arbitrary.rb', line 18

def initialize(options = {}, &block)
  self.__writeable__ = true
  super
  self.__writeable__ = false if block
end

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

Respond to missing according to the method_missing implementation

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/configurations/arbitrary.rb', line 43

def respond_to_missing?(method, include_private = false)
  __respond_to_writer?(method) ||
    __respond_to_method_for_read?(method, *args, &block) ||
    __respond_to_method_for_write?(method) ||
    super
end