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



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

Examples:

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

Parameters:

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

Returns:

  • (Hash)


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

Parameters:

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

Returns:

  • (Hash)


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

Parameters:

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

Returns:

  • (Hash)


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

Examples:

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

Parameters:

  • attributes (Hash)
  • keys (Array)

Returns:

  • (Hash)

    attributes



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

Parameters:

  • _attributes (Hash)
  • name (#to_s)

Returns:

  • (undefined)

Raises:

  • (ArgumentError)


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

Examples:

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

Parameters:

  • attributes (Hash)
  • keys (Array)

Returns:

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

Parameters:

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

Returns:

  • (Hash)

Raises:

  • (ArgumentError)

    if the condition is NOT satisfied



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