Class: Dreader::Util
- Inherits:
-
Object
- Object
- Dreader::Util
- Defined in:
- lib/dreader.rb
Overview
Utilities function to simplify importing data into ActiveRecords
Class Method Summary collapse
-
.clean(hash, keys) ⇒ Object
remove all ‘keys` from `hash`.
-
.prepend(hash, key) ⇒ Object
given a hash, return a new hash with key and whose value is the hash.
-
.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”.
-
.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.
-
.slice(hash, *keys) ⇒ Object
an alias for Hash.slice.
Class Method Details
.clean(hash, keys) ⇒ Object
remove all ‘keys` from `hash`
78 79 80 |
# File 'lib/dreader.rb', line 78 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}}
91 92 93 |
# File 'lib/dreader.rb', line 91 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
73 74 75 |
# File 'lib/dreader.rb', line 73 def self.slice hash, *keys hash.slice keys end |