Class: Orthoses::ActiveSupport::ClassAttribute
- Inherits:
-
Object
- Object
- Orthoses::ActiveSupport::ClassAttribute
- Defined in:
- lib/orthoses/active_support/class_attribute.rb
Overview
class Class
# >= v5.2
def class_attribute(*attrs)
# <= v6.0
def class_attribute(*attrs, instance_accessor: true,
instance_reader: instance_accessor, instance_writer: instance_accessor, instance_predicate: true, default: nil)
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(loader, if: nil) ⇒ ClassAttribute
constructor
A new instance of ClassAttribute.
Constructor Details
#initialize(loader, if: nil) ⇒ ClassAttribute
Returns a new instance of ClassAttribute.
12 13 14 15 |
# File 'lib/orthoses/active_support/class_attribute.rb', line 12 def initialize(loader, if: nil) @loader = loader @if = binding.local_variable_get(:if) end |
Instance Method Details
#call ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 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 62 |
# File 'lib/orthoses/active_support/class_attribute.rb', line 17 def call lazy_tracer = Orthoses::CallTracer::Lazy.new store = lazy_tracer.trace('Class#class_attribute') do @loader.call end lazy_tracer.captures.each do |capture| receiver_name = Orthoses::Utils.module_name(capture.method.receiver) next unless receiver_name methods = [] if ::ActiveSupport::VERSION::MAJOR < 6 = capture.argument[:attrs]. capture.argument[:instance_reader] = .fetch(:instance_accessor, true) && .fetch(:instance_reader, true) capture.argument[:instance_writer] = .fetch(:instance_accessor, true) && .fetch(:instance_writer, true) capture.argument[:instance_predicate] = .fetch(:instance_predicate, true) capture.argument[:default_value] = .fetch(:default, nil) end content = store[receiver_name] capture.argument[:attrs].each do |name| next unless @if.nil? || @if.call(method, name) # skip internal attribute next if name.to_s.start_with?("_") next if name == :attribute_type_decorations next if name == :attributes_to_define_after_schema_loads methods << "def self.#{name}: () -> untyped" methods << "def self.#{name}?: () -> bool" if capture.argument[:instance_predicate] methods << "def self.#{name}=: (untyped value) -> untyped" methods << "def #{name}: () -> untyped" if capture.argument[:instance_reader] methods << "def #{name}?: () -> bool" if capture.argument[:instance_predicate] && capture.argument[:instance_reader] methods << "def #{name}=: (untyped value) -> untyped" if capture.argument[:instance_writer] # In RBS, `foo=` and attr_writer :foo cannot live together. content.body.delete_if { |line| line.start_with?("attr_writer #{name}:") } end next if methods.empty? content.concat(methods) end store end |