Module: Ambition::API
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
-
#all?(&block) ⇒ Boolean
See Enumerable#all?.
-
#ambition_adapter ⇒ Object
Gives you the current ambitious adapter.
-
#ambition_adapter=(klass) ⇒ Object
Assign the ambition adapter.
-
#ambition_context ⇒ Object
Builds a new
Context
. - #ambition_owner ⇒ Object
-
#any?(&block) ⇒ Boolean
See Enumerable#any?.
-
#detect(&block) ⇒ Object
See Enumerable#detect.
-
#each(&block) ⇒ Object
See Array#each, applied to
entries
. -
#empty? ⇒ Boolean
See Array#empty?.
-
#entries ⇒ Object
(also: #to_a)
Entries that our context is able to find.
-
#first(count = 1) ⇒ Object
See Array#first.
-
#select(&block) ⇒ Object
Entry methods.
- #size ⇒ Object
- #slice(start, length = nil) ⇒ Object (also: #[])
- #sort_by(&block) ⇒ Object
Instance Method Details
#all?(&block) ⇒ Boolean
See Enumerable#all?
62 63 64 |
# File 'lib/ambition/api.rb', line 62 def all?(&block) size == select(&block).size end |
#ambition_adapter ⇒ Object
Gives you the current ambitious adapter.
77 78 79 80 81 |
# File 'lib/ambition/api.rb', line 77 def ambition_adapter name = respond_to?(:name) ? name : self.class.name parent = respond_to?(:superclass) ? superclass : self.class.superclass @@ambition_adapter[name] || @@ambition_adapter[parent.name] 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
88 89 90 91 92 |
# File 'lib/ambition/api.rb', line 88 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_context ⇒ Object
Builds a new Context
.
72 73 74 |
# File 'lib/ambition/api.rb', line 72 def ambition_context Context.new(self) end |
#ambition_owner ⇒ Object
94 95 96 |
# File 'lib/ambition/api.rb', line 94 def ambition_owner @owner || self end |
#any?(&block) ⇒ Boolean
See Enumerable#any?
57 58 59 |
# File 'lib/ambition/api.rb', line 57 def any?(&block) select(&block).size > 0 end |
#detect(&block) ⇒ Object
See Enumerable#detect
41 42 43 |
# File 'lib/ambition/api.rb', line 41 def detect(&block) select(&block).first end |
#each(&block) ⇒ Object
See Array#each, applied to entries
52 53 54 |
# File 'lib/ambition/api.rb', line 52 def each(&block) entries.each(&block) end |
#empty? ⇒ Boolean
See Array#empty?
67 68 69 |
# File 'lib/ambition/api.rb', line 67 def empty? size.zero? end |
#entries ⇒ Object Also known as: to_a
Entries that our context is able to find.
22 23 24 |
# File 'lib/ambition/api.rb', line 22 def entries ambition_context.kick end |
#first(count = 1) ⇒ Object
See Array#first
46 47 48 49 |
# File 'lib/ambition/api.rb', line 46 def first(count = 1) sliced = slice(0, count) count == 1 ? Array(sliced.kick).first : sliced end |
#select(&block) ⇒ Object
Entry methods
11 12 13 14 |
# File 'lib/ambition/api.rb', line 11 def select(&block) context = ambition_context context << Processors::Select.new(context, block) end |
#size ⇒ Object
27 28 29 |
# File 'lib/ambition/api.rb', line 27 def size ambition_context == self ? super : ambition_context.size end |
#slice(start, length = nil) ⇒ Object Also known as: []
31 32 33 34 |
# File 'lib/ambition/api.rb', line 31 def slice(start, length = nil) context = ambition_context context << Processors::Slice.new(context, start, length) end |
#sort_by(&block) ⇒ Object
16 17 18 19 |
# File 'lib/ambition/api.rb', line 16 def sort_by(&block) context = ambition_context context << Processors::Sort.new(context, block) end |