module Fauna
##
# A Ref.
#
# Reference: {FaunaDB Special Types}[https://faunadb.com/documentation/queries-values-special_types]
class Ref
# The raw ref string.
attr_accessor :value
##
# Creates a Ref object.
#
# :call-seq:
# Ref.new('databases/prydain')
#
# +value+: A string.
def initialize(value)
@value = value
end
##
# 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')
.
def to_class
parts = value.split '/'
if parts.length == 1
self
else
Fauna::Ref.new(parts[0...-1].join('/'))
end
end
##
# Removes the class part of the ref, leaving only the id.
# This is everything after the last /.
def id
parts = value.split '/'
fail ArgumentError.new 'The Ref does not have an id.' if parts.length == 1
parts.last
end
# Converts the Ref to a string
def to_s
value
end
# Converts the Ref in Hash form.
def to_hash
{ :@ref => value }
end
# Returns +true+ if +other+ is a Ref and contains the same value.
def ==(other)
return false unless other.is_a? Ref
value == other.value
end
alias_method :eql?, :==
end
##
# A SetRef.
#
# Reference: {FaunaDB Special Types}[https://faunadb.com/documentation/queries-values-special_types]
class SetRef
# The raw set hash.
attr_accessor :value
##
# Creates a new SetRef with the given parameters.
#
# +params+:: Hash of parameters to build the SetRef with.
#
# Reference: {FaunaDB Special Types}[https://faunadb.com/documentation/queries-values-special_types]
def initialize(params = {})
self.value = params
end
# Converts the SetRef to Hash form.
def to_hash
{ :@set => value }
end
# Returns +true+ if +other+ is a SetRef and contains the same value.
def ==(other)
return false unless other.is_a? SetRef
value == other.value
end
alias_method :eql?, :==
end
end