module Aspire::Object::ResourcePropertyMixin

Delegates citation_<property> method calls to the parent resource

Constants

CITATION_PROPERTIES

Public Instance Methods

get_property_name_from_method(method_name, _include_all = false) click to toggle source

Returns the property name from a citation_<property> missing method call @param method_name [Symbol] the method name @param _include_all [Boolean] if true, include private/protected methods @return [String, nil] the property name, or nil if not a valid property

# File lib/aspire/object/resource.rb, line 43
def get_property_name_from_method(method_name, _include_all = false)
  # Ignore method names not beginning with citation_
  return nil unless method_name.to_s.start_with?('citation_')
  # Remove the 'citation_' prefix to get the property name
  property = method_name[9..-1]
  return nil if property.nil? || property.empty?
  # Accept only whitelisted properties
  return nil unless CITATION_PROPERTIES.include?(property)
  # Accept only properties with accessor methods
  return nil unless respond_to?(property)
  # Return the property name
  property
end
method_missing(method_name, *args, &block) click to toggle source

Handles citation_<property>() accessor calls by proxying to the parent resource if no instance value is set. The <property> accessor for this instance is called first, and if this returns nil and there is a parent resource (is_part_of), the property accessor of the parent is called. This continues up through the ancestor resources until a value is found. @param method_name [Symbol] the method name Positional and keyword arguments are passed to the property accessor

Calls superclass method
# File lib/aspire/object/resource.rb, line 22
def method_missing(method_name, *args, &block)
  property_name = get_property_name_from_method(method_name)
  super if property_name.nil?
  # Try the resource's property first
  value = public_send(property_name, *args, &block)
  return value unless value.nil?
  # Delegate to the parent resource's property if it exists
  #   Call the parent's citation_<property> rather than <property> to
  #   delegate up the ancestor chain.
  if is_part_of
    value = is_part_of.public_send(method_name, *args, &block)
    return value unless value.nil?
  end
  # Otherwise return nil
  nil
end
respond_to_missing?(method_name, include_all = false) click to toggle source

Returns true if this method is supported, false if not @param method_name [Symbol] the method name @param include_all [Boolean] if true, include private/protected methods @return [Boolean] true if the method is supported, false otherwise

# File lib/aspire/object/resource.rb, line 61
def respond_to_missing?(method_name, include_all = false)
  property_name = get_property_name_from_method(method_name, include_all)
  # property_name is not nil if the method is supported
  !property_name.nil?
end