Class: Dreader::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/dreader.rb

Overview

Utilities function to simplify importing data into ActiveRecords

Class Method Summary collapse

Class Method Details

.clean(hash, keys) ⇒ Object

remove all ‘keys` from `hash`



79
80
81
# File 'lib/dreader.rb', line 79

def self.clean hash, keys
  hash.reject { |k, v| keys.include?(k) }
end

.prepend(hash, key) ⇒ Object

given a hash, return a new hash with key and whose value is the hash

Example:

hash = {name: "A", size: 10}
prepend hash, :product_attributes
{product_attributes: {name: "A", size: 10}}


92
93
94
# File 'lib/dreader.rb', line 92

def self.prepend hash, key
  {key => hash}
end

.restructure(hash, kept, subordinate_key, moved_keys) ⇒ Object

given a hash returned by Engine, keep the “kept” keys in the top of the hierarchy and move the “moved_key” below the “subordinate_key”

Example

hash = “A”, surname: “B”, address: “via XX Settembre”, city: “Genoa” restructure hash, [:name, :surname], :address_attributes, [:address, :city] “A”, surname: “B”, address_attributes: {address: “via XX Settembre”, city: “Genoa”}



66
67
68
69
70
# File 'lib/dreader.rb', line 66

def self.restructure hash, kept, subordinate_key, moved_keys
  head = hash.slice kept
  subordinate = self.prepend subordinate_key, hash.slice(moved_keys)
  head.merge subordinate
end

.simplify(hash) ⇒ Object

given a hash returned by Engine, return the same hash with keys directly bound to the content of the :value sub-key

Example

hash = {value: “A”, …, surname: “B”, …} simplify hash “A”, surname: “B”



50
51
52
53
54
# File 'lib/dreader.rb', line 50

def self.simplify hash
  new_hash = {}
  hash.keys.map { |k| new_hash[k] = hash[k][:value] }
  new_hash
end

.slice(hash, keys) ⇒ Object

an alias for Hash.slice keys is an array of keys



74
75
76
# File 'lib/dreader.rb', line 74

def self.slice hash, keys 
  hash.slice *keys
end