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

Direct Known Subclasses

ArbitraryConfiguration, StrictConfiguration

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



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/configurations/configuration.rb', line 12

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



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

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



71
72
73
# File 'lib/configurations/configuration.rb', line 71

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



78
79
80
# File 'lib/configurations/configuration.rb', line 78

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

#__empty?Boolean

Returns whether this configuration is empty.

Returns:

  • (Boolean)

    whether this configuration is empty



83
84
85
# File 'lib/configurations/configuration.rb', line 83

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:



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

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

  self
end

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

Respond to missing according to the method_missing implementation

Returns:

  • (Boolean)


36
37
38
# File 'lib/configurations/configuration.rb', line 36

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



44
45
46
47
48
49
50
# File 'lib/configurations/configuration.rb', line 44

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

    h
  end
end