Class: Ambition::Context

Inherits:
Object show all
Includes:
API
Defined in:
lib/ambition/context.rb

Overview

This class includes several methods you will likely want to be accessing through your Query and Translator classes:

  • clauses

  • owner

  • stash

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API

#all?, #ambition_adapter, #ambition_adapter=, #ambition_owner, #any?, #chain, #detect, #each, #empty?, #entries, #first, #infer_ambition_adapter, #select, #size, #slice, #sort_by

Constructor Details

#initialize(owner) ⇒ Context

Returns a new instance of Context.



31
32
33
34
35
# File 'lib/ambition/context.rb', line 31

def initialize(owner)
  @owner   = owner
  @clauses = Hash.array_backed
  @stash   = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



57
58
59
60
# File 'lib/ambition/context.rb', line 57

def method_missing(method, *args, &block)
  return super unless adapter_query.respond_to? method
  adapter_query.send(method, *args, &block)
end

Instance Attribute Details

#clausesObject (readonly)

A hash of arrays, one key per processor. So, if someone called User.select, your clauses hash would have a :select key with an array of translated strings via your Select class.

This is accessible from your Query and Translator classes.



19
20
21
# File 'lib/ambition/context.rb', line 19

def clauses
  @clauses
end

#ownerObject (readonly)

The class everything was called on. Like ‘User`

This is accessible from your Query and Translator classes.



24
25
26
# File 'lib/ambition/context.rb', line 24

def owner
  @owner
end

#stashObject (readonly)

A place for you to stick stuff. Available to all Translators and your Query class.

This is accessible from your Query and Translator classes.



29
30
31
# File 'lib/ambition/context.rb', line 29

def stash
  @stash
end

Instance Method Details

#<<(clause) ⇒ Object

Adds a clause to this context.



48
49
50
51
# File 'lib/ambition/context.rb', line 48

def <<(clause)
  @clauses[clause.key].push(clause.to_s).uniq!
  self
end

#adapter_queryObject



53
54
55
# File 'lib/ambition/context.rb', line 53

def adapter_query
  Processors::Base.translator(self, :Query)
end

#ambition_contextObject

Return a duplicate of the context, otherwise subsequent operations would alter self.



43
44
45
# File 'lib/ambition/context.rb', line 43

def ambition_context
  dup
end

#initialize_copy(other_context) ⇒ Object



37
38
39
40
# File 'lib/ambition/context.rb', line 37

def initialize_copy(other_context)
  @clauses = Hash.array_backed.dup_update other_context.clauses
  @stash   = {}.dup_update other_context.stash
end

#inspectObject



62
63
64
# File 'lib/ambition/context.rb', line 62

def inspect
  "(Query object: call #to_s or #to_hash to inspect, call an Enumerable (such as #each or #first) to request data)"
end