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.

Examples:

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

Initializes a link options object. Available options:

* templated
* type
* name
* profile
* title
* hreflang

Examples:

Builds an options object with a name and a title

Options.new({
  :name => "Apple",
  :title => "Swift"
})
#=> #<Halibut::Core::Link::Options:0x007f80c880c460
  @hreflang=nil,
  @name="Apple",
  @profile=nil,
  @templated=nil,
  @title="Swift",
  @type=nil>

105
106
107
108
109
110
111
112
113
114
# File 'lib/halibut/core/link.rb', line 105

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


79
80
81
# File 'lib/halibut/core/link.rb', line 79

def hreflang
  @hreflang
end

#nameObject (readonly)

Returns the value of attribute name


79
80
81
# File 'lib/halibut/core/link.rb', line 79

def name
  @name
end

#profileObject (readonly)

Returns the value of attribute profile


79
80
81
# File 'lib/halibut/core/link.rb', line 79

def profile
  @profile
end

#templatedObject (readonly)

Returns the value of attribute templated


79
80
81
# File 'lib/halibut/core/link.rb', line 79

def templated
  @templated
end

#titleObject (readonly)

Returns the value of attribute title


79
80
81
# File 'lib/halibut/core/link.rb', line 79

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type


79
80
81
# File 'lib/halibut/core/link.rb', line 79

def type
  @type
end

Instance Method Details

#==(other) ⇒ true, false

Straight forward comparison between two Options objects.

Examples:

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

153
154
155
# File 'lib/halibut/core/link.rb', line 153

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.


122
123
124
# File 'lib/halibut/core/link.rb', line 122

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.


132
133
134
135
136
137
138
139
140
141
# File 'lib/halibut/core/link.rb', line 132

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