Class: BeanBag
- Inherits:
-
Object
- Object
- BeanBag
- Defined in:
- lib/beanbag.rb
Class Method Summary collapse
- .all ⇒ Object
- .database ⇒ Object
- .design_doc ⇒ Object
- .found_by(*identifiers) ⇒ Object
- .in_database(db_name) ⇒ Object
- .view(view_name) ⇒ Object
Instance Method Summary collapse
-
#initialize(args) ⇒ BeanBag
constructor
A new instance of BeanBag.
Constructor Details
#initialize(args) ⇒ BeanBag
8 9 10 |
# File 'lib/beanbag.rb', line 8 def initialize(args) @data = args end |
Class Method Details
.all ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/beanbag.rb', line 42 def self.all begin view sprintf('%s/all_%ss', self.name.downcase, self.name.downcase) rescue RestClient::ResourceNotFound doc = design_doc doc['views']["all_#{self.name.downcase}s"] = {} view_string = "function(doc){" if not @identifiers.empty? view_string << "if(" conditions = [] @identifiers.each_pair do |name, value| conditions << "doc.#{name.to_s}" conditions << "doc.#{name.to_s} === '#{value.to_s}'" if not value.nil? end view_string << conditions.join(" && ") view_string << ")" end view_string << "emit(doc['_id'], doc);}" doc['views']["all_#{self.name.downcase}s"]['map'] = view_string database.save_doc(doc) retry end end |
.database ⇒ Object
68 69 70 71 72 73 |
# File 'lib/beanbag.rb', line 68 def self.database if not defined? @database @database = CouchRest.new.database(@db_name) end @database end |
.design_doc ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/beanbag.rb', line 29 def self.design_doc design_doc_name = "_design/#{self.name.downcase}" begin doc = database.get(design_doc_name) rescue RestClient::ResourceNotFound doc = CouchRest::Document.new("views" => {}) doc.id = design_doc_name database.save_doc(doc) retry end doc end |
.found_by(*identifiers) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/beanbag.rb', line 16 def self.found_by(*identifiers) @identifiers = {} identifiers.each do |id| if id.is_a? Hash @identifiers = @identifiers.merge id elsif id.is_a? String or id.is_a? Symbol @identifiers[id] = nil end end end |
.in_database(db_name) ⇒ Object
12 13 14 |
# File 'lib/beanbag.rb', line 12 def self.in_database(db_name) @db_name = db_name end |
.view(view_name) ⇒ Object
75 76 77 |
# File 'lib/beanbag.rb', line 75 def self.view(view_name) database.view view_name end |