Class: Fech::Mapped

Inherits:
Hash
  • Object
show all
Defined in:
lib/fech/mapped.rb

Overview

Fech::Mapped is a thin wrapper around Hash which allows values to be referenced either by key or by an alias specified in the associated Filing’s Translations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filing, row_type) ⇒ Mapped

Returns a new instance of Mapped.



11
12
13
14
# File 'lib/fech/mapped.rb', line 11

def initialize(filing, row_type)
  @filing   = filing
  @row_type = row_type
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



33
34
35
# File 'lib/fech/mapped.rb', line 33

def method_missing(method, *args, &block)
  self[method]
end

Instance Attribute Details

#filingObject

Returns the value of attribute filing.



8
9
10
# File 'lib/fech/mapped.rb', line 8

def filing
  @filing
end

#row_typeObject

Returns the value of attribute row_type.



8
9
10
# File 'lib/fech/mapped.rb', line 8

def row_type
  @row_type
end

Instance Method Details

#[](key, &block) ⇒ Object

Just calls Hash’s [] method, unless the specified key doesn’t exist, in which case it checks for any aliases on the filing’s translator.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fech/mapped.rb', line 19

def [](key, &block)
  if has_key?(key)
    old_bracket(key, &block)
  else
    # Look up aliases in reverse, to find the most recent one
    # Does not allow (obvious) recursion
    aliias = filing.translator.aliases.reverse.detect do |a|
      a[:alias] == key && a[:row].match(row_type) && a[:alias] != a[:for]
    end
    # Pass the key this alias references back to this function
    aliias ? old_bracket(aliias[:for], &block) : nil
  end
end

#old_bracketObject



9
# File 'lib/fech/mapped.rb', line 9

alias :old_bracket :[]