Class: PalletJack::KeyTransformer
- Inherits:
-
Object
- Object
- PalletJack::KeyTransformer
- Defined in:
- lib/palletjack/keytransformer.rb
Defined Under Namespace
Instance Method Summary collapse
-
#initialize(key_transforms = {}) ⇒ KeyTransformer
constructor
A new instance of KeyTransformer.
-
#transform!(pallet) ⇒ Object
Destructively transform the values in
palletaccording to the loaded transform rules.
Constructor Details
#initialize(key_transforms = {}) ⇒ KeyTransformer
Returns a new instance of KeyTransformer.
206 207 208 |
# File 'lib/palletjack/keytransformer.rb', line 206 def initialize(key_transforms = {}) @key_transforms = key_transforms end |
Instance Method Details
#transform!(pallet) ⇒ Object
Destructively transform the values in pallet according to the loaded transform rules.
YAML structure:
- key:
- transform1:
[transform-specific configuration]
- transform2:
[transform-specific configuration]
[...]
Transforms are evaluated in order from top to bottom, and the first one to successfully produce a value is used.
Transforms are methods in PalletJack::KeyTransformer::Writer, called by name. They should return the new value, or false if unsuccessful.
Transforms are given two parameters, param and context:
param-
transform-specific configuration from transforms.yaml
contextpallet-
The pallet object being processed
key-
The key from transforms.yaml being processed
value-
Current locally assigned value for key in pallet
abort-
#throw this to abort transforms for current key
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/palletjack/keytransformer.rb', line 237 def transform!(pallet) @key_transforms.each do |keytrans_item| # Enable early termination of transforms for a key # by wrapping execution in a catch block. catch do |abort_tag| key, transforms = keytrans_item.flatten context = { pallet: pallet, key: key, value: pallet[key, shallow: true], abort: abort_tag } transforms.each do |t| transform, param = t.flatten if self.respond_to?(transform.to_sym) then if new_value = self.send(transform.to_sym, param, context) then new_value = TraceableString.new(new_value) new_value.file = transform.file new_value.line = transform.line new_value.column = transform.column new_value.byte = transform.byte pallet[key] = new_value break end end end end end @hash end |