Class: Jinx::AttributeEnumerator

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

Overview

A filter on the standard attribute symbol => metadata hash that yields each attribute which satisfies the attribute metadata condition.

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(hash) {|prop| ... } ⇒ AttributeEnumerator

Returns a new instance of AttributeEnumerator.

Parameters:

  • hash ({Symbol => Property})

    the attribute symbol => metadata hash

Yields:

  • (prop)

    optional condition which determines whether the attribute is selected (default is all attributes)

Yield Parameters:

  • the (Property)

    metadata for the standard attribute

Raises:

  • (ArgumentError)

    if a parameter is missing



14
15
16
17
18
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 14

def initialize(hash, &filter)
  raise ArgumentError.new("Attribute filter missing hash argument") if hash.nil?
  @hash = hash
  @filter = block_given? ? filter : Proc.new { true }
end

Instance Method Details

#compose {|prop| ... } ⇒ AttributeEnumerator

Returns a new eumerator which applies the filter block given to this method with the Property enumerated by this enumerator.

Yields:

  • (prop)

    the attribute selection filter

Yield Parameters:

  • prop (Property)

    the candidate attribute metadata

Returns:

  • (AttributeEnumerator)

    a new eumerator which applies the filter block given to this method with the Property enumerated by this enumerator



63
64
65
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 63

def compose
  AttributeEnumerator.new(@hash) { |prop| yield(prop) if @filter.call(prop) }
end

#detect_attribute_with_property {|prop| ... } ⇒ Symbol

Returns the first attribute whose metadata satisfies the block.

Yields:

  • (prop)

    the block to apply to the attribute metadata

Yield Parameters:

  • prop (Property)

    the attribute metadata

Returns:

  • (Symbol)

    the first attribute whose metadata satisfies the block



54
55
56
57
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 54

def detect_attribute_with_property
  each_pair { |pa, prop| return pa if yield(prop) }
  nil
end

#each_attribute {|attribute| ... } ⇒ Object Also known as: each

Yields:

  • (attribute)

    block to apply to each filtered attribute

Yield Parameters:

  • the (Symbol)

    attribute which satisfies the filter condition



34
35
36
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 34

def each_attribute(&block)
  each_pair { |pa, prop| yield(pa) }
end

#each_pair {|attribute, prop| ... } ⇒ Object

Yields:

  • (attribute, prop)

    the block to apply to the filtered attribute metadata and attribute

Yield Parameters:

  • attribute (Symbol)

    the attribute

  • prop (Property)

    the attribute metadata



23
24
25
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 23

def each_pair
  @hash.each { |pa, prop| yield(pa, prop) if @filter.call(prop) }
end

#each_property {|prop| ... } ⇒ Object

Yields:

  • (prop)

    the block to apply to the filtered attribute metadata

Yield Parameters:

  • prop (Property)

    the attribute metadata



42
43
44
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 42

def each_property
  each_pair { |pa, prop| yield(prop) }
end

#enum_pairs<(Symbol, Property)>

Returns the (symbol, attribute) enumerator.

Returns:



28
29
30
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 28

def enum_pairs
  enum_for(:each_pair)
end

#properties<Property>

Returns the property enumerator.

Returns:

  • (<Property>)

    the property enumerator



47
48
49
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 47

def properties
  @prop_enum ||= enum_for(:each_property)
end