Class: Alba::ConditionalAttribute Private

Inherits:
Object
  • Object
show all
Defined in:
lib/alba/conditional_attribute.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents attribute with ‘if` option

Instance Method Summary collapse

Constructor Details

#initialize(body:, condition:) ⇒ ConditionalAttribute

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ConditionalAttribute.

Parameters:



13
14
15
16
# File 'lib/alba/conditional_attribute.rb', line 13

def initialize(body:, condition:)
  @body = body
  @condition = condition
end

Instance Method Details

#with_passing_condition(resource:, object: nil) ⇒ Alba::REMOVE_KEY, Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns attribute body if condition passes

Parameters:

  • resource (Alba::Resource)
  • object (Object) (defaults to: nil)

    needed for collection, each object from collection

Returns:

  • (Alba::REMOVE_KEY, Object)

    REMOVE_KEY if condition is unmet, fetched attribute otherwise



23
24
25
26
27
28
29
30
31
32
# File 'lib/alba/conditional_attribute.rb', line 23

def with_passing_condition(resource:, object: nil)
  return Alba::REMOVE_KEY unless condition_passes?(resource, object)

  fetched_attribute = yield(@body)
  return fetched_attribute unless with_two_arity_proc_condition

  return Alba::REMOVE_KEY unless resource.instance_exec(object, objectize(fetched_attribute), &@condition)

  fetched_attribute
end