Class: Hollybush::List
- Inherits:
-
Object
- Object
- Hollybush::List
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations, Enumerable
- Defined in:
- lib/hollybush/list.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
Returns the value of attribute entries.
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
- .count(query = {}) ⇒ Object
- .create(attributes) ⇒ Object
- .delete(query = {}) ⇒ Object
- .delete_entry(query = {}) ⇒ Object
- .find(query = {}, options = {}) ⇒ Object
Instance Method Summary collapse
- #<<(entry) ⇒ Object
- #delete ⇒ Object
- #delete_entry(entry) ⇒ Object
- #each(&a_block) ⇒ Object
-
#initialize(attributes = {}) ⇒ List
constructor
A new instance of List.
- #persisted? ⇒ Boolean
- #save ⇒ Object
- #update_attributes(attributes = {}) ⇒ Object
- #update_entry(entry) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ List
Returns a new instance of List.
12 13 14 15 16 |
# File 'lib/hollybush/list.rb', line 12 def initialize(attributes = {}) update_attributes(attributes) @entries = [] unless @entries @id = attributes["_id"].to_s if attributes.include?("_id") end |
Instance Attribute Details
#entries ⇒ Object
Returns the value of attribute entries.
9 10 11 |
# File 'lib/hollybush/list.rb', line 9 def entries @entries end |
#id ⇒ Object
Returns the value of attribute id.
9 10 11 |
# File 'lib/hollybush/list.rb', line 9 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/hollybush/list.rb', line 9 def name @name end |
Class Method Details
.count(query = {}) ⇒ Object
95 96 97 |
# File 'lib/hollybush/list.rb', line 95 def self.count(query = {}) coll.find(query, :fields => []).count end |
.create(attributes) ⇒ Object
31 32 33 34 35 |
# File 'lib/hollybush/list.rb', line 31 def self.create(attributes) list = List.new(attributes) list.save list end |
.delete(query = {}) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/hollybush/list.rb', line 71 def self.delete(query = {}) begin List.coll.remove(prepare(query)) rescue BSON::InvalidObjectId end end |
.delete_entry(query = {}) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/hollybush/list.rb', line 78 def self.delete_entry(query = {}) begin List.coll.remove(prepare(query)) rescue BSON::InvalidObjectId end end |
Instance Method Details
#<<(entry) ⇒ Object
42 43 44 45 46 |
# File 'lib/hollybush/list.rb', line 42 def <<(entry) save unless persisted? BSON::ObjectId.create_pk(entry) @entries << entry if List.coll.update({"_id" => make_id(@id)}, {"$push" => {:entries => entry}}) end |
#delete ⇒ Object
67 68 69 |
# File 'lib/hollybush/list.rb', line 67 def delete List.coll.remove({"_id" => make_id(@id)}) end |
#delete_entry(entry) ⇒ Object
48 49 50 |
# File 'lib/hollybush/list.rb', line 48 def delete_entry(entry) @entries.delete(entry) if List.coll.update({"_id" => make_id(@id)}, {"$pull" => {:entries => {"_id" => entry["_id"]}}}) end |
#each(&a_block) ⇒ Object
37 38 39 40 |
# File 'lib/hollybush/list.rb', line 37 def each(&a_block) doc = List.coll.find_one({"_id" => @id}) doc["entries"].each(&a_block) if doc["entries"] end |
#persisted? ⇒ Boolean
57 58 59 |
# File 'lib/hollybush/list.rb', line 57 def persisted? @id != nil end |
#save ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/hollybush/list.rb', line 18 def save if valid? update_doc = {:name => @name, :entries => @entries} if @id List.coll.update({"_id" => make_id(@id)}, update_doc) else @id = List.coll.save(update_doc).to_s end else false end end |
#update_attributes(attributes = {}) ⇒ Object
61 62 63 64 65 |
# File 'lib/hollybush/list.rb', line 61 def update_attributes(attributes = {}) attributes.each do |name, value| send("#{name}=", value) if respond_to?(name) end end |
#update_entry(entry) ⇒ Object
52 53 54 55 |
# File 'lib/hollybush/list.rb', line 52 def update_entry(entry) entry["_id"] = make_id(entry["_id"]) List.coll.update({"_id" => make_id(@id), "entries._id" => entry["_id"]}, {"$set" => {"entries.$"=> entry}}) end |