Class: WrapIt::Link

Inherits:
Base
  • Object
show all
Includes:
TextContainer
Defined in:
lib/wrap_it/link.rb

Overview

HTML link element

You can specify link by link, href or url option or by first String argument. Also includes TextContainer module, so you can specify link body with text or body option or by second String argument or inside block.

Examples:

usage

link = WrapIt::Link.new(template, 'http://some.url', 'text')
link.render # => '<a href="http://some.url">test</a>'
link = WrapIt::Link.new(template, link: 'http://some.url', text: 'text')
link.render # => '<a href="http://some.url">test</a>'
link = WrapIt::Link.new(template, 'text', link: http://some.url')
link.render # => '<a href="http://some.url">test</a>'

in template

<%= link 'http://some.url' do %>text<% end %>

Author:

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from WrapIt::Base

Instance Method Details

#bodyString Originally defined in module TextContainer

Retrieves body text

Returns:

  • (String)

    text

#hrefString

Retrieves current link

Returns:

  • (String)

    link



41
42
43
# File 'lib/wrap_it/link.rb', line 41

def href
  html_attr[:href]
end

#href=(value) ⇒ String Also known as: link=, url=

Sets link

Parameters:

  • value (String)

    link

Returns:

  • (String)

    setted link



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wrap_it/link.rb', line 50

def href=(value)
  if value.is_a?(Hash)
    WrapIt.rails? || fail(
      ArgumentError,
      'Hash links supported only in Rails env'
    )
    value = @template.url_for(value)
  end
  value.is_a?(String) || fail(ArgumentError, 'Wrong link type')
  html_attr[:href] = value
end