Class: ProjectAdapter::Adapter

Inherits:
Object
  • Object
show all
Defined in:
app/concerns/project_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, adapter_module) ⇒ Adapter

Returns a new instance of Adapter.



28
29
30
31
32
33
# File 'app/concerns/project_adapter.rb', line 28

def initialize(model, adapter_module)
  @model          = model
  @namespace      = adapter_module
  @name           = adapter_module.name
  @attribute_name = name.demodulize.underscore
end

Instance Attribute Details

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



35
36
37
# File 'app/concerns/project_adapter.rb', line 35

def attribute_name
  @attribute_name
end

#modelObject (readonly)

Returns the value of attribute model.



35
36
37
# File 'app/concerns/project_adapter.rb', line 35

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



35
36
37
# File 'app/concerns/project_adapter.rb', line 35

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



35
36
37
# File 'app/concerns/project_adapter.rb', line 35

def namespace
  @namespace
end

Instance Method Details

#adapter_methodObject



49
50
51
# File 'app/concerns/project_adapter.rb', line 49

def adapter_method
  :"#{attribute_name}_adapter"
end

#before_update_methodObject



45
46
47
# File 'app/concerns/project_adapter.rb', line 45

def before_update_method
  :"#{attribute_name}_before_update"
end

#define_methods!Object



57
58
59
60
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
88
89
90
91
92
93
94
# File 'app/concerns/project_adapter.rb', line 57

def define_methods!
  model.module_eval <<-RUBY
    def self.with_#{attribute_name}
      where arel_table[:#{attribute_name}_name].not_eq("None")
    end

    def has_#{attribute_name}?
      #{attribute_name}_name != "None"
    end

    def #{validation_method}
      #{adapter_method}.errors_with_parameters(self, *#{params_method}.values).each do |attribute, messages|
        errors.add(attribute, messages) if messages.any?
      end
    end

    def #{before_update_method}
      return true unless #{attribute_name}.respond_to?(:before_update)
      #{attribute_name}.before_update(self)
    end

    def #{attribute_name}
      @#{attribute_name} ||= #{adapter_method}
          .build(self, *#{params_method}.values)
          .extend(FeatureSupport)
    end

    def #{params_method}
      #{adapter_method}.parameters.each_with_object({}) do |parameter, hash|
        hash[parameter] = extended_attributes[parameter.to_s]
      end
    end

    def #{adapter_method}
      #{namespace}.adapter(#{attribute_name}_name)
    end
  RUBY
end

#params_methodObject



53
54
55
# File 'app/concerns/project_adapter.rb', line 53

def params_method
  :"parameters_for_#{attribute_name}_adapter"
end

#titleObject



37
38
39
# File 'app/concerns/project_adapter.rb', line 37

def title
  name.demodulize.titleize
end

#validation_methodObject



41
42
43
# File 'app/concerns/project_adapter.rb', line 41

def validation_method
  :"#{attribute_name}_configuration_is_valid"
end