Class: ROM::Relation::Curried

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Includes:
Pipeline, Materializable
Defined in:
lib/rom/relation/curried.rb

Overview

Curried relation is a special relation proxy used by auto-curry mechanism.

When a relation view method is called without all arguments, a curried proxy is returned that can be fully applied later on.

Curried relations are typically used for relation composition

Constant Summary collapse

WRAPS =
[Relation, Graph, Wrap, Composite].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Initializer

extended

Methods included from Pipeline

#map_with

Methods included from Pipeline::Operator

#>>

Methods included from Materializable

#each, #first, #one, #one!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth) ⇒ Object (private)

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.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rom/relation/curried.rb', line 110

def method_missing(meth, ...)
  if relation.respond_to?(meth)
    response = relation.__send__(meth, ...)

    super if response.is_a?(self.class)

    if WRAPS.any? { |klass| response.is_a?(klass) }
      __new__(response)
    else
      response
    end
  else
    super
  end
end

Instance Attribute Details

#arityInteger (readonly)

Returns View’s arity.

Returns:

  • (Integer)

    View’s arity



43
# File 'lib/rom/relation/curried.rb', line 43

option :arity, type: Types::Strict::Integer

#curry_argsArray (readonly)

Returns Arguments that will be passed to curried view.

Returns:

  • (Array)

    Arguments that will be passed to curried view



47
# File 'lib/rom/relation/curried.rb', line 47

option :curry_args, default: -> { EMPTY_ARRAY }

#relationRelation (readonly)

Returns The source relation that is curried.

Returns:

  • (Relation)

    The source relation that is curried



35
# File 'lib/rom/relation/curried.rb', line 35

param :relation

#viewSymbol (readonly)

Returns The name of relation’s view method.

Returns:

  • (Symbol)

    The name of relation’s view method



39
# File 'lib/rom/relation/curried.rb', line 39

option :view, type: Types::Strict::Symbol

Instance Method Details

#call(*args) ⇒ Loaded, Curried Also known as: []

Load relation if args match the arity

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rom/relation/curried.rb', line 54

def call(*args)
  all_args = curry_args + args

  if all_args.empty?
    raise ArgumentError,
          "curried #{relation.class}##{view} relation was called without any arguments"
  end

  if args.empty?
    self
  elsif arity == all_args.size
    Loaded.new(relation.__send__(view, *all_args))
  else
    __new__(relation, curry_args: all_args)
  end
end

#curried?true

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.

Return if this lazy relation is curried

Returns:

  • (true)


92
# File 'lib/rom/relation/curried.rb', line 92

def curried? = true

#respond_to_missing?(name, include_private = false) ⇒ 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)


95
96
97
# File 'lib/rom/relation/curried.rb', line 95

def respond_to_missing?(name, include_private = false)
  super || relation.respond_to?(name, include_private)
end

#to_aObject Also known as: to_ary

Relations are coercible to an array but a curried relation cannot be coerced When something tries to do this, an exception will be raised

Raises:

  • ArgumentError



78
79
80
81
82
83
84
# File 'lib/rom/relation/curried.rb', line 78

def to_a
  raise(
    ArgumentError,
    "#{relation.class}##{view} arity is #{arity} " \
    "(#{curry_args.size} args given)"
  )
end