Class: Swicky::ExhibitJson::ItemCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/swicky/exhibit_json/item_collection.rb

Overview

Takes a number of triples and encodes them in the SIMILE JSON format. See simile.mit.edu/wiki/Exhibit/Understanding_Exhibit_Database

Instance Method Summary collapse

Constructor Details

#initialize(triples, original_xpointer = nil) ⇒ ItemCollection

Intialize with the given triples. The original xpointer will (optionally) be passed into the resulting JSON for reference



12
13
14
15
16
# File 'lib/swicky/exhibit_json/item_collection.rb', line 12

def initialize(triples, original_xpointer = nil)
  @triples = triples
  @original_xpointer = original_xpointer
  triple_hash.each { |object, values| add_item(object, values) }
end

Instance Method Details

#add_item(item, values = nil) ⇒ Object

Add a new item to the collection. This passes in a resource-like object and returns the id of the resulting item



40
41
42
43
44
# File 'lib/swicky/exhibit_json/item_collection.rb', line 40

def add_item(item, values = nil)
  items[item.to_s] = Item.new(item, self) if(!items[item.to_s])
  items[item.to_s].add_properties(values) if(values)
  items[item.to_s].id
end

#add_property(prop, value_type) ⇒ Object

As #add_item, but for properties. This must have a value type which will be added to the resulting property description



54
55
56
57
58
59
60
# File 'lib/swicky/exhibit_json/item_collection.rb', line 54

def add_property(prop, value_type)
  properties[prop.to_s] = Item.new(prop, self) if(!properties[prop.to_s])
  if(!properties[prop.to_s]['valueType'])
    properties[prop.to_s]['valueType'] = value_type
  end
  properties[prop.to_s].id
end

#add_type(type) ⇒ Object

As #add_item, but for types



47
48
49
50
# File 'lib/swicky/exhibit_json/item_collection.rb', line 47

def add_type(type)
  types[type.to_s] = Item.new(type, self) if(!types[type.to_s])
  types[type.to_s].id
end

#make_id(item) ⇒ Object

Make a unique id for the given item, based on the label of the item



63
64
65
66
67
68
69
# File 'lib/swicky/exhibit_json/item_collection.rb', line 63

def make_id(item)
  if(id_hash_inv[item.uri]) # Already existing id
    id_hash_inv[item.uri]
  else
    first_free(item.label, item.uri)
  end
end

#to_json(*a) ⇒ Object

Returns the Exhibit-compatible JSON representation



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/swicky/exhibit_json/item_collection.rb', line 19

def to_json(*a)
  my_types = {}
  types.values.each { |t| my_types[t.id] = t }
  my_properties = {}
  properties.values.each { |p| my_properties[p.id] = p }
  result = {
    'items' => items.values,
    'types' => my_types,
    'properties' => my_properties
  }
  if(@original_xpointer)
    result['annotation-for'] = {
      'uri' => @original_xpointer,
      'hash' => ('h_' << Digest::MD5.hexdigest(@original_xpointer))
    }
  end
  result.to_json(*a)
end