Class: Jinx::AttributeEnumerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
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

#collection?, #compact, #compact_map, #detect_value, #detect_with_value, #difference, #empty?, #enumerate, #filter, #first, #flatten, #hashify, #intersect, #join, #last, #partial_sort, #partial_sort_by, #pp_s, #pretty_print, #pretty_print_cycle, #qp, #size, #to_compact_hash, #to_compact_hash_with_index, #to_enum, #to_series, #transform, #transitive_closure, #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



12
13
14
15
16
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 12

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



69
70
71
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 69

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

#detect_property {|attribute| ... } ⇒ Property

Returns the first attribute metadata whose attribute satisfies the block.

Yields:

  • (attribute)

    the block to apply to the attribute

Yield Parameters:

  • attribute (Symbol)

    the attribute to detect

Returns:

  • (Property)

    the first attribute metadata whose attribute satisfies the block



52
53
54
55
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 52

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

#detect_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



60
61
62
63
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 60

def detect_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



32
33
34
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 32

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



21
22
23
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 21

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



40
41
42
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 40

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

#enum_pairs<(Symbol, Property)>

Returns the (symbol, attribute) enumerator.

Returns:



26
27
28
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 26

def enum_pairs
  enum_for(:each_pair)
end

#properties<Property>

Returns the property enumerator.

Returns:

  • (<Property>)

    the property enumerator



45
46
47
# File 'lib/jinx/metadata/attribute_enumerator.rb', line 45

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