Class: Shrink::Wrap::Transformer::CollectionFromKey
- Includes:
- Support::TypeCheck
- Defined in:
- lib/shrink/wrap/transformer/collection_from_key.rb
Constant Summary
Constants inherited from Base
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ CollectionFromKey
constructor
A new instance of CollectionFromKey.
- #transform(input) ⇒ Object
Methods included from Support::TypeCheck
#ensure_callable!, #ensure_type!
Constructor Details
#initialize(opts = {}) ⇒ CollectionFromKey
Returns a new instance of CollectionFromKey.
9 10 11 12 13 14 15 |
# File 'lib/shrink/wrap/transformer/collection_from_key.rb', line 9 def initialize(opts = {}) unless (opts) raise ArgumentError, 'options must contain one key/value pair' end super end |
Instance Method Details
#transform(input) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/shrink/wrap/transformer/collection_from_key.rb', line 17 def transform(input) ensure_type!(Hash, input) from, to = .first data = input[from] return input unless data.is_a?(Hash) mapped = map(data, to) input.merge(from => mapped) end |