Class: Rindle::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/rindle/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {}
  { :indices => [], :last_access => nil }.merge!(options)
  @name = name.gsub('@en-US', '')
  @indices = options[:indices] || []
  @last_access = Time.at(options[:last_access]) if options[:last_access]
end

Instance Attribute Details

#indicesObject

Returns the value of attribute indices.



69
70
71
# File 'lib/rindle/collection.rb', line 69

def indices
  @indices
end

#last_accessObject (readonly)

Returns the value of attribute last_access.



69
70
71
# File 'lib/rindle/collection.rb', line 69

def last_access
  @last_access
end

#nameObject (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 options = {}
  filtered = []
  Rindle.collections.each_pair do |name, collection|
    match = true
    options.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, options = {}
  collection = Collection.new(name, options)
  Rindle.collections[collection.name] = collection
end

.exists?(options) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/rindle/collection.rb', line 64

def exists? options
  !Collection.first(options).nil?
end

.find(method = :all, options = {}) ⇒ Object



43
44
45
# File 'lib/rindle/collection.rb', line 43

def find(method = :all, options={})
  self.send method, options
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 options = {}
  Rindle.collections.each_pair do |name, collection|
    match = true
    options.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

#documentsObject

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`.

Returns:

  • (Boolean)


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_hashObject

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

#touchObject

Update the last access timestamp.



160
161
162
# File 'lib/rindle/collection.rb', line 160

def touch
  last_access = Time.now
end