Class: Halibut::Core::Link::Options
- Inherits:
-
Object
- Object
- Halibut::Core::Link::Options
- 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
-
#hreflang ⇒ Object
readonly
Returns the value of attribute hreflang.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#profile ⇒ Object
readonly
Returns the value of attribute profile.
-
#templated ⇒ Object
readonly
Returns the value of attribute templated.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Straight forward comparison between two Options objects.
-
#initialize(opts) ⇒ Options
constructor
A new instance of Options.
-
#templated? ⇒ true, false
Tells us if the href of the associated link is templated.
-
#to_hash ⇒ Object
When converting to a hash, options that weren’t set (.nil? == true) are kept out.
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 = 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
#hreflang ⇒ Object (readonly)
Returns the value of attribute hreflang.
76 77 78 |
# File 'lib/halibut/core/link.rb', line 76 def hreflang @hreflang end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
76 77 78 |
# File 'lib/halibut/core/link.rb', line 76 def name @name end |
#profile ⇒ Object (readonly)
Returns the value of attribute profile.
76 77 78 |
# File 'lib/halibut/core/link.rb', line 76 def profile @profile end |
#templated ⇒ Object (readonly)
Returns the value of attribute templated.
76 77 78 |
# File 'lib/halibut/core/link.rb', line 76 def templated @templated end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
76 77 78 |
# File 'lib/halibut/core/link.rb', line 76 def title @title end |
#type ⇒ Object (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
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.
96 97 98 |
# File 'lib/halibut/core/link.rb', line 96 def templated? @templated || false end |
#to_hash ⇒ Object
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 |