Class: StyleTrain::Style

Inherits:
Object
  • Object
show all
Defined in:
lib/style_train/style.rb

Constant Summary collapse

TAGS =
[
  :a, :abbr, :acronym, :address, :area, :b, :base, :bdo, 
  :big, :blockquote, :body, :br, :button, :caption, :center, 
  :cite, :code, :col, :colgroup, :dd, :del, :dfn, :div, 
  :dl, :dt, :em, :embed, :fieldset, :form, :frame, :frameset, 
  :h1, :h2, :h3, :h4, :h5, :h6, :head, :hr, :html, :i, :iframe, 
  :img, :input, :ins, :kbd, :label, :legend, :li, :link, 
  :map, :meta, :noframes, :noscript, :object, :ol, :optgroup, 
  :option, :p, :param, :pre, :q, :s, :samp, :script, :select, 
  :small, :span, :strike, :strong, :sub, :sup, :table, 
  :tbody, :td, :textarea, :tfoot, :th, :thead, :title, 
  :tr, :tt, :u, :ul, :var
]
PSUEDOS =
[
  :link, :visited, :hover, :active, :target, :before, :after, 
  :enabled, :disabled, :checked, :focus
]
INDENT =
'  '

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Style

Returns a new instance of Style.



5
6
7
8
9
10
11
# File 'lib/style_train/style.rb', line 5

def initialize(opts)
  self.opts = opts
  self.level = opts[:level].to_i
  self.selectors = []
  self.properties = []
  set_selectors opts[:selectors], opts[:context] && opts[:context].selectors
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



3
4
5
# File 'lib/style_train/style.rb', line 3

def level
  @level
end

#optsObject

Returns the value of attribute opts.



3
4
5
# File 'lib/style_train/style.rb', line 3

def opts
  @opts
end

#propertiesObject

Returns the value of attribute properties.



3
4
5
# File 'lib/style_train/style.rb', line 3

def properties
  @properties
end

#selectorsObject

Returns the value of attribute selectors.



3
4
5
# File 'lib/style_train/style.rb', line 3

def selectors
  @selectors
end

Class Method Details

.selector(value, opts = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/style_train/style.rb', line 54

def self.selector(value, opts={})
  value = if value.is_a?(String) || (!opts[:exclude_tags] && TAGS.include?(value))
    value.to_s 
  elsif PSUEDOS.include?(value)
    ":#{value}"
  else
    ".#{value}"
  end
  opts[:child] ? "> #{value}" : value
end

Instance Method Details

#indent(plus = 0) ⇒ Object



111
112
113
# File 'lib/style_train/style.rb', line 111

def indent(plus=0)
  INDENT * (level+plus)
end

#make_selector(value) ⇒ Object



50
51
52
# File 'lib/style_train/style.rb', line 50

def make_selector(value)
  self.class.selector(value, opts)
end

#render(type = :full) ⇒ Object Also known as: to_s



67
68
69
70
71
72
73
74
75
# File 'lib/style_train/style.rb', line 67

def render(type=:full)
  if properties.empty?
    render_empty
  elsif type == :full
    render_full
  else
    render_linear
  end
end

#render_emptyObject



79
80
81
# File 'lib/style_train/style.rb', line 79

def render_empty
  "#{indent}/* #{selectors.join(', ')} {} */"
end

#render_fullObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/style_train/style.rb', line 83

def render_full
  str = ""
  
  # selectors
  array = selectors.dup
  last_selector = array.pop
  array.each do |selector|
    str << "#{indent}#{selector},\n"
  end
  str << "#{indent}#{last_selector} {" 
  
  # properties
  properties.each do |property|
    str << "\n#{indent(1)}#{property}"
  end
  str << "\n#{indent}}"
  
  str 
end

#render_linearObject



103
104
105
106
107
108
109
# File 'lib/style_train/style.rb', line 103

def render_linear
  str = "#{indent}"
  str << selectors.join(', ')
  str << " { "
  str << properties.join(' ')
  str << " }"
end

#set_selectors(values, contexts) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/style_train/style.rb', line 17

def set_selectors values, contexts
  if contexts
    contexts.each do |context|
      values.each do |value|
        self.selectors << "#{context}#{spacing(value)}#{make_selector(value)}"
      end
    end
  else
    values.each do |value|
      self.selectors << make_selector(value)
    end
  end
end

#spacing(value) ⇒ Object



13
14
15
# File 'lib/style_train/style.rb', line 13

def spacing(value)
  PSUEDOS.include?(value) || opts[:concat] ? '' : ' '
end