Class: Halogen::Links::Definition

Inherits:
Definition show all
Defined in:
lib/halogen/links/definition.rb

Overview

:nodoc

Instance Attribute Summary

Attributes inherited from Definition

#name, #options, #procedure

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Definition

#enabled?

Constructor Details

#initialize(name, *args, procedure) ⇒ Definition

Links have special keywords that other definitions don't, so override the standard initializer to build options from keywords


9
10
11
# File 'lib/halogen/links/definition.rb', line 9

def initialize(name, *args, procedure)
  super name, self.class.build_options(args), procedure
end

Class Method Details

.build_attrs(keywords) ⇒ Object

Raises:


63
64
65
66
67
68
69
70
71
72
# File 'lib/halogen/links/definition.rb', line 63

def build_attrs(keywords)
  keywords.each_with_object({}) do |keyword, attrs|
    case keyword
    when :templated, 'templated'
      attrs[:templated] = true
    else
      fail InvalidDefinition, "Unrecognized link keyword: #{keyword}"
    end
  end
end

.build_options(args) ⇒ Hash

Build hash of options from flexible definition arguments


50
51
52
53
54
55
56
57
# File 'lib/halogen/links/definition.rb', line 50

def build_options(args)
  {}.tap do |options|
    options.merge!(args.pop) if args.last.is_a?(Hash)

    options[:attrs] ||= {}
    options[:attrs].merge!(build_attrs(args))
  end
end

Instance Method Details

#validatetrue

Returns if nothing is raised.

Raises:


17
18
19
20
21
22
23
24
# File 'lib/halogen/links/definition.rb', line 17

def validate
  super

  return true if procedure || options.key?(:value)

  fail InvalidDefinition,
       'Link requires either procedure or explicit value'
end

#value(_instance) ⇒ nil, Hash


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/halogen/links/definition.rb', line 28

def value(_instance)
  hrefs = super

  attrs = options.fetch(:attrs, {})

  case hrefs
  when Array
    hrefs.map { |href| attrs.merge(href: href) }
  when nil    # no-op

  else
    attrs.merge(href: hrefs)
  end
end