Module: AttributesDSL::Transprocs
- Extended by:
- Transproc::Registry
- Defined in:
- lib/attributes_dsl/transprocs.rb
Overview
The collection of pure composable functions
Class Method Summary collapse
-
.blacklist(attributes, name, condition) ⇒ Hash
Checks if the attributes has no blacklisted values.
-
.coerce(attributes, name, coercer) ⇒ Hash
Coerces attributes’ name’s value using given proc.
-
.convert(attributes, name, presence, absence) ⇒ Hash
Checks whether the name is present in attributes’ keys, and transforms it using either the first or the second procedure.
-
.default(attributes, name, value) ⇒ Hash
Updates the hash with default name’s value.
-
.filter(attributes, keys) ⇒ Hash
Symbolizes hash keys and filters them by given ones.
-
.missed(_attributes, name) ⇒ undefined
Complains about missed attribute in a hash.
-
.update(attributes, keys) ⇒ Hash
Ensures all given keys are present in the hash.
-
.whitelist(attributes, name, condition) ⇒ Hash
Checks if the attributes has whitelisted values.
Class Method Details
.blacklist(attributes, name, condition) ⇒ Hash
Checks if the attributes has no blacklisted values
95 96 97 98 99 |
# File 'lib/attributes_dsl/transprocs.rb', line 95 def self.blacklist(attributes, name, condition) return attributes unless check(attributes[name], condition) fail ArgumentError.new "Attribute #{name} is invalid: #{attributes[name]}" end |
.coerce(attributes, name, coercer) ⇒ Hash
Coerces attributes’ name’s value using given proc
128 129 130 |
# File 'lib/attributes_dsl/transprocs.rb', line 128 def self.coerce(attributes, name, coercer) attributes.merge(name => coercer[attributes[name]]) end |
.convert(attributes, name, presence, absence) ⇒ Hash
Checks whether the name is present in attributes’ keys, and transforms it using either the first or the second procedure
54 55 56 57 58 59 60 |
# File 'lib/attributes_dsl/transprocs.rb', line 54 def self.convert(attributes, name, presence, absence) if attributes.keys.include? name presence[attributes] else absence[attributes] end end |
.default(attributes, name, value) ⇒ Hash
Updates the hash with default name’s value
82 83 84 |
# File 'lib/attributes_dsl/transprocs.rb', line 82 def self.default(attributes, name, value) attributes.merge(name => value) end |
.filter(attributes, keys) ⇒ Hash
Symbolizes hash keys and filters them by given ones
22 23 24 25 26 27 |
# File 'lib/attributes_dsl/transprocs.rb', line 22 def self.filter(attributes, keys) attributes .map { |key, value| [key.to_sym, value] } .select { |key, _| keys.include? key } .to_h end |
.missed(_attributes, name) ⇒ undefined
Complains about missed attribute in a hash
70 71 72 |
# File 'lib/attributes_dsl/transprocs.rb', line 70 def self.missed(_attributes, name) fail ArgumentError.new "Attribute '#{name}' is required" end |
.update(attributes, keys) ⇒ Hash
Ensures all given keys are present in the hash
40 41 42 |
# File 'lib/attributes_dsl/transprocs.rb', line 40 def self.update(attributes, keys) keys.map { |key| [key, attributes[key]] }.to_h end |
.whitelist(attributes, name, condition) ⇒ Hash
Checks if the attributes has whitelisted values
110 111 112 113 114 |
# File 'lib/attributes_dsl/transprocs.rb', line 110 def self.whitelist(attributes, name, condition) return attributes if check(attributes[name], condition) fail ArgumentError.new "Attribute #{name} is invalid: #{attributes[name]}" end |