Class: Alchemy::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/alchemy/configuration.rb,
lib/alchemy/configuration/base_option.rb,
lib/alchemy/configuration/class_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, PathnameOption, RegexpOption, StringOption, SymbolOption

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration_hash = {}) ⇒ Configuration

Returns a new instance of Configuration.



30
31
32
# File 'lib/alchemy/configuration.rb', line 30

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

Class Method Details

.configuration(name, configuration_class) ⇒ Object



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

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_configurationsObject



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

def defined_configurations = []

.defined_optionsObject



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

def defined_options = []

.defined_valuesObject



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

def defined_values
  defined_options + defined_configurations
end

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



108
109
110
111
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
# File 'lib/alchemy/configuration.rb', line 108

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?



148
149
150
151
152
# File 'lib/alchemy/configuration.rb', line 148

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



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

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

#hashObject



142
143
144
145
146
# File 'lib/alchemy/configuration.rb', line 142

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

#set(configuration_hash) ⇒ Object



34
35
36
37
38
# File 'lib/alchemy/configuration.rb', line 34

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

#set_from_yaml(file) ⇒ Object



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

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

#showObject



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

def show = self

#to_hObject



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

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