Class: Inkcite::Renderer::Responsive::Rule

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

Direct Known Subclasses

TargetRule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tags, klass, declarations, active = true) ⇒ Rule

Returns a new instance of Rule.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/inkcite/renderer/responsive.rb', line 39

def initialize tags, klass, declarations, active=true
  @klass = klass
  @declarations = declarations

  @tags = Set.new [*tags]

  # By default, a rule isn't considered active until it has
  # been marked used.  This allows the view to declare built-in
  # styles (such as hide or stack) that don't show up in the
  # rendered HTML unless the author references them.
  @active = active

end

Instance Attribute Details

#declarationsObject (readonly)

Returns the value of attribute declarations.



36
37
38
# File 'lib/inkcite/renderer/responsive.rb', line 36

def declarations
  @declarations
end

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

Instance Method Details

#<<(tag) ⇒ Object



53
54
55
# File 'lib/inkcite/renderer/responsive.rb', line 53

def << tag
  @tags << tag
end

#activate!Object



57
58
59
# File 'lib/inkcite/renderer/responsive.rb', line 57

def activate!
  @active = true
end

#active?Boolean

Returns:

  • (Boolean)


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

def active?
  @active
end

#att_selector_stringObject



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

def att_selector_string
  ".#{@klass}"
end

#block?Boolean

Returns:

  • (Boolean)


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

def block?
  declaration_string.downcase.include?('block')
end

#declaration_stringObject



73
74
75
76
77
78
79
80
81
# File 'lib/inkcite/renderer/responsive.rb', line 73

def declaration_string
  if @declarations.is_a?(Hash)
    Renderer.render_styles(@declarations)
  elsif @declarations.is_a?(Array)
    @declarations.join(' ')
  else
    @declarations.to_s
  end
end

#include?(tag) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/inkcite/renderer/responsive.rb', line 83

def include? tag
  universal? || @tags.include?(tag)
end

#to_cssObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/inkcite/renderer/responsive.rb', line 87

def to_css

  rule = ""

  att_selector = att_selector_string

  if universal?

    # Only the attribute selector is needed when the rule is universal.
    # http://www.w3.org/TR/CSS2/selector.html#universal-selector
    rule << att_selector

  else

    # Create an attribute selector that targets each tag.
    @tags.sort.each do |tag|
      rule << ',' unless rule.blank?
      rule << tag
      rule << att_selector
    end

  end

  rule << " { "
  rule << declaration_string
  rule << " }"

  rule
end

#universal?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/inkcite/renderer/responsive.rb', line 117

def universal?
  @tags.include?(UNIVERSAL)
end