Class: Inkcite::Renderer::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/inkcite/renderer/element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, att = {}) ⇒ Element



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/inkcite/renderer/element.rb', line 7

def initialize tag, att={}

  # The tag, attribute and in-line CSS styles.
  @tag = tag
  @att = att

  # Initializing @classes to avoid a Ruby warning that it hasn't been
  # declared when it is lazy-initialized in the classes() method.
  @classes = nil

  # True if the tag self-closes as in "<img .../>"
  @self_close = att.delete(:self_close) == true

  # For caller convenience, accept a style hash from the attributes
  # or initialize it here.
  @style = att.delete(:style) || {}

  # Collection of mobile-only CSS properties for this element.
  @mobile_style = att.delete(:mobile_style) || {}

end

Instance Attribute Details

#mobile_styleObject (readonly)

Returns the value of attribute mobile_style.



5
6
7
# File 'lib/inkcite/renderer/element.rb', line 5

def mobile_style
  @mobile_style
end

#styleObject (readonly)

Returns the value of attribute style.



5
6
7
# File 'lib/inkcite/renderer/element.rb', line 5

def style
  @style
end

#tagObject (readonly)

Returns the value of attribute tag.



5
6
7
# File 'lib/inkcite/renderer/element.rb', line 5

def tag
  @tag
end

Instance Method Details

#+(html) ⇒ Object Also known as: concat

I found myself doing a lot of Element.new(‘tag’, { }).to_s + ‘more html’ so this method makes it easier by allowing elements to be added to strings.



32
33
34
# File 'lib/inkcite/renderer/element.rb', line 32

def + html
  to_s << html.to_s
end

#[](key) ⇒ Object



37
38
39
# File 'lib/inkcite/renderer/element.rb', line 37

def [] key
  @att[key]
end

#[]=(key, val) ⇒ Object



41
42
43
# File 'lib/inkcite/renderer/element.rb', line 41

def []= key, val
  @att[key] = val
end

#add_rule(rule) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/inkcite/renderer/element.rb', line 45

def add_rule rule

  # Mark the rule as active in case it was one of the pre-defined rules
  # that can be activated on first use.
  rule.activate!

  # Add the rule to those that will affect this element
  responsive_styles << rule

  # Add the rule's klass to those that will be rendered in the
  # element's HTML.
  classes << rule.klass

  rule
end

#classesObject



61
62
63
# File 'lib/inkcite/renderer/element.rb', line 61

def classes
  @classes ||= Set.new
end

#responsive_stylesObject



65
66
67
# File 'lib/inkcite/renderer/element.rb', line 65

def responsive_styles
  @responsive_rules ||= []
end

#self_close?Boolean



69
70
71
# File 'lib/inkcite/renderer/element.rb', line 69

def self_close?
  @self_close
end

#to_helperObject

Generates a Helper tag rather than a string tag - e.g. src=test.png rather than <img src=test.png>



75
76
77
# File 'lib/inkcite/renderer/element.rb', line 75

def to_helper
  to_s('{', '}')
end

#to_s(open = '<', close = '>') ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/inkcite/renderer/element.rb', line 79

def to_s open='<', close='>'

  # Convert the style hash into CSS style attribute.
  @att[:style] = Renderer.quote(Renderer.render_styles(@style)) unless @style.blank?

  # Convert the list of CSS classes assigned to this element into an attribute
  self[:class] = Renderer.quote(@classes.to_a.sort.join(' ')) unless @classes.blank?

  html = open
  html << @tag

  unless @att.empty?
    html << ' '
    html << Renderer.join_hash(@att)
  end

  html << ' /' if self_close?
  html << close

  html
end