Class: Neuromancer::Indexer::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/neuromancer/indexer/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Document

Returns a new instance of Document.



8
9
10
11
12
# File 'lib/neuromancer/indexer/document.rb', line 8

def initialize(attributes)
  @id = indifferent_value(attributes, :id).to_s
  @type = indifferent_value(attributes, :type).to_s
  @attributes = indifferent_value(attributes, :attributes)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/neuromancer/indexer/document.rb', line 6

def attributes
  @attributes
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/neuromancer/indexer/document.rb', line 6

def id
  @id
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/neuromancer/indexer/document.rb', line 6

def type
  @type
end

Instance Method Details

#as_jsonObject



14
15
16
17
18
19
20
# File 'lib/neuromancer/indexer/document.rb', line 14

def as_json
  {
    id: id,
    type: type,
    attributes: attributes
  }
end

#indifferent_value(hash, key) ⇒ Object



32
33
34
# File 'lib/neuromancer/indexer/document.rb', line 32

def indifferent_value(hash, key)
  hash[key.to_sym] || hash[key.to_s]
end

#validate!Object

Raises:



22
23
24
25
26
27
28
29
30
# File 'lib/neuromancer/indexer/document.rb', line 22

def validate!
  errors = []

  errors << 'document#id is empty' if id.empty?
  errors << 'document#type is empty' if type.empty?
  errors << 'document#attributes is not a Hash' unless attributes.is_a?(Hash)

  raise(InvalidDocument, errors) unless errors.empty?
end