Class: Rindle::Collection
- Inherits:
-
Object
- Object
- Rindle::Collection
- Defined in:
- lib/rindle/collection.rb
Instance Attribute Summary collapse
-
#indices ⇒ Object
Returns the value of attribute indices.
-
#last_access ⇒ Object
readonly
Returns the value of attribute last_access.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .all(options = {}) ⇒ Object
- .create(name, options = {}) ⇒ Object
- .exists?(options) ⇒ Boolean
- .find(method = :all, options = {}) ⇒ Object
- .find_by_name(name) ⇒ Object
- .first(options = {}) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#add(index) ⇒ Object
Adds an index or a document to the collection.
-
#destroy! ⇒ Object
Destroys the collection.
-
#documents ⇒ Object
Returns an ‘Array` of `Document` objects.
-
#documents=(documents) ⇒ Object
Sets the array of ‘Document` objects.
-
#include?(obj) ⇒ Boolean
Returns true if the collection includes the given index, ‘Document` or `Array`.
-
#initialize(name, options = {}) ⇒ Collection
constructor
A new instance of Collection.
-
#remove(index) ⇒ Object
Removes an entry from this collection.
-
#rename!(new_name) ⇒ Object
Renames the collection.
-
#to_hash ⇒ Object
Returns a hash that may be saved to ‘collections.json` file.
-
#touch ⇒ Object
Update the last access timestamp.
Constructor Details
#initialize(name, options = {}) ⇒ Collection
Returns a new instance of Collection.
78 79 80 81 82 83 |
# File 'lib/rindle/collection.rb', line 78 def initialize name, = {} { :indices => [], :last_access => nil }.merge!() @name = name.gsub('@en-US', '') @indices = [:indices] || [] @last_access = Time.at([:last_access]) if [:last_access] end |
Instance Attribute Details
#indices ⇒ Object
Returns the value of attribute indices.
69 70 71 |
# File 'lib/rindle/collection.rb', line 69 def indices @indices end |
#last_access ⇒ Object (readonly)
Returns the value of attribute last_access.
69 70 71 |
# File 'lib/rindle/collection.rb', line 69 def last_access @last_access end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
69 70 71 |
# File 'lib/rindle/collection.rb', line 69 def name @name end |
Class Method Details
.all(options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rindle/collection.rb', line 4 def all = {} filtered = [] Rindle.collections.each_pair do |name, collection| match = true .each_pair do |key,value| case key when :named match = match && name =~ /#{value}/ when :including match = match && collection.include?(value) when :accessed time = value.is_a?(Integer) ? Time.at(value) : value match = match && collection.last_access == time end end filtered << collection if match end filtered end |
.create(name, options = {}) ⇒ Object
59 60 61 62 |
# File 'lib/rindle/collection.rb', line 59 def create name, = {} collection = Collection.new(name, ) Rindle.collections[collection.name] = collection end |
.exists?(options) ⇒ Boolean
64 65 66 |
# File 'lib/rindle/collection.rb', line 64 def exists? !Collection.first().nil? end |
.find(method = :all, options = {}) ⇒ Object
43 44 45 |
# File 'lib/rindle/collection.rb', line 43 def find(method = :all, ={}) self.send method, end |
.find_by_name(name) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rindle/collection.rb', line 47 def find_by_name name Rindle.collections.values.each do |col| case name when String return col if col.name == name when Regexp return col if col.name =~ name end end nil end |
.first(options = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rindle/collection.rb', line 24 def first = {} Rindle.collections.each_pair do |name, collection| match = true .each_pair do |key,value| case key when :named match = match && collection.name =~ /#{value}/ when :including match = match && collection.include?(value) when :accessed time = value.is_a?(Integer) ? Time.at(value) : value match = match && collection.last_access == time end end return collection if match end nil end |
Instance Method Details
#==(other) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/rindle/collection.rb', line 71 def == other other.is_a?(Rindle::Collection) && name == other.name && indices == other.indices && last_access == other.last_access end |
#add(index) ⇒ Object
Adds an index or a document to the collection.
110 111 112 113 114 115 116 |
# File 'lib/rindle/collection.rb', line 110 def add index index = index.index if index.is_a?(Document) unless @indices.include?(index) @indices << index @documents = nil end end |
#destroy! ⇒ Object
Destroys the collection. This removes the collections key from the collections hash.
105 106 107 |
# File 'lib/rindle/collection.rb', line 105 def destroy! Rindle.collections.delete @name end |
#documents ⇒ Object
Returns an ‘Array` of `Document` objects.
135 136 137 |
# File 'lib/rindle/collection.rb', line 135 def documents @documents ||= @indices.map { |i| Rindle.index[i] } end |
#documents=(documents) ⇒ Object
Sets the array of ‘Document` objects.
140 141 142 143 |
# File 'lib/rindle/collection.rb', line 140 def documents= documents self.indices = documents.map(&:index) @documents = documents end |
#include?(obj) ⇒ Boolean
Returns true if the collection includes the given index, ‘Document` or `Array`.
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rindle/collection.rb', line 147 def include? obj if obj.is_a?(Array) obj.inject(true) { |acc, o| acc = acc and include?(o) } elsif obj.is_a?(Document) indices.include? obj.index elsif obj.is_a?(String) indices.include? obj else false end end |
#remove(index) ⇒ Object
Removes an entry from this collection.
119 120 121 122 123 124 125 |
# File 'lib/rindle/collection.rb', line 119 def remove index index = index.index if index.is_a?(Document) if @indices.include?(index) @indices.delete index @documents = nil end end |
#rename!(new_name) ⇒ Object
Renames the collection. This changes the collections name and updates the Collections hash.
97 98 99 100 101 |
# File 'lib/rindle/collection.rb', line 97 def rename! new_name Rindle.collections.delete @name @name = new_name Rindle.collections[@name] = self end |
#to_hash ⇒ Object
Returns a hash that may be saved to ‘collections.json` file.
86 87 88 89 90 91 92 93 |
# File 'lib/rindle/collection.rb', line 86 def to_hash { "#{@name}@en-US" => { "items" => @indices, "lastAccess" => @last_access.to_i } } end |
#touch ⇒ Object
Update the last access timestamp.
160 161 162 |
# File 'lib/rindle/collection.rb', line 160 def touch last_access = Time.now end |