class Dreader::Util

Utilities function to simplify importing data into ActiveRecords

Public Class Methods

clean(hash, keys) click to toggle source

remove all `keys` from `hash`

# File lib/dreader.rb, line 79
def self.clean hash, keys
  hash.reject { |k, v| keys.include?(k) }
end
prepend(hash, key) click to toggle source

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}}
# File lib/dreader.rb, line 92
def self.prepend hash, key
  {key => hash}
end
restructure(hash, kept, subordinate_key, moved_keys) click to toggle source

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 = {name: “A”, surname: “B”, address: “via XX Settembre”, city: “Genoa”} restructure hash, [:name, :surname], :address_attributes, [:address, :city] {name: “A”, surname: “B”, address_attributes: {address: “via XX Settembre”, city: “Genoa”}}

# 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) click to toggle source

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

Example

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

# 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) click to toggle source

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

# File lib/dreader.rb, line 74
def self.slice hash, keys 
  hash.slice *keys
end