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



7
8
9
# File 'lib/halogen/links/definition.rb', line 7

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

Class Method Details

.build_attrs(keywords) ⇒ Object

Parameters:

  • keywords (Array)

    array of special keywords

Raises:



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

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

Parameters:

  • args (Array)

    the raw definition arguments

Returns:

  • (Hash)

    standardized hash of options



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

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.

Returns:

  • (true)

    if nothing is raised

Raises:



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

def validate
  super

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

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

#value(_instance) ⇒ nil, Hash

Returns:

  • (nil, Hash)


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

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