Class: Orthoses::ActiveRecord::GeneratedAttributeMethods

Inherits:
Object
  • Object
show all
Defined in:
lib/orthoses/active_record/generated_attribute_methods.rb

Constant Summary collapse

TARGET_TYPE_MAP =
{
  "attribute?" => "() -> bool",
  "attribute_before_last_save" => "() -> %<opt>s",
  "attribute_before_type_cast" => "() -> %<type>s",
  "attribute_came_from_user?" => "() -> bool",
  "attribute_change" => "() -> [%<opt>s, %<opt>s]",
  "attribute_change_to_be_saved" => "() -> Array[%<opt>s]?",
  "attribute_changed?" => "() -> bool",
  "attribute_for_database" => "() -> %<type>s",
  "attribute_in_database" => "() -> %<opt>s",
  "attribute_previous_change" => "() -> Array[%<opt>s]?",
  "attribute_previously_changed?" => "() -> bool",
  "attribute_previously_was" => "() -> %<opt>s",
  "attribute_was" => "() -> %<opt>s",
  "attribute_will_change!" => "() -> void",
  "clear_attribute_change" => "() -> void",
  "restore_attribute!" => "() -> void",
  "saved_change_to_attribute" => "() -> Array[%<opt>s]?",
  "saved_change_to_attribute?" => "() -> bool",
  "will_save_change_to_attribute?" => "() -> bool",
}

Instance Method Summary collapse

Constructor Details

#initialize(loader) ⇒ GeneratedAttributeMethods

Returns a new instance of GeneratedAttributeMethods.



28
29
30
# File 'lib/orthoses/active_record/generated_attribute_methods.rb', line 28

def initialize(loader)
  @loader = loader
end

Instance Method Details

#callObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/orthoses/active_record/generated_attribute_methods.rb', line 32

def call
  @loader.call.tap do |store|
    ::ActiveRecord::Base.descendants.each do |klass|
      next if klass.abstract_class?

      base_klass_name = Utils.module_name(klass) || next
      begin
        klass.columns
      rescue ::ActiveRecord::StatementInvalid => e
        Orthoses.logger.warn(e.to_s)
        next
      end

      lines = []
      klass.columns_hash.each do |name, col|
        req = ActiveRecord.sql_type_to_rbs(col.type)
        opt = "#{req}?"
        type = col.null ? opt : req

        lines << "attr_accessor #{name}: #{type}"
        attribute_method_patterns(::ActiveRecord::Base).each do |matcher|
          tmpl = TARGET_TYPE_MAP[matcher.proxy_target] or next
          lines << "def #{matcher.method_name(name)}: #{tmpl % {type: type, opt: opt}}"
        end
      end
      klass.attribute_aliases.each do |alias_name, column_name|
        attribute_method_patterns(::ActiveRecord::Base).each do |matcher|
          lines << "alias #{matcher.method_name(alias_name)} #{matcher.method_name(column_name)}"
        end
      end

      generated_attribute_methods = "#{base_klass_name}::GeneratedAttributeMethods"
      store[base_klass_name] << "include #{generated_attribute_methods}"

      store[generated_attribute_methods].header = "module #{generated_attribute_methods}"
      store[generated_attribute_methods].concat(lines)
    end
  end
end