Module: Mattock::Configurable

Extended by:
ClassMethods
Included in:
CascadingDefinition, DirectoryStructure::StructurePath, Struct, ConfigurableTask
Defined in:
lib/mattock/configurable.rb,
lib/mattock/configurable/proxy-value.rb,
lib/mattock/configurable/class-methods.rb,
lib/mattock/configurable/field-metadata.rb,
lib/mattock/configurable/field-processor.rb,
lib/mattock/configurable/instance-methods.rb,
lib/mattock/configurable/directory-structure.rb

Overview

Handles setting options on objects it’s mixed into

Settings can have default values or be required (as opposed to defaulting to nil). Settings and their defaults are inherited (and can be overridden) by subclasses.

Mattock also includes a yard-extension that will document settings of a Configurable

Examples:

class ConfExample
  include Configurable

  setting :foo
  settings :bar => 1, :baz => 3
  nil_fields :hoo, :ha, :harum
  required_fields :must

  def initialize
    setup_defaults
  end
end

ce = ConfExample.new
ce.bar #=> 1
ce.hoo #=> nil
ce.hoo = "hallo"
ce.check_required #=> raises error because :must and :foo aren't set

Defined Under Namespace

Modules: ClassMethods, DirectoryStructure Classes: Exception, FieldMetadata, FieldProcessor, MissingRelativePaths, NoDefaultValue, ProxyDecorator, ProxyValue, SettingsCopier, SettingsProxier, Struct

Constant Summary collapse

RequiredField =
Object.new.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

default_value_for, default_values, field_metadata, field_names, included, inspect_instance, missing_required_fields_on, nested, nil_fields, required_fields, runtime_required_fields, runtime_setting, set_defaults_on, setting, settings

Class Method Details

.unset_defaults_guardObject



31
32
# File 'lib/mattock/configurable/instance-methods.rb', line 31

def self.unset_defaults_guard
end

Instance Method Details

#check_requiredObject

Checks that all required fields have be set, otherwise raises an error

Raises:

  • RuntimeError if any required fields are unset



40
41
42
43
44
45
46
47
# File 'lib/mattock/configurable/instance-methods.rb', line 40

def check_required
  unset_defaults_guard
  missing = self.class.missing_required_fields_on(self)
  unless missing.empty?
    raise "Required field#{missing.length > 1 ? "s" : ""} #{missing.map{|field| field.to_s.inspect}.join(", ")} unset on #{self.inspect}"
  end
  self
end

#copy_settingsObject



3
4
5
# File 'lib/mattock/configurable/instance-methods.rb', line 3

def copy_settings
  SettingsCopier.new(self)
end

#copy_settings_to(other) ⇒ Object



7
8
9
10
# File 'lib/mattock/configurable/instance-methods.rb', line 7

def copy_settings_to(other)
  copy_settings.to(other)
  self
end

#fail_unless_set(name) ⇒ Object Also known as: fail_if_unset

Requires that a named field be set



63
64
65
66
67
68
# File 'lib/mattock/configurable/instance-methods.rb', line 63

def fail_unless_set(name)
  if field_unset?(name)
    raise "Assertion failed: Field #{name} unset"
  end
  true
end

#field_unset?(name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/mattock/configurable/instance-methods.rb', line 58

def field_unset?(name)
  self.class.(name).unset_on?(self)
end

#proxy_settingsObject



12
13
14
# File 'lib/mattock/configurable/instance-methods.rb', line 12

def proxy_settings
  SettingsProxier.new(self)
end

#proxy_settings_to(other) ⇒ Object



16
17
18
# File 'lib/mattock/configurable/instance-methods.rb', line 16

def proxy_settings_to(other)
  proxy_settings.to(other)
end

#proxy_valueObject



49
50
51
# File 'lib/mattock/configurable/instance-methods.rb', line 49

def proxy_value
  ProxyDecorator.new(self)
end

#setup_defaultsObject

Call during initialize to set default values on settings - if you’re using Configurable outside of Mattock, be sure this gets called.



30
31
32
33
34
35
36
# File 'lib/mattock/configurable/instance-methods.rb', line 30

def setup_defaults
  def self.unset_defaults_guard
  end

  self.class.set_defaults_on(self)
  self
end

#to_hashObject



20
21
22
# File 'lib/mattock/configurable/instance-methods.rb', line 20

def to_hash
  self.class.to_hash(self)
end

#unset?(value) ⇒ Boolean

XXX deprecate

Returns:

  • (Boolean)


54
55
56
# File 'lib/mattock/configurable/instance-methods.rb', line 54

def unset?(value)
  value.nil?
end

#unset_defaults_guardObject



24
25
26
# File 'lib/mattock/configurable/instance-methods.rb', line 24

def unset_defaults_guard
  raise "Tried to check required settings before running setup_defaults"
end