Class: Shrink::Wrap::Transformer::CollectionFromKey

Inherits:
Base
  • Object
show all
Includes:
Support::TypeCheck
Defined in:
lib/shrink/wrap/transformer/collection_from_key.rb

Constant Summary

Constants inherited from Base

Base::ATTRIBUTES

Instance Method Summary collapse

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 valid_options?(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 = options.first
  data = input[from]
  return input unless data.is_a?(Hash)

  mapped = map(data, to)
  input.merge(from => mapped)
end