module AttributesDSL::Transprocs
The collection of pure composable functions
Public Class Methods
Checks if the attributes has no blacklisted values
@param [Hash] attributes @param [Symbol] name @param [Array, Proc, Regexp, Range, Module] condition
@return [Hash] @raise [ArgumentError] if the condition is satisfied
# 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
Coerces attributes’ name’s value using given proc
@example
coerce({ foo: :BAR, bar: :BAZ }, :foo, -> v { v.to_s }) # => { foo: "BAR", bar: :BAZ }
@param [Hash] attributes @param [Symbol] name @param [Proc] coercer
@return [Hash]
# File lib/attributes_dsl/transprocs.rb, line 129 def self.coerce(attributes, name, coercer) attributes.merge(name => coercer[attributes[name]]) end
Checks whether the name is present in attributes’ keys, and transforms it using either the first or the second procedure
@param [Hash] attributes @param [Symbol] name @param [Proc] presence @param [Proc] absence
@return [Hash]
# 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
Updates the hash with default name’s value
@param [Hash] attributes @param [Symbol] name @param [Object] value
@return [Hash]
# File lib/attributes_dsl/transprocs.rb, line 83 def self.default(attributes, name, value) attributes.merge(name => value) end
Symbolizes hash keys and filters them by given ones
@example
filter({ "foo" => :BAR, bar: :BAZ }, [:foo, :qux]) # => { foo: :BAR }
@param [Hash] attributes @param [Array] keys
@return [Hash] attributes
# 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
Complains about missed attribute in a hash
@param [Hash] _attributes @param [#to_s] name
@return [undefined] @raise [ArgumentError]
# File lib/attributes_dsl/transprocs.rb, line 71 def self.missed(_attributes, name) fail ArgumentError.new "Attribute '#{name}' is required" end
Ensures all given keys are present in the hash
@example
filter({ foo: :BAR }, [:foo, :qux]) # => { foo: :BAR, :qux: nil }
@param [Hash] attributes @param [Array] keys
@return [Hash]
# File lib/attributes_dsl/transprocs.rb, line 39 def self.update(attributes, keys) keys .inject({}) { |hash, key| hash.merge(key => nil) } .merge(attributes) end
Checks if the attributes has whitelisted values
@param [Hash] attributes @param [Symbol] name @param [Array, Proc, Regexp, Range, Module] condition
@return [Hash] @raise [ArgumentError] if the condition is NOT satisfied
# 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
Private Class Methods
# File lib/attributes_dsl/transprocs.rb, line 135 def self.check(value, condition) method = condition.is_a?(Array) ? :include? : :=== condition.send method, value rescue false end