Class: Jinx::ReferenceEnumerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Collection
Defined in:
lib/jinx/resource/reference_enumerator.rb

Overview

A ReferenceEnumerator iterates over domain property references.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#enumerate, #pp_s, #pretty_print, #pretty_print_cycle, #qp, #to_enum, #transitive_closure

Methods included from Collection

#compact, #compact_map, #detect_value, #detect_with_value, #difference, #empty?, #filter, #first, #flatten, #hashify, #intersect, #join, #last, #partial_sort, #partial_sort!, #partial_sort_by, #size, #to_compact_hash, #to_compact_hash_with_index, #to_series, #transform, #union

Constructor Details

#initialize(on = nil, properties = nil) ⇒ ReferenceEnumerator

Returns a new instance of ReferenceEnumerator.

Parameters:

  • on (Resource, nil) (defaults to: nil)

    the object containing the references

  • properties (<Property>, Property, nil) (defaults to: nil)

    the property or properties to dereference



25
26
27
28
# File 'lib/jinx/resource/reference_enumerator.rb', line 25

def initialize(on=nil, properties=nil)
  @subject = on
  @properties = properties
end

Instance Attribute Details

#property<Property> (readonly)

Returns the current property.

Returns:



21
22
23
# File 'lib/jinx/resource/reference_enumerator.rb', line 21

def property
  @property
end

#subjectResource (readonly) Also known as: on, from

Returns the domain object containing the references.

Returns:

  • (Resource)

    the domain object containing the references



14
15
16
# File 'lib/jinx/resource/reference_enumerator.rb', line 14

def subject
  @subject
end

Instance Method Details

#each {|obj, from, property| ... } ⇒ (Resource, Resource, Property)

Returns the (visited, visiting, property) tuples.

Parameters:

  • obj (Resource)

    the visiting domain object

Yields:

  • (obj, from, property)

    operates on the visited domain object

Yield Parameters:

  • obj (Resource)

    the visited domain object

  • from (Resource)

    the visiting domain object

  • property (Property)

    the visiting property

Returns:



36
37
38
39
40
41
42
43
44
45
# File 'lib/jinx/resource/reference_enumerator.rb', line 36

def each
  return if @subject.nil?
  @properties.enumerate do |prop|
    @property = prop
    # the reference(s) to visit
    refs = @subject.send(prop.attribute)
    # associate each reference to visit with the current visited attribute
    refs.enumerate { |ref| yield(ref) }
  end
end