Class: Alchemy::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/alchemy/configuration.rb,
lib/alchemy/configuration/base_option.rb,
lib/alchemy/configuration/list_option.rb,
lib/alchemy/configuration/class_option.rb,
lib/alchemy/configuration/regexp_option.rb,
lib/alchemy/configuration/string_option.rb,
lib/alchemy/configuration/boolean_option.rb,
lib/alchemy/configuration/integer_option.rb,
lib/alchemy/configuration/class_set_option.rb,
lib/alchemy/configuration/string_list_option.rb,
lib/alchemy/configuration/integer_list_option.rb

Defined Under Namespace

Classes: BaseOption, BooleanOption, ClassOption, ClassSetOption, IntegerListOption, IntegerOption, ListOption, RegexpOption, StringListOption, StringOption

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration_hash = {}) ⇒ Configuration

Returns a new instance of Configuration.



17
18
19
# File 'lib/alchemy/configuration.rb', line 17

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

Class Method Details

.configuration(name, configuration_class) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/alchemy/configuration.rb', line 61

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



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

def defined_configurations = []

.defined_optionsObject



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

def defined_options = []

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



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

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) do
    unless instance_variable_defined?(:"@#{name}")
      send(:"#{name}=", default)
    end
    instance_variable_get(:"@#{name}").value
  end

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

Instance Method Details

#fetch(key, default = nil) ⇒ Object



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

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

#set(configuration_hash) ⇒ Object



21
22
23
24
25
# File 'lib/alchemy/configuration.rb', line 21

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

#set_from_yaml(file) ⇒ Object



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

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

#showObject



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

def show = self

#to_hObject



46
47
48
49
50
51
52
53
54
# File 'lib/alchemy/configuration.rb', line 46

def to_h
  self.class.defined_options.map do |option|
    [option, send(option)]
  end.concat(
    self.class.defined_configurations.map do |configuration|
      [configuration, send(configuration).to_h]
    end
  ).to_h
end