Class: HasHelpers::Icon

Inherits:
Object
  • Object
show all
Includes:
Attributes::AttributeAssignment, UIAttributes::HTMLAttributes
Defined in:
lib/has_helpers/utils/icon.rb

Overview

typed: false frozen_string_literal: true

Instance Attribute Summary collapse

Attributes included from UIAttributes::HTMLAttributes

#data

Instance Method Summary collapse

Methods included from UIAttributes::HTMLAttributes

#raw_html_attrs

Methods included from Attributes::GuardedAttributes

included

Methods included from Attributes::AttributeAssignment

#assign_attributes

Constructor Details

#initialize(icon_name, **options) ⇒ Icon

Handle options passed to new. Hash args are assigned to attributes like ActiveRecord does.

Examples

::HasHelpers::Icon.new("calendar") ::HasHelpers::Icon.new("plane", :orientation => :flip_horizontal) ::HasHelpers::Icon.new("plane", :orientation => "rotate-90")



16
17
18
19
# File 'lib/has_helpers/utils/icon.rb', line 16

def initialize(icon_name, **options)
  self.name = icon_name
  assign_attributes(**options)
end

Instance Attribute Details

#attributesObject

Returns an Array of attributes assigned to the current icon. The result is mutable and may be used to append, remove or modify the current attributes.

Examples

icon = ::HasHelpers::Icon.new("calendar", :julian) icon.attributes # => [ :julian ]

icon.attributes << :reformed
icon.attributes  # => [ :julian, :reformed ]


55
56
57
# File 'lib/has_helpers/utils/icon.rb', line 55

def attributes
  @attributes ||= Array(@attributes)
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/has_helpers/utils/icon.rb', line 8

def name
  @name
end

#orientationObject

Returns the value of attribute orientation.



8
9
10
# File 'lib/has_helpers/utils/icon.rb', line 8

def orientation
  @orientation
end

Instance Method Details

#to_icon(**options) ⇒ Object



21
22
23
24
# File 'lib/has_helpers/utils/icon.rb', line 21

def to_icon(**options)
  assign_attributes(**options)
  self
end

#to_partial_pathObject



63
64
65
# File 'lib/has_helpers/utils/icon.rb', line 63

def to_partial_path
  "has_helpers/icons/icon"
end

#to_renderableObject



59
60
61
# File 'lib/has_helpers/utils/icon.rb', line 59

def to_renderable
  self
end

#to_sObject Also known as: skin

Renders a string representation of the current Icon. This is currently set to render as Font Awesome class names but could be easily altered (perhaps using optional settings) if needed.

Examples

::HasHelpers::Icon.new("calendar").to_s #=> "fa fa-calendar" ::HasHelpers::Icon.new("calendar", :category_green).to_s #=> "fa fa-calendar category-green" ::HasHelpers::Icon.new("plane", :orientation => :flip_horizantal).to_s #=> "fa fa-plane fa-flip-horizontal" ::HasHelpers::Icon.new("plane", :category_green, :orientation => "rotate-90").to_s #=> "fa fa-plane fa-rotate-90 category-green" ::HasHelpers::Icon.new("plane", :attributes => [:category_green, :category_disabled], :orientation => "rotate-90").to_s #=> "fa fa-plane fa-rotate-90 category-green"



36
37
38
39
40
41
42
# File 'lib/has_helpers/utils/icon.rb', line 36

def to_s
  [
    "fa fa-#{ name }",
    *("fa-#{orientation}" if orientation),
    *attributes
  ].join(" ").tr("_", "-")
end