Module: Aspire::Object::ResourcePropertyMixin

Included in:
Resource
Defined in:
lib/aspire/object/resource.rb

Overview

Delegates citation_<property> method calls to the parent resource

Constant Summary collapse

CITATION_PROPERTIES =
%w[
  authors book_jacket_url date doi edition edition_data eissn has_part
  is_part_of isbn10 isbn13 isbns issn issue issued latest_edition
  local_control_number online_resource page page_end page_start
  place_of_publication publisher title type url volume
].freeze

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

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. Positional and keyword arguments are passed to the property accessor

Parameters:

  • method_name (Symbol)

    the method name



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Method Details

#get_property_name_from_method(method_name, _include_all = false) ⇒ String?

Returns the property name from a citation_<property> missing method call

Parameters:

  • method_name (Symbol)

    the method name

  • _include_all (Boolean) (defaults to: false)

    if true, include private/protected methods

Returns:

  • (String, nil)

    the property name, or nil if not a valid property



43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

#respond_to_missing?(method_name, include_all = false) ⇒ Boolean

Returns true if this method is supported, false if not

Parameters:

  • method_name (Symbol)

    the method name

  • include_all (Boolean) (defaults to: false)

    if true, include private/protected methods

Returns:

  • (Boolean)

    true if the method is supported, false otherwise



61
62
63
64
65
# 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