Class: Fauna::Ref

Inherits:
Object
  • Object
show all
Defined in:
lib/fauna/objects.rb

Overview

A Ref.

Reference: FaunaDB Special Types

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Ref

Creates a Ref object.

:call-seq:

Ref.new('databases/prydain')

value: A string.



17
18
19
# File 'lib/fauna/objects.rb', line 17

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject

The raw ref string.



8
9
10
# File 'lib/fauna/objects.rb', line 8

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Returns true if other is a Ref and contains the same value.



55
56
57
58
# File 'lib/fauna/objects.rb', line 55

def ==(other)
  return false unless other.is_a? Ref
  value == other.value
end

#idObject

Removes the class part of the ref, leaving only the id. This is everything after the last /.



38
39
40
41
42
# File 'lib/fauna/objects.rb', line 38

def id
  parts = value.split '/'
  fail ArgumentError.new 'The Ref does not have an id.' if parts.length == 1
  parts.last
end

#to_classObject

Gets the class part out of the Ref. This is done by removing ref.id(). So Fauna::Ref.new('a/b/c').to_class will be Fauna::Ref.new('a/b').



26
27
28
29
30
31
32
33
# File 'lib/fauna/objects.rb', line 26

def to_class
  parts = value.split '/'
  if parts.length == 1
    self
  else
    Fauna::Ref.new(parts[0...-1].join('/'))
  end
end

#to_hashObject

Converts the Ref in Hash form.



50
51
52
# File 'lib/fauna/objects.rb', line 50

def to_hash
  { :@ref => value }
end

#to_sObject

Converts the Ref to a string



45
46
47
# File 'lib/fauna/objects.rb', line 45

def to_s
  value
end