Class: RubyCss::Selector
- Inherits:
-
Object
- Object
- RubyCss::Selector
- Defined in:
- lib/ruby_css.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#name ⇒ Object
Returns the value of attribute name.
-
#properties ⇒ Object
Returns the value of attribute properties.
Instance Method Summary collapse
-
#c(id_name, &blk) ⇒ Object
Generates CSS for a given class name.
-
#id(id_name, &blk) ⇒ Object
Generates CSS for a given ID.
-
#initialize(name, &blk) ⇒ Selector
constructor
A new instance of Selector.
-
#method_missing(method_name, *args, &blk) ⇒ Object
Handles generation of CSS for any HTML tag.
-
#s(selector_name, &blk) ⇒ Object
Generates CSS for the given selector name.
-
#to_s ⇒ Object
Generates the formatted output CSS as a string.
Constructor Details
#initialize(name, &blk) ⇒ Selector
Returns a new instance of Selector.
8 9 10 11 12 13 |
# File 'lib/ruby_css.rb', line 8 def initialize(name, &blk) @properties = {} @name = name.to_s @children = [] instance_eval(&blk) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &blk) ⇒ Object
Handles generation of CSS for any HTML tag.
Example: nav { float ‘right’ }
Generates: nav { float: right; }
TODO: Create selectors only for valid HTML tags.
69 70 71 72 73 74 75 |
# File 'lib/ruby_css.rb', line 69 def method_missing(method_name, *args, &blk) if block_given? @children << Selector.new([@name, method_name].join(' '), &blk) else @properties[method_name.to_s.sub('_', '-')] = args[0] end end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
6 7 8 |
# File 'lib/ruby_css.rb', line 6 def children @children end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/ruby_css.rb', line 6 def name @name end |
#properties ⇒ Object
Returns the value of attribute properties.
6 7 8 |
# File 'lib/ruby_css.rb', line 6 def properties @properties end |
Instance Method Details
#c(id_name, &blk) ⇒ Object
Generates CSS for a given class name.
Example: c(‘post’) { font_size ‘12px’ }
Generates: .post { font-size: 12px; }
53 54 55 |
# File 'lib/ruby_css.rb', line 53 def c(id_name, &blk) @children << Selector.new("#{@name} .#{id_name}", &blk) end |
#id(id_name, &blk) ⇒ Object
Generates CSS for a given ID.
Example: id(‘posts’) { font_size ‘12px’ }
Generates: #posts { font-size: 12px; }
39 40 41 |
# File 'lib/ruby_css.rb', line 39 def id(id_name, &blk) @children << Selector.new("#{@name} \##{id_name}", &blk) end |
#s(selector_name, &blk) ⇒ Object
Generates CSS for the given selector name.
Example: s(‘h1.title’) { font_size ‘2em’ }
Generates: h1.title { font-size: 2em; }
25 26 27 |
# File 'lib/ruby_css.rb', line 25 def s(selector_name, &blk) @children << Selector.new("#{@name} #{selector_name}", &blk) end |
#to_s ⇒ Object
Generates the formatted output CSS as a string.
79 80 81 82 83 84 85 |
# File 'lib/ruby_css.rb', line 79 def to_s css = "#{@name} {\n" @properties.each_pair { |k, v| css += " #{k}: #{v};\n" } css += "}\n\n" @children.each { |c| css += c.to_s } css end |