Class: LcpRuby::Presenter::IncludesResolver::AssociationDependency

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/presenter/includes_resolver/association_dependency.rb

Overview

Value object representing a single association that needs eager loading.

Constant Summary collapse

VALID_REASONS =
%i[display query].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, reason:) ⇒ AssociationDependency

Returns a new instance of AssociationDependency.



16
17
18
19
20
21
# File 'lib/lcp_ruby/presenter/includes_resolver/association_dependency.rb', line 16

def initialize(path:, reason:)
  @path = path
  @reason = reason.to_sym

  validate!
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/lcp_ruby/presenter/includes_resolver/association_dependency.rb', line 14

def path
  @path
end

#reasonObject (readonly)

Returns the value of attribute reason.



14
15
16
# File 'lib/lcp_ruby/presenter/includes_resolver/association_dependency.rb', line 14

def reason
  @reason
end

Instance Method Details

#association_nameObject

Top-level association name regardless of nesting. :company from either :company or { company: :industry }



25
26
27
28
29
30
31
# File 'lib/lcp_ruby/presenter/includes_resolver/association_dependency.rb', line 25

def association_name
  case path
  when Symbol then path
  when Hash   then path.keys.first
  else raise ArgumentError, "Invalid path type: #{path.class}"
  end
end

#display?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/lcp_ruby/presenter/includes_resolver/association_dependency.rb', line 41

def display?
  reason == :display
end

#nested?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/lcp_ruby/presenter/includes_resolver/association_dependency.rb', line 33

def nested?
  path.is_a?(Hash)
end

#query?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/lcp_ruby/presenter/includes_resolver/association_dependency.rb', line 37

def query?
  reason == :query
end