Class: Stylish::Generate::Variable
- Inherits:
-
Object
- Object
- Stylish::Generate::Variable
- Defined in:
- lib/stylish/generate.rb
Overview
Variables are elements of a selector tree that haven’t been assigned values yet. When a tree that includes Variable objects is serialised, it must be passed a symbol table so that the variables may be given values.
Instance Method Summary collapse
-
#initialize(name_or_hash, constructor = nil) ⇒ Variable
constructor
When Variable objects are initialised they may be given either a simple symbol, or a compound object (such as a hash) which contains symbols.
-
#to_s(symbols) ⇒ Object
The symbol table is given as an argument to the root element of a selector tree when it is serialised, and passed down to each node as the tree is traversed.
Constructor Details
#initialize(name_or_hash, constructor = nil) ⇒ Variable
When Variable objects are initialised they may be given either a simple symbol, or a compound object (such as a hash) which contains symbols. When a compound object is given, a constructor must also be given.
varbg = Variable.new({:image => :button, :color => :bright})
varbg.to_s({:button => "button.png", :bright => "0f0"})
Which would give the following:
background-image:url('button.png'); background-color:#0f0;
Constructors can also be given for simple values, e.g. when creating a Color.
varc = Variable.new(:bright, Color)
varc.to_s({:bright => "f00"}) # => "#f00"
102 103 104 105 |
# File 'lib/stylish/generate.rb', line 102 def initialize(name_or_hash, constructor = nil) @name = name_or_hash @constructor = constructor end |
Instance Method Details
#to_s(symbols) ⇒ Object
The symbol table is given as an argument to the root element of a selector tree when it is serialised, and passed down to each node as the tree is traversed. Nodes must then serialise themselves, and if they contain Variables they must pass them the symbol table so that they can be resolved to a given value.
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/stylish/generate.rb', line 112 def to_s(symbols) if @constructor.nil? symbols[@name] elsif @name.is_a? Symbol @constructor.new(symbols[@name]).to_s else @constructor.new(@name.to_a.inject({}) {|a, e| a[e.first] = symbols[e.last] a }).to_s end end |