Module: GlueGun::Model

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

Defined Under Namespace

Classes: ServiceRegistry

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **kwargs, &block) ⇒ Object (private)



245
246
247
248
249
250
251
252
253
# File 'lib/glue_gun/model.rb', line 245

def method_missing(method_name, *args, **kwargs, &block)
  service_object = instance_variable_get("@#{service_attribute_name}")

  if service_object && service_object.respond_to?(method_name)
    service_object.send(method_name, *args, **kwargs, &block)
  else
    super
  end
end

Instance Method Details

#assign_service_attributes(attributes) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/glue_gun/model.rb', line 126

def assign_service_attributes(attributes)
  return if attributes.blank?

  service_object = instance_variable_get("@#{service_attribute_name}")
  return unless service_object.present?

  extract_service_attributes(attributes, service_object.class).each do |attr_name, value|
    unless service_object.respond_to?("#{attr_name}=")
      raise NoMethodError, "Undefined attribute #{attr_name} for #{service_object.class.name}"
    end

    service_object.send("#{attr_name}=", value)
    attribute_will_change!(attr_name.to_s)
  end
end

#initialize(attributes = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/glue_gun/model.rb', line 116

def initialize(attributes = {})
  attributes = {} if attributes.nil?
  attributes = attributes.to_h.deep_symbolize_keys
  attributes[:root_dir] ||= detect_root_dir
  attributes[option_key] ||= resolve_service_type(attributes, true)
  db_attributes = self.class.extract_db_attributes(attributes)
  super(db_attributes)
  build_service_object(attributes)
end