Module: InlineTransforms

Included in:
Object
Defined in:
lib/inline_transforms.rb,
lib/inline_transforms/version.rb

Overview

Defines only_if, unless, and transform, which we'll then want to make available in Object instances.

Constant Summary collapse

VERSION =
'0.3.0'.freeze

Instance Method Summary collapse

Instance Method Details

#only_if(good_value, **options) ⇒ Object

Returns self only if it matches a "good" value; otherwise returns an alternate (else:) value.

(NOTE: this takes an else: nil named param, but is defined as taking (**options) because else is reserved.)

Parameters:

  • good_value (Object)

    The one value we consider "good", and for which we'd like self returned.

  • else: (Hash)

    a customizable set of options

Returns:

  • Returns either self or the value given as the else: option.



26
27
28
29
30
# File 'lib/inline_transforms.rb', line 26

def only_if(good_value, **options)
  return self if inline_transforms_equality? good_value

  inline_transforms_response_processor options[:else]
end

#transform(**options) ⇒ Object

Returns self ... unless it matches one of the keys in the given "transformation hash", in which case the corresponding hash value is returned. If no matching hash key is found, this returns a fallback (else:) value.

(NOTE: this takes an else: nil named param, but is defined as taking (**options) because else is reserved.)

Parameters:

  • ** (Hash)

    a customizable set of options

  • else: (Hash)

    a customizable set of options

Returns:

  • Returns either self, one of the transformation hash values, or the else: option.



68
69
70
71
72
73
# File 'lib/inline_transforms.rb', line 68

def transform(**options)
  default = options.delete :else

  options.each { |key, value| return inline_transforms_response_processor value if inline_transforms_equality? key }
  inline_transforms_response_processor default
end

#unless(bad_value, **options) ⇒ Object

Returns self ... unless it matches a "bad" value, in which case it returns an alternate (then:) value.

(NOTE: this takes a then: nil named param, but is defined as taking (**options) because then is reserved.)

Parameters:

  • bad_value (Object)

    The one value we consider "bad", and for which we'd like the then: option back instead.

  • then: (Hash)

    a customizable set of options

Returns:

  • Returns either self or the value given as the then: option.



47
48
49
50
51
# File 'lib/inline_transforms.rb', line 47

def unless(bad_value, **options)
  return self unless inline_transforms_equality? bad_value

  inline_transforms_response_processor options[:then]
end