Class: StyleTrain::Style
- Inherits:
-
Object
- Object
- StyleTrain::Style
- 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
-
#level ⇒ Object
Returns the value of attribute level.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#properties ⇒ Object
Returns the value of attribute properties.
-
#selectors ⇒ Object
Returns the value of attribute selectors.
Class Method Summary collapse
Instance Method Summary collapse
- #indent(plus = 0) ⇒ Object
-
#initialize(opts) ⇒ Style
constructor
A new instance of Style.
- #make_selector(value) ⇒ Object
- #render(type = :full) ⇒ Object (also: #to_s)
- #render_empty ⇒ Object
- #render_full ⇒ Object
- #render_linear ⇒ Object
- #set_selectors(values, contexts) ⇒ Object
- #spacing(value) ⇒ Object
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
#level ⇒ Object
Returns the value of attribute level.
3 4 5 |
# File 'lib/style_train/style.rb', line 3 def level @level end |
#opts ⇒ Object
Returns the value of attribute opts.
3 4 5 |
# File 'lib/style_train/style.rb', line 3 def opts @opts end |
#properties ⇒ Object
Returns the value of attribute properties.
3 4 5 |
# File 'lib/style_train/style.rb', line 3 def properties @properties end |
#selectors ⇒ Object
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_empty ⇒ Object
79 80 81 |
# File 'lib/style_train/style.rb', line 79 def render_empty "#{indent}/* #{selectors.join(', ')} {} */" end |
#render_full ⇒ Object
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_linear ⇒ Object
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 |