Class: Mida::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/mida/item.rb

Overview

Class that holds a validated item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(itemscope) ⇒ Item

Create a new Item object from an Itemscope and validates its properties

itemscope

The itemscope that has been parsed by Itemscope



27
28
29
30
31
32
33
# File 'lib/mida/item.rb', line 27

def initialize(itemscope)
  @type = itemscope.type
  @id = itemscope.id
  @vocabulary = Mida::Vocabulary.find(@type)
  @properties = itemscope.properties
  validate_properties
end

Instance Attribute Details

#idObject (readonly)

The Global Identifier of the item



16
17
18
# File 'lib/mida/item.rb', line 16

def id
  @id
end

#propertiesObject (readonly)

A Hash representing the properties as name/values paris The values will be an array containing either String or Mida::Item instances



21
22
23
# File 'lib/mida/item.rb', line 21

def properties
  @properties
end

#typeObject (readonly)

The Type of the item



13
14
15
# File 'lib/mida/item.rb', line 13

def type
  @type
end

#vocabularyObject (readonly)

The vocabulary used to interpret this item



10
11
12
# File 'lib/mida/item.rb', line 10

def vocabulary
  @vocabulary
end

Instance Method Details

#==(other) ⇒ Object



54
55
56
57
# File 'lib/mida/item.rb', line 54

def ==(other)
  @vocabulary == other.vocabulary && @type == other.type &&
  @id == other.id && @properties == other.properties
end

#to_hObject

Return a Hash representation of the form:

{ type: 'http://example.com/vocab/review',
  id: 'urn:isbn:1-934356-08-5',
  properties: {'a name' => 'avalue' }
}


41
42
43
44
45
46
47
48
# File 'lib/mida/item.rb', line 41

def to_h
  # Only fill hash with non-nil values
  hash = {}
  @type and hash[:type] = @type
  @id and hash[:id] = @id
  @properties.any? and hash[:properties] = properties_to_h(@properties)
  hash
end

#to_sObject



50
51
52
# File 'lib/mida/item.rb', line 50

def to_s
  to_h.to_s
end