Class: Foobara::ActiveRecordThunk

Inherits:
BasicObject
Defined in:
foobara-active-record-type-0.0.14/src/active_record_thunk.rb

Overview

NOTE: it can help when debugging to comment out the inheritance from BasicObject

Instance Method Summary collapse

Constructor Details

#initialize(active_record_class, primary_key) ⇒ ActiveRecordThunk

Returns a new instance of ActiveRecordThunk.



4
5
6
7
8
9
10
# File 'foobara-active-record-type-0.0.14/src/active_record_thunk.rb', line 4

def initialize(active_record_class, primary_key)
  super()

  @foobara_active_record_class = active_record_class
  @foobara_primary_key_attribute = active_record_class.foobara_type.declaration_data[:primary_key]
  @foobara_primary_key = primary_key
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

BasicObject doesn’t have .respond_to? nor .respond_to_missing? so disable this cop rubocop:disable Style/MissingRespondToMissing



14
15
16
17
18
19
20
21
22
# File 'foobara-active-record-type-0.0.14/src/active_record_thunk.rb', line 14

def method_missing(method_name, ...)
  if method_name == @foobara_primary_key_attribute
    return @foobara_primary_key
  end

  foobara_load_if_needed(method_name)

  @foobara_active_record.send(method_name, ...)
end

Instance Method Details

#==(other) ⇒ Object

rubocop:enable Style/TrivialAccessors



54
55
56
57
58
# File 'foobara-active-record-type-0.0.14/src/active_record_thunk.rb', line 54

def ==(other)
  return false unless @foobara_primary_key

  other.instance_of?(@foobara_active_record_class) && @foobara_primary_key == other.id
end

#foobara_active_record_classObject

BasicObject doesn’t have .attr_reader rubocop:disable Style/TrivialAccessors



41
42
43
# File 'foobara-active-record-type-0.0.14/src/active_record_thunk.rb', line 41

def foobara_active_record_class
  @foobara_active_record_class
end

#foobara_primary_keyObject



49
50
51
# File 'foobara-active-record-type-0.0.14/src/active_record_thunk.rb', line 49

def foobara_primary_key
  @foobara_primary_key
end

#foobara_primary_key_attributeObject



45
46
47
# File 'foobara-active-record-type-0.0.14/src/active_record_thunk.rb', line 45

def foobara_primary_key_attribute
  @foobara_primary_key_attribute
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

rubocop:enable Style/MissingRespondToMissing

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'foobara-active-record-type-0.0.14/src/active_record_thunk.rb', line 25

def respond_to?(method_name, include_private = false)
  method_name = method_name.to_sym

  case method_name
  when :foobara_load_if_needed
    include_private
  when :foobara_active_record_class, :foobara_primary_key_attribute, :foobara_primary_key
    true
  else
    @foobara_active_record.respond_to?(method_name, include_private) ||
      foobara_active_record_class.instance_methods.include?(method_name)
  end
end