Class: Orthoses::ActiveModel::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/orthoses/active_model/attributes.rb

Overview

< 7.0

def attribute(name, type = Type::Value.new, **options)

>= 7.0

def attribute(name, cast_type = nil, default: NO_DEFAULT_PROVIDED, **options)

Constant Summary collapse

DEFAULT_TYPES =
{
  big_integer: '::Integer',
  binary: '::String',
  boolean: 'bool',
  date: '::Date',
  datetime: '::Time',
  decimal: '::BigDecimal',
  float: '::Float',
  immutable_string: '::String',
  integer: '::Integer',
  string: '::String',
  time: '::Time'
}

Instance Method Summary collapse

Constructor Details

#initialize(loader) ⇒ Attributes

Returns a new instance of Attributes.



24
25
26
# File 'lib/orthoses/active_model/attributes.rb', line 24

def initialize(loader)
  @loader = loader
end

Instance Method Details

#callObject



28
29
30
31
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
# File 'lib/orthoses/active_model/attributes.rb', line 28

def call
  attribute = CallTracer::Lazy.new
  store = attribute.trace('ActiveModel::Attributes::ClassMethods#attribute') do
    @loader.call
  end
  attribute.captures.each do |capture|
    receiver_name = Utils.module_name(capture.method.receiver) or next
    name = capture.argument[:name]
    active_model_version = Gem::Version.new(::ActiveModel::VERSION::STRING)
    cast_type =
      if active_model_version >= Gem::Version.new('7.1')
        # https://github.com/rails/rails/commit/608cbfae36b125d7962b7ed9083c9e9e6ce70b88
        capture.argument[:*].first
      elsif active_model_version >= Gem::Version.new('7.0')
        capture.argument[:cast_type]
      else
        capture.argument[:type]
      end

    return_type = DEFAULT_TYPES[cast_type] || 'untyped'

    generated_attribute_methods = "#{receiver_name}::ActiveModelGeneratedAttributeMethods"
    c = store[generated_attribute_methods]
    c.header = "module #{generated_attribute_methods}"
    c << "def #{name}: () -> #{return_type}?"
    c << "def #{name}=: (untyped) -> untyped"

    unless store[receiver_name].body.include?("include #{generated_attribute_methods}")
      store[receiver_name] << "include #{generated_attribute_methods}"
    end
  end

  store
end