Class: Pyper::Pipes::FieldRename

Inherits:
Struct
  • Object
show all
Defined in:
lib/pyper/pipes/field_rename.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attr_mapObject

Returns the value of attribute attr_map

Returns:

  • (Object)

    the current value of attr_map



3
4
5
# File 'lib/pyper/pipes/field_rename.rb', line 3

def attr_map
  @attr_map
end

Instance Method Details

#pipe(attrs_or_items, status = {}) ⇒ Hash|Enumerator<Hash>

Returns The item(s) with fields renamed.

Parameters:

  • args (Hash|Enumerator<Hash>)

    One or more item hashes

  • status (Hash) (defaults to: {})

    The mutable status field

Returns:

  • (Hash|Enumerator<Hash>)

    The item(s) with fields renamed



8
9
10
11
12
13
# File 'lib/pyper/pipes/field_rename.rb', line 8

def pipe(attrs_or_items, status = {})
  case attrs_or_items
  when Hash then rename(attrs_or_items)
  else attrs_or_items.map { |item| rename(item) }
  end
end

#rename(item) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/pyper/pipes/field_rename.rb', line 15

def rename(item)
  attr_map.each do |old,new|
    item[new.to_sym] = item.delete(old.to_sym) if item.has_key?(old.to_sym)
    item[new.to_s] = item.delete(old.to_s) if item.has_key?(old.to_s)
  end
  item
end