Module: Ambition::API

Includes:
Enumerable
Included in:
Context
Defined in:
lib/ambition/api.rb

Overview

Module that you will extend from in your adapters in your toplevel file.

For example, for ambitious_sphinx in lib/ambition/adapters/ambitious_sphinx.rb, we have:

ActiveRecord::Base.extend Ambition::API

Instance Method Summary collapse

Instance Method Details

#all?(&block) ⇒ Boolean

See Enumerable#all?

Returns:

  • (Boolean)


70
71
72
# File 'lib/ambition/api.rb', line 70

def all?(&block)
  size == select(&block).size
end

#ambition_adapterObject

Gives you the current ambitious adapter.



85
86
87
88
89
# File 'lib/ambition/api.rb', line 85

def ambition_adapter
  own_name = respond_to?(:name) ? name : self.class.name
  parent = respond_to?(:superclass) ? superclass : self.class.superclass
  @@ambition_adapter[own_name] || @@ambition_adapter[parent.name] || infer_ambition_adapter
end

#ambition_adapter=(klass) ⇒ Object

Assign the ambition adapter. Typically, you use this in the toplevel file of your adapter.

For example, for ambitious_sphinx, in our lib/ambition/adapters/ambitious_sphinx.rb:

ActiveRecord::Base.ambition_adapter = Ambition::Adapters::AmbitiousSphinx


106
107
108
109
110
# File 'lib/ambition/api.rb', line 106

def ambition_adapter=(klass)
  @@ambition_adapter ||= {}
  # should this be doing the same check for respond_to?(:name) like above?
  @@ambition_adapter[name] = klass
end

#ambition_contextObject

Builds a new Context.



80
81
82
# File 'lib/ambition/api.rb', line 80

def ambition_context
  Context.new(self)
end

#ambition_ownerObject



112
113
114
# File 'lib/ambition/api.rb', line 112

def ambition_owner
  @owner || self
end

#any?(&block) ⇒ Boolean

See Enumerable#any?

Returns:

  • (Boolean)


65
66
67
# File 'lib/ambition/api.rb', line 65

def any?(&block)
  select(&block).size > 0
end

#chain(other_context) ⇒ Object



9
10
11
12
13
14
# File 'lib/ambition/api.rb', line 9

def chain(other_context)
  other_context.clauses.inject(ambition_context) {|context,(k,v)|
    context.clauses[k].concat(other_context.clauses[k]).uniq!
    context
  } unless other_context.nil?
end

#detect(&block) ⇒ Object

See Enumerable#detect



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

def detect(&block)
  select(&block).first
end

#each(&block) ⇒ Object

See Array#each, applied to entries



60
61
62
# File 'lib/ambition/api.rb', line 60

def each(&block)
  entries.each(&block)
end

#empty?Boolean

See Array#empty?

Returns:

  • (Boolean)


75
76
77
# File 'lib/ambition/api.rb', line 75

def empty?
  size.zero?
end

#entriesObject Also known as: to_a

Entries that our context is able to find.



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

def entries
  ambition_context.kick
end

#first(count = 1) ⇒ Object

See Array#first



54
55
56
57
# File 'lib/ambition/api.rb', line 54

def first(count = 1)
  sliced = slice(0, count)
  count == 1 ? Array(sliced.kick).first : sliced
end

#infer_ambition_adapterObject

If we’re in a class that doesn’t have an adapter assigned, but descends from a class that does, use that adapter (storing it for next time, since ancestors.include? is expensive to run constantly).



94
95
96
97
98
99
# File 'lib/ambition/api.rb', line 94

def infer_ambition_adapter
  famous_ancestor = @@ambition_adapter.keys.detect {|key|
    ancestors.include?(Object.recursive_const_get(key))
  }
  self.ambition_adapter = @@ambition_adapter[famous_ancestor] unless famous_ancestor.nil?
end

#select(&block) ⇒ Object

Entry methods



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

def select(&block)
  context = ambition_context 
  context << Processors::Select.new(context, block)
end

#sizeObject



34
35
36
37
# File 'lib/ambition/api.rb', line 34

def size
  context = ambition_context
  context == self ? super : context.size
end

#slice(start, length = nil) ⇒ Object Also known as: []



39
40
41
42
# File 'lib/ambition/api.rb', line 39

def slice(start, length = nil)
  context = ambition_context 
  context << Processors::Slice.new(context, start, length)
end

#sort_by(&block) ⇒ Object



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

def sort_by(&block)
  context = ambition_context 
  context << Processors::Sort.new(context, block)
end