Class: Halibut::Core::Link::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/halibut/core/link.rb

Overview

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é

Defined Under Namespace

Modules: Helpers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Options

Returns a new instance of Options.



79
80
81
82
83
84
85
86
87
88
# 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

Instance Attribute Details

#hreflangObject (readonly)

Returns the value of attribute hreflang.



76
77
78
# File 'lib/halibut/core/link.rb', line 76

def hreflang
  @hreflang
end

#nameObject (readonly)

Returns the value of attribute name.



76
77
78
# File 'lib/halibut/core/link.rb', line 76

def name
  @name
end

#profileObject (readonly)

Returns the value of attribute profile.



76
77
78
# File 'lib/halibut/core/link.rb', line 76

def profile
  @profile
end

#templatedObject (readonly)

Returns the value of attribute templated.



76
77
78
# File 'lib/halibut/core/link.rb', line 76

def templated
  @templated
end

#titleObject (readonly)

Returns the value of attribute title.



76
77
78
# File 'lib/halibut/core/link.rb', line 76

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



76
77
78
# File 'lib/halibut/core/link.rb', line 76

def type
  @type
end

Instance Method Details

#==(other) ⇒ true, false

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

Parameters:

  • other (Options)

    Options object to compare to.

Returns:

  • (true, false)

    whether these two objects are equivalent or not.



126
127
128
# File 'lib/halibut/core/link.rb', line 126

def ==(other)
  to_hash == other.to_hash
end

#templated?true, false

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.

Returns:

  • (true, false)

    whether the href is a templated uri or not.



96
97
98
# File 'lib/halibut/core/link.rb', line 96

def templated?
  @templated || false
end

#to_hashObject

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.



106
107
108
109
110
111
112
113
114
115
# 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