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:



10
11
12
13
# File 'lib/alba/conditional_attribute.rb', line 10

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



20
21
22
23
24
25
26
27
28
29
# File 'lib/alba/conditional_attribute.rb', line 20

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, attribute_from_association_body_or(fetched_attribute), &@condition)

  fetched_attribute
end