Class: CloudKit::ExtractionView

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudkit/store/extraction_view.rb

Overview

An ExtractionView observes a resource collection and extracts specified elements for querying.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ ExtractionView

Initialize an ExtractionView.

Examples

Observe a “notes” collection and extract the titles and colors.

view = ExtractionView.new(
  :name => :note_features,
  :observe => :notes,
  :extract => [:title, :color])


18
19
20
21
22
# File 'lib/cloudkit/store/extraction_view.rb', line 18

def initialize(name, options)
  @name    = name
  @observe = options[:observe]
  @extract = options[:extract]
end

Instance Attribute Details

#extractObject

Returns the value of attribute extract.



6
7
8
# File 'lib/cloudkit/store/extraction_view.rb', line 6

def extract
  @extract
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/cloudkit/store/extraction_view.rb', line 6

def name
  @name
end

#observeObject

Returns the value of attribute observe.



6
7
8
# File 'lib/cloudkit/store/extraction_view.rb', line 6

def observe
  @observe
end

Instance Method Details

#initialize_storage(db) ⇒ Object

Initialize the storage for this view



46
47
48
49
50
51
52
53
54
55
# File 'lib/cloudkit/store/extraction_view.rb', line 46

def initialize_storage(db)
  extractions = @extract
  db.create_table @name do
    extractions.each do |field|
      text field
    end
    primary_key :id
    varchar     :uri
  end unless db.table_exists?(@name)
end

#map(db, collection, uri, data) ⇒ Object

Map the provided data into a collection for querying.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cloudkit/store/extraction_view.rb', line 25

def map(db, collection, uri, data)
  if @observe == collection
    elements = @extract.inject({}) do |e, field|
      e.merge(field.to_s => data[field.to_s])
    end
    elements.merge!('uri' => uri)
    db.transaction do
      db[@name].filter(:uri => uri).delete
      db[@name].insert(elements)
    end
  end
end

#unmap(db, type, uri) ⇒ Object

Remove the data with the specified URI from the view



39
40
41
42
43
# File 'lib/cloudkit/store/extraction_view.rb', line 39

def unmap(db, type, uri)
  if @observe == type
    db[@name].filter(:uri => uri).delete
  end
end