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

Class Method Details

.blacklist(attributes, name, condition) ⇒ Hash

Checks if the attributes has no blacklisted values

Parameters:

  • attributes (Hash)
  • name (Symbol)
  • condition (Array, Proc, Regexp, Range, Module)

Returns:

  • (Hash)

Raises:

  • (ArgumentError)

    if the condition is satisfied



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

Examples:

coerce({ foo: :BAR, bar: :BAZ }, :foo, -> v { v.to_s })
# => { foo: "BAR", bar: :BAZ }

Parameters:

  • attributes (Hash)
  • name (Symbol)
  • coercer (Proc)

Returns:

  • (Hash)


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

Parameters:

  • attributes (Hash)
  • name (Symbol)
  • presence (Proc)
  • absence (Proc)

Returns:

  • (Hash)


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

Parameters:

  • attributes (Hash)
  • name (Symbol)
  • value (Object)

Returns:

  • (Hash)


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

Examples:

filter({ "foo" => :BAR, bar: :BAZ }, [:foo, :qux])
# => { foo: :BAR }

Parameters:

  • attributes (Hash)
  • keys (Array)

Returns:

  • (Hash)

    attributes



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

Parameters:

  • _attributes (Hash)
  • name (#to_s)

Returns:

  • (undefined)

Raises:

  • (ArgumentError)


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

Examples:

filter({ foo: :BAR }, [:foo, :qux])
# => { foo: :BAR, :qux: nil }

Parameters:

  • attributes (Hash)
  • keys (Array)

Returns:

  • (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

Parameters:

  • attributes (Hash)
  • name (Symbol)
  • condition (Array, Proc, Regexp, Range, Module)

Returns:

  • (Hash)

Raises:

  • (ArgumentError)

    if the condition is NOT satisfied



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