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.



25
26
27
28
29
30
# File 'app/concerns/project_adapter.rb', line 25

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.



32
33
34
# File 'app/concerns/project_adapter.rb', line 32

def attribute_name
  @attribute_name
end

#modelObject (readonly)

Returns the value of attribute model.



32
33
34
# File 'app/concerns/project_adapter.rb', line 32

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'app/concerns/project_adapter.rb', line 32

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



32
33
34
# File 'app/concerns/project_adapter.rb', line 32

def namespace
  @namespace
end

Instance Method Details

#adapter_methodObject



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

def adapter_method
  :"#{attribute_name}_adapter"
end

#adapter_name_methodObject



58
59
60
# File 'app/concerns/project_adapter.rb', line 58

def adapter_name_method
  :"#{attribute_name}_name"
end

#before_update_methodObject



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

def before_update_method
  :"#{attribute_name}_before_update"
end

#concern_nameObject



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

def concern_name
  :"#{name.demodulize}Concern"
end

#define_methods!Object



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/concerns/project_adapter.rb', line 66

def define_methods!
  concern = ProjectAdapter.const_set(concern_name, Module.new)
  concern.extend ActiveSupport::Concern

  class_methods = concern.const_set(:ClassMethods, Module.new)
  class_methods.module_eval <<-RUBY, __FILE__ , __LINE__ + 1
    def with_#{attribute_name}(value=nil)
      if value
        where [ "COALESCE(projects.props->>'#{prop_name}', 'None') = ?", value ]
      else
        where "COALESCE(projects.props->>'#{prop_name}', 'None') != 'None'"
      end
    end
  RUBY

  concern.module_eval <<-RUBY, __FILE__ , __LINE__ + 1
    included do
      validate :#{validation_method}
      before_update :#{before_update_method}
    end

    def #{adapter_name_method}
      props["#{prop_name}"] || "None"
    end

    def has_#{attribute_name}?
      #{adapter_name_method} != "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] = props[parameter.to_s]
      end
    end

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

  model.send :include, concern
end

#params_methodObject



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

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

#prop_nameObject



62
63
64
# File 'app/concerns/project_adapter.rb', line 62

def prop_name
  "adapter.#{attribute_name.to_s.camelize(:lower)}"
end

#titleObject



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

def title
  name.demodulize.titleize
end

#validation_methodObject



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

def validation_method
  :"#{attribute_name}_configuration_is_valid"
end