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
96 97 98 99 100 |
# File 'lib/attributes_dsl/transprocs.rb', line 96 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
129 130 131 |
# File 'lib/attributes_dsl/transprocs.rb', line 129 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
55 56 57 58 59 60 61 |
# File 'lib/attributes_dsl/transprocs.rb', line 55 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
83 84 85 |
# File 'lib/attributes_dsl/transprocs.rb', line 83 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 |
# File 'lib/attributes_dsl/transprocs.rb', line 22 def self.filter(attributes, keys) attributes .inject({}) { |hash, (key, value)| hash.merge(key.to_sym => value) } .select { |key, _| keys.include? key } end |
.missed(_attributes, name) ⇒ undefined
Complains about missed attribute in a hash
71 72 73 |
# File 'lib/attributes_dsl/transprocs.rb', line 71 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
39 40 41 42 43 |
# File 'lib/attributes_dsl/transprocs.rb', line 39 def self.update(attributes, keys) keys .inject({}) { |hash, key| hash.merge(key => nil) } .merge(attributes) end |
.whitelist(attributes, name, condition) ⇒ Hash
Checks if the attributes has whitelisted values
111 112 113 114 115 |
# File 'lib/attributes_dsl/transprocs.rb', line 111 def self.whitelist(attributes, name, condition) return attributes if check(attributes[name], condition) fail ArgumentError.new "Attribute #{name} is invalid: #{attributes[name]}" end |