Class: Guillotine::MongoAdapter
- Defined in:
- lib/guillotine/adapters/mongo_adapter.rb
Instance Method Summary collapse
-
#add(url, code = nil) ⇒ Object
Public: Stores the shortened version of a URL.
-
#clear(url) ⇒ Object
Public: Removes the assigned short code for a URL.
-
#code_for(url) ⇒ Object
Public: Retrieves the code for a given URL.
-
#find(code) ⇒ Object
Public: Retrieves a URL from the code.
-
#initialize(collection) ⇒ MongoAdapter
constructor
A new instance of MongoAdapter.
- #select(field, query) ⇒ Object
Methods inherited from Adapter
Constructor Details
#initialize(collection) ⇒ MongoAdapter
Returns a new instance of MongoAdapter.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/guillotine/adapters/mongo_adapter.rb', line 5 def initialize(collection) @collection = collection @collection.ensure_index([['url', Mongo::ASCENDING]]) # \m/ @transformers = { :url => lambda { |doc| doc['url'] }, :code => lambda { |doc| doc['_id'] } } end |
Instance Method Details
#add(url, code = nil) ⇒ Object
Public: Stores the shortened version of a URL.
url - The String URL to shorten and store. code - Optional String code for the URL.
Returns the unique String code for the URL. If the URL is added multiple times, this should return the same code.
23 24 25 |
# File 'lib/guillotine/adapters/mongo_adapter.rb', line 23 def add(url, code = nil) code_for(url) || insert(url, code || shorten(url)) end |
#clear(url) ⇒ Object
Public: Removes the assigned short code for a URL.
url - The String URL to remove.
Returns nothing.
51 52 53 |
# File 'lib/guillotine/adapters/mongo_adapter.rb', line 51 def clear(url) @collection.remove(:url => url) end |
#code_for(url) ⇒ Object
Public: Retrieves the code for a given URL.
url - The String URL to lookup.
Returns the String code, or nil if none is found.
42 43 44 |
# File 'lib/guillotine/adapters/mongo_adapter.rb', line 42 def code_for(url) select :code, :url => url end |
#find(code) ⇒ Object
Public: Retrieves a URL from the code.
code - The String code to lookup the URL.
Returns the String URL, or nil if none is found.
33 34 35 |
# File 'lib/guillotine/adapters/mongo_adapter.rb', line 33 def find(code) select :url, :_id => code end |
#select(field, query) ⇒ Object
55 56 57 |
# File 'lib/guillotine/adapters/mongo_adapter.rb', line 55 def select(field, query) @collection.find_one(query, {:transformer => @transformers[field]}) end |