Class: PalletJack::KeyTransformer::Writer
- Inherits:
-
PalletJack::KeyTransformer
- Object
- PalletJack::KeyTransformer
- PalletJack::KeyTransformer::Writer
- Defined in:
- lib/palletjack/keytransformer.rb
Instance Method Summary collapse
- #concatenate(param, context = {}) ⇒ Object
-
#inherit(_, context = {}) ⇒ Object
Synthesized value will override an inherited value for a
key, but in some cases the intent is actually to only synthesize a value when there is no inherited value. -
#synthesize(param, context = {}) ⇒ Object
Synthesize a pallet value by pasting others together.
-
#synthesize_regexp(param, context = {}) ⇒ Object
Synthesize a pallet value from others, using regular expressions to pull out parts of values.
Methods inherited from PalletJack::KeyTransformer
Constructor Details
This class inherits a constructor from PalletJack::KeyTransformer
Instance Method Details
#concatenate(param, context = {}) ⇒ Object
12 13 14 |
# File 'lib/palletjack/keytransformer.rb', line 12 def concatenate(param, context = {}) context[:value].join(param) if context[:value] end |
#inherit(_, context = {}) ⇒ Object
Synthesized value will override an inherited value for a key, but in some cases the intent is actually to only synthesize a value when there is no inherited value. This provides early termination of transforms for such keys.
Example:
- net.layer2.name:
- inherit: ~
- synthesize: "#[chassis.nic.name]"
201 202 203 |
# File 'lib/palletjack/keytransformer.rb', line 201 def inherit(_, context = {}) throw context[:abort] if context[:pallet][context[:key]] end |
#synthesize(param, context = {}) ⇒ Object
Synthesize a pallet value by pasting others together.
:call-seq:
synthesize(param, context) -> string or nil
If context contains a non-nil :value, an earlier transform has already produced a value for this key, so do nothing and return nil.
Otherwise, use the parsed YAML structure in param to build and return a new value. If any failure occurs while building the new value, return nil to let another transform try.
YAML structure:
- synthesize: "rule"
or
- synthesize:
- "rule"
- "rule"
...
Rules are strings used to build the new value. The value of another key is inserted by #[key], and all other characters are copied verbatim.
Rules are evaluated in order, and the first one to successfully produce a value without failing a key lookup is used.
Example:
- net.dns.fqdn:
- synthesize: "#[net.ip.name].#[domain.name]"
- chassis.nic.name:
- synthesize:
- "p#[chassis.nic.pcislot]p#[chassis.nic.port]"
- "em#[chassis.nic.port]"
110 111 112 113 114 |
# File 'lib/palletjack/keytransformer.rb', line 110 def synthesize(param, context = {}) return if context[:value] synthesize_internal(param, context[:pallet]) end |
#synthesize_regexp(param, context = {}) ⇒ Object
Synthesize a pallet value from others, using regular expressions to pull out parts of values.
:call-seq:
synthesize_regexp(param, context) -> string or nil
If context contains a non-nil :value, an earlier transform has already produced a value for this key, so do nothing and return nil.
Otherwise, use the parsed YAML structure in param to build and return a new value. If any failure occurs while building the new value, return nil to let another transform try.
YAML structure:
- synthesize_regexp:
sources:
source0:
key: "key"
regexp: "regexp"
source1:
key: "key"
regexp: "regexp"
...
produce: "recipe"
where:
sourceN-
Arbitrary number of sources for partial values, with arbitrary names
key-
Name of the key to read a partial value from
regexp-
Regular expression for parsing the value indicated by
key, with named captures used to save substrings for producing the final value. Capture names must not be repeated within the same synthesize_regexp block. produce-
A recipe for building the new value. Named captures are inserted by
#[capture], and all other characters are copied verbatim.
Example:
Take strings like 192.168.0.0_24 from pallet.ipv4_network and produce strings like 192.168.0.0/24 in net.ipv4.cidr.
- net.ipv4.cidr:
- synthesize_regexp:
sources:
ipv4_network:
key: "pallet.ipv4_network"
regexp: "^(?<network>[0-9.]+)_(?<prefix_length>[0-9]+)$"
produce: "#[network]/#[prefix_length]"
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/palletjack/keytransformer.rb', line 169 def synthesize_regexp(param, context = {}) return if context[:value] captures = {} param['sources'].each do |_, source| # Trying to read values from a non-existent key. Return nil # and let another transform try. return unless lookup = context[:pallet][source['key']] # Save all named captures Regexp.new(source['regexp']).match(lookup) do |md| md.names.each do |name| captures[name] = md[name.to_sym] end end end synthesize_internal(param['produce'], captures) end |