Class: Alba::ConditionalAttribute

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

Overview

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:



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

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

Instance Method Details

#condition_passes?(resource, object) ⇒ Boolean

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.

Parameters:

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/alba/conditional_attribute.rb', line 35

def condition_passes?(resource, object)
  if @condition.is_a?(Proc)
    arity = @condition.arity
    # We can return early to skip fetch_attribute if arity is 1
    # When arity is 2, we check the condition later
    return true if arity >= 2
    return false if arity <= 1 && !resource.instance_exec(object, &@condition)

    true
  else # Symbol
    resource.__send__(@condition)
  end
end

#second_object(object, resource) ⇒ 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.

Parameters:

Returns:

  • (Object)


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/alba/conditional_attribute.rb', line 53

def second_object(object, resource)
  case @body
  when Symbol, Alba::Association, Alba::TypedAttribute
    object.__send__(@body.name)
  when Alba::NestedAttribute
    nil
  when Proc
    resource.instance_exec(object, &@body)
  else raise Alba::Error, "Unreachable code, @body is: #{@body.inspect}"
  end
end

#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



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

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, second_object(object, resource), &@condition)

  fetched_attribute
end

#with_two_arity_proc_condition?Boolean

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:

  • (Boolean)


49
50
51
# File 'lib/alba/conditional_attribute.rb', line 49

def with_two_arity_proc_condition?
  @condition.is_a?(Proc) && @condition.arity >= 2
end