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?, #detect, #each, #empty?, #entries, #first, #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 = {}
  @stash   = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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.



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

def <<(clause)
  @clauses[clause.key] ||= []
  @clauses[clause.key] << clause.to_s
  self
end

#adapter_queryObject



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

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

#ambition_contextObject

Gets the ambition_context. From a Ambition::Context, this is actually self.



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

def ambition_context
  self
end

#inspectObject



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

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