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
# 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, &__not_configured_callback_for__(method))
  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



67
68
69
# File 'lib/configurations/arbitrary.rb', line 67

def __configurable?(_property)
  true
end

#__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



77
78
79
80
81
82
83
84
# File 'lib/configurations/arbitrary.rb', line 77

def __writeable__=(data)
  @__writeable__ = data
  return if @data.nil?

  @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)



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

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)


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

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