class Halibut::Core::Link::Options

Options reifies the various optional properties of a link, as per the spec: templated, type, name, profile, title, hreflang.

hash = { name: 'John le Carré', templated: true }
opts = Options.new(hash)
opts.name    # => John le Carré
opts['name'] # => John le Carré
opts[:name]  # => John le Carré

Attributes

hreflang[R]
name[R]
profile[R]
templated[R]
title[R]
type[R]

Public Class Methods

new(opts) click to toggle source
# File lib/halibut/core/link.rb, line 79
def initialize opts
  string_options = Helpers::stringify_keys(opts)

  @templated = opts[:templated] || opts['templated']
  @type      = opts[:type]      || opts['type']
  @name      = opts[:name]      || opts['name']
  @profile   = opts[:profile]   || opts['profile']
  @title     = opts[:title]     || opts['title']
  @hreflang  = opts[:hreflang]  || opts['hreflang']
end

Public Instance Methods

==(other) click to toggle source

Straight forward comparison between two Options objects.

opts_one = Options.new(name: 'Link', templated: true)
opts_two = Options.new(name: 'Link', templated: true)
opts_one == opts_two
# => true

@param [Options] other Options object to compare to. @return [true,false] whether these two objects are equivalent or not.

# File lib/halibut/core/link.rb, line 126
def ==(other)
  to_hash == other.to_hash
end
templated?() click to toggle source

Tells us if the href of the associated link is templated.

The reason for not returning @templated directly is that all of the options are optional, thus nil could be returned instead of a boolean.

@return [true, false] whether the href is a templated uri or not.

# File lib/halibut/core/link.rb, line 96
def templated?
  @templated || false
end
to_hash() click to toggle source

When converting to a hash, options that weren't set (.nil? == true) are kept out.

This might have some implications, as it does not 'serialiaze' options that were explicitely set to nil. On the other hand, one can argue that if they were explicitly set to nil, then they shouldn't show up anyway.

# File lib/halibut/core/link.rb, line 106
def to_hash
  instance_variables.each_with_object({}) do |ivar, hash|
    name  = ivar.to_s.reverse.chomp("@").reverse
    value = instance_variable_get(ivar)

    next if value.nil?

    hash[name] = value
  end
end