Class: Halogen::Links::Definition
- Inherits:
-
Definition
- Object
- Definition
- Halogen::Links::Definition
- Defined in:
- lib/halogen/links/definition.rb
Overview
:nodoc
Instance Attribute Summary
Attributes inherited from Definition
Class Method Summary collapse
- .build_attrs(keywords) ⇒ Object
-
.build_options(args) ⇒ Hash
Build hash of options from flexible definition arguments.
Instance Method Summary collapse
-
#initialize(name, *args, procedure) ⇒ Definition
constructor
Links have special keywords that other definitions don’t, so override the standard initializer to build options from keywords.
-
#validate ⇒ true
If nothing is raised.
- #value(_instance) ⇒ nil, Hash
Methods inherited from Definition
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.(args), procedure end |
Class Method Details
.build_attrs(keywords) ⇒ Object
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
48 49 50 51 52 53 54 55 |
# File 'lib/halogen/links/definition.rb', line 48 def (args) {}.tap do || .merge!(args.pop) if args.last.is_a?(Hash) [:attrs] ||= {} [:attrs].merge!(build_attrs(args)) end end |
Instance Method Details
#validate ⇒ true
Returns if nothing is raised.
15 16 17 18 19 20 21 22 |
# File 'lib/halogen/links/definition.rb', line 15 def validate super return true if procedure || .key?(:value) fail InvalidDefinition, 'Link requires either procedure or explicit value' end |
#value(_instance) ⇒ 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 = .fetch(:attrs, {}) case hrefs when Array hrefs.map { |href| attrs.merge(href: href) } when nil # no-op else attrs.merge(href: hrefs) end end |