Module: Configurations::Configurable::ClassMethods

Defined in:
lib/configurations/configurable.rb

Overview

Class methods that will get installed in the host module

Instance Method Summary collapse

Instance Method Details

#configurable(*properties, &block) ⇒ Object

configurable can be used to set the properties which should be configurable, as well as a type which the given property should be asserted to

Examples:

Define a configurable property

configurable :foo

Define a type asserted, nested property for type String

configurable String, bar: :baz

Define a custom assertion for a property

configurable biz: i(bi bu) do |value|
  unless %w(a b c).include?(value)
    fail ArgumentError, 'must be one of a, b, c'
  end
end

Parameters:

  • properties (Class, Symbol, Hash)

    a type as a first argument to type assert (if any) or nested properties to allow for setting

  • block (Proc)

    a block with arity 2 to evaluate when a property is set. It will be given: property name and value



119
120
121
122
123
124
# File 'lib/configurations/configurable.rb', line 119

def configurable(*properties, &block)
  type = properties.shift if properties.first.is_a?(Module)

  @configurable ||= {}
  @configurable.merge! to_configurable_hash(properties, type, &block)
end

#configurable?(property) ⇒ Boolean

returns whether a property is set to be configurable

Parameters:

  • property (Symbol)

    the property to ask status for

Returns:

  • (Boolean)

    whether the property is configurable



130
131
132
# File 'lib/configurations/configurable.rb', line 130

def configurable?(property)
  @configurable.is_a?(Hash) && @configurable.key?(property)
end

#configuration_defaults(&block) ⇒ Object

Configuration defaults can be used to set the defaults of any Configuration

Parameters:

  • block (Proc)

    setting the default values of the configuration



97
98
99
# File 'lib/configurations/configurable.rb', line 97

def configuration_defaults(&block)
  @configuration_defaults = block
end

#configuration_method(method, &block) ⇒ Object

configuration method can be used to retrieve properties from the configuration which use your gem’s context

Examples:

Define a configuration method ‘foobararg’

configuration_method :foobararg do |arg|
  foo + bar + arg
end

Define a configuration method on a nested property

configuration_method foo: { bar: :arg } do
  baz + biz
end

Parameters:

  • method (Class, Symbol, Hash)

    the method to define

  • block (Proc)

    the block to evaluate



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/configurations/configurable.rb', line 148

def configuration_method(method, &block)
  fail(
    ArgumentError,
    "can't be configuration property and a method"
  ) if configurable?(method)

  @configuration_methods ||= {}
  method_hash = if method.is_a?(Hash)
                  ingest_configuration_block!(method, &block)
                else
                  { method => block }
                end

  @configuration_methods.merge! method_hash
end

#not_configured(*properties, &block) {|Symbol| ... } ⇒ Object

not_configured defines the behaviour when a property has not been configured. This can be useful for presence validations of certain properties or behaviour for undefined properties deviating from the original behaviour.

Examples:

Define a specific not_configured callback

not_configured :property1, property2: :property3 do |property|
  raise ArgumentError, "#{property} should be configured"
end

Define a catch-all not_configured callback

not_configured do |property|
  raise StandardError, "You did not configure #{property}"
end

Parameters:

  • properties (Array, Symbol, Hash)

    the properties to install the callback on. If omitted, the callback will be installed on all properties that have no specific callbacks

  • block (Proc)

    the block to evaluate when a property has not been configured

Yields:

  • (Symbol)

    the property that has not been configured



183
184
185
186
187
188
189
190
191
# File 'lib/configurations/configurable.rb', line 183

def not_configured(*properties, &block)
  @not_configured ||= {}

  if properties.empty?
    @not_configured.default_proc = ->(h, k) { h[k] = block }
  else
    nested_merge_not_configured_hash(*properties, &block)
  end
end