Module: GlueGun::DSL

Extended by:
ActiveSupport::Concern
Defined in:
lib/glue_gun/dsl.rb

Defined Under Namespace

Modules: ClassMethods, Initialization Classes: ConfigAttr, DependencyBuilder, OptionBuilder

Instance Method Summary collapse

Instance Method Details

#dependenciesObject



271
272
273
# File 'lib/glue_gun/dsl.rb', line 271

def dependencies
  @dependencies ||= {}
end

#initialize_dependencies(attributes) ⇒ Object



173
174
175
176
177
178
# File 'lib/glue_gun/dsl.rb', line 173

def initialize_dependencies(attributes)
  self.class.dependency_definitions.each do |component_type, definition|
    value = attributes[component_type] || self.class.hardcoded_dependencies[component_type]
    instance_variable_set("@#{component_type}", initialize_dependency(component_type, value, definition))
  end
end

#initialize_dependency(component_type, init_args = nil, definition = nil) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/glue_gun/dsl.rb', line 180

def initialize_dependency(component_type, init_args = nil, definition = nil)
  definition ||= self.class.dependency_definitions[component_type]
  is_array = init_args.is_a?(Array)
  is_hash = definition.is_hash?(init_args)

  if init_args.nil? && definition.default_option_name.nil?
    return nil if definition.lazy?
    if definition.when_block.nil?
      raise "No default option or when block present for component_type #{component_type}. Don't know how to build!"
    end
  end

  if is_array
    dep = []
    config = []
    Array(init_args).each do |args|
      d, c = definition.initialize_single_dependency(args, self)
      dep.push(d)
      config.push(c)
    end
  elsif is_hash
    dep = {}
    config = {}
    definition.validate_hash_dependencies(init_args)

    init_args.each do |key, args|
      d, c = definition.initialize_single_dependency(args, self)
      dep[key] = d
      config[key] = c
    end
  else
    dep, config = definition.initialize_single_dependency(init_args, self)
  end

  dependencies[component_type] = {
    instance: dep,
    option: config
  }

  dep
end

#initialized?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/glue_gun/dsl.rb', line 169

def initialized?
  @initialized == true
end

#inspectObject



275
276
277
# File 'lib/glue_gun/dsl.rb', line 275

def inspect
  "#<#{self.class} #{attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(", ")}>"
end

#propagate_attribute_change(attr_name, value) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/glue_gun/dsl.rb', line 232

def propagate_attribute_change(attr_name, value)
  self.class.dependency_definitions.each do |component_type, _builder|
    dependency_instance = send(component_type)

    if dependency_instance.is_a?(Array)
      option_config = dependencies.dig(component_type, :option)

      dependency_instance.zip(option_config).each do |dep, opt|
        propagate_attribute_to_instance(attr_name, value, dep, opt)
      end
    elsif dependency_instance.is_a?(Hash)
      option_config = dependencies.dig(component_type, :option)

      dependency_instance.each do |key, dep|
        propagate_attribute_to_instance(attr_name, value, dep, option_config[key])
      end
    else
      option_config = dependencies.dig(component_type, :option)
      next unless option_config

      propagate_attribute_to_instance(attr_name, value, dependency_instance, option_config)
    end
  end
end

#propagate_attribute_to_instance(attr_name, value, dependency_instance, option_config) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/glue_gun/dsl.rb', line 257

def propagate_attribute_to_instance(attr_name, value, dependency_instance, option_config)
  bound_attrs = option_config.attributes.select do |_, attr_config|
    (attr_config.source == attr_name.to_sym) || (attr_config.name == attr_name.to_sym)
  end

  bound_attrs.each do |dep_attr_name, config_attr|
    block = config_attr.block.present? ? config_attr.block : proc { |att| att }
    if dependency_instance.respond_to?("#{dep_attr_name}=")
      dependency_instance.send("#{dep_attr_name}=",
                               block.call(value))
    end
  end
end

#propagate_changesObject



222
223
224
225
226
227
228
229
230
# File 'lib/glue_gun/dsl.rb', line 222

def propagate_changes
  changed_attributes.each do |attr_name, _old_value|
    new_value = read_attribute(attr_name)
    propagate_attribute_change(attr_name, new_value)
  end

  # Clear the changes after propagation
  changes_applied
end

#validate_dependenciesObject



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/glue_gun/dsl.rb', line 279

def validate_dependencies
  errors.clear
  self.class.dependency_definitions.keys.each do |component_type|
    dependency = send(component_type)

    # Only validate if the dependency responds to `valid?`
    next unless dependency.respond_to?(:valid?) && !dependency.valid?

    dependency.errors.each do |error|
      if error.is_a?(ActiveModel::Error)
        attribute = error.attribute
        message = error.message
      end
      errors.add("#{component_type}.#{attribute}", message)
    end
  end
  errors.none?
end