Class: Alchemy::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/alchemy/configuration.rb,
lib/alchemy/configuration/base_option.rb,
lib/alchemy/configuration/proc_option.rb,
lib/alchemy/configuration/class_option.rb,
lib/alchemy/configuration/number_option.rb,
lib/alchemy/configuration/regexp_option.rb,
lib/alchemy/configuration/string_option.rb,
lib/alchemy/configuration/symbol_option.rb,
lib/alchemy/configuration/boolean_option.rb,
lib/alchemy/configuration/integer_option.rb,
lib/alchemy/configuration/pathname_option.rb,
lib/alchemy/configuration/collection_option.rb,
lib/alchemy/configuration/configuration_option.rb

Defined Under Namespace

Classes: BaseOption, BooleanOption, ClassOption, CollectionOption, ConfigurationError, ConfigurationOption, IntegerOption, NumberOption, PathnameOption, ProcOption, RegexpOption, StringOption, SymbolOption

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration_hash = {}) ⇒ Configuration

Returns a new instance of Configuration.



32
33
34
# File 'lib/alchemy/configuration.rb', line 32

def initialize(configuration_hash = {})
  set(configuration_hash)
end

Class Method Details

.configuration(name, configuration_class) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/alchemy/configuration.rb', line 84

def configuration(name, configuration_class)
  # The defined configurations on a class are all those defined directly on
  # that class as well as those defined on ancestors.
  # We store these as a class instance variable on each class which has a
  # configuration. super() collects configurations defined on ancestors.
  singleton_configurations = (@defined_singleton_configurations ||= [])
  singleton_configurations << name.to_sym

  define_singleton_method :defined_configurations do
    super() + singleton_configurations
  end

  define_method(name) do
    unless instance_variable_get(:"@#{name}")
      send(:"#{name}=", configuration_class.new)
    end
    instance_variable_get(:"@#{name}")
  end

  define_method(:"#{name}=") do |value|
    if value.is_a?(configuration_class)
      instance_variable_set(:"@#{name}", value)
    else
      send(name).set(value)
    end
  end
end

.defined_configurations ⇒ Object



76
# File 'lib/alchemy/configuration.rb', line 76

def defined_configurations = []

.defined_options ⇒ Object



78
# File 'lib/alchemy/configuration.rb', line 78

def defined_options = []

.defined_values ⇒ Object



80
81
82
# File 'lib/alchemy/configuration.rb', line 80

def defined_values
  defined_options + defined_configurations
end

.option(name, type, default: nil, **args) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/alchemy/configuration.rb', line 112

def option(name, type, default: nil, **args)
  klass = "Alchemy::Configuration::#{type.to_s.camelize}Option".constantize
  # The defined options on a class are all those defined directly on
  # that class as well as those defined on ancestors.
  # We store these as a class instance variable on each class which has a
  # option. super() collects options defined on ancestors.
  singleton_options = (@defined_singleton_options ||= [])
  singleton_options << name.to_sym

  define_singleton_method :defined_options do
    super() + singleton_options
  end

  define_method("#{name}_option") do
    unless instance_variable_defined?(:"@#{name}")
      send(:"#{name}=", default)
    end
    instance_variable_get(:"@#{name}")
  end

  define_method(name) do
    send("#{name}_option").value
  end

  define_method("raw_#{name}") do
    send("#{name}_option").raw_value
  end

  define_method(:"#{name}=") do |value|
    instance_variable_set(:"@#{name}", klass.new(value:, name:, **args))
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



152
153
154
155
156
# File 'lib/alchemy/configuration.rb', line 152

def ==(other)
  equal?(other) || self.class == other.class && self.class.defined_values.all? do |var|
    send(var) == other.send(var)
  end
end

#fetch(key, default = nil) ⇒ Object



48
49
50
# File 'lib/alchemy/configuration.rb', line 48

def fetch(key, default = nil)
  get(key) || default
end

#hash ⇒ Object



146
147
148
149
150
# File 'lib/alchemy/configuration.rb', line 146

def hash
  self.class.defined_values.map do |ivar|
    [ivar, send(ivar).hash]
  end.hash
end

#set(configuration_hash) ⇒ Object



36
37
38
39
40
# File 'lib/alchemy/configuration.rb', line 36

def set(configuration_hash)
  configuration_hash.each do |key, value|
    send(:"#{key}=", value)
  end
end

#set_from_yaml(file) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/alchemy/configuration.rb', line 52

def set_from_yaml(file)
  set(
    YAML.safe_load(
      ERB.new(File.read(file)).result,
      permitted_classes: YAML_PERMITTED_CLASSES,
      aliases: true
    ) || {}
  )
end

#show ⇒ Object



46
# File 'lib/alchemy/configuration.rb', line 46

def show = self

#to_h ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/alchemy/configuration.rb', line 62

def to_h
  self.class.defined_options.map do |option|
    value = send(option)
    [option, value.respond_to?(:to_serializable_array) ? value.to_serializable_array : value]
  end.concat(
    self.class.defined_configurations.map do |configuration|
      [configuration, send(configuration).to_h]
    end
  ).to_h
end