Class: Maglove::Engine::StyleBuilder
- Inherits:
-
Object
- Object
- Maglove::Engine::StyleBuilder
- Defined in:
- lib/maglove/engine/style_builder.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#styles ⇒ Object
Returns the value of attribute styles.
Instance Method Summary collapse
- #add(style, value = nil, template = nil) ⇒ Object
- #add_multi(styles) ⇒ Object
-
#initialize(options, styles = []) ⇒ StyleBuilder
constructor
A new instance of StyleBuilder.
- #process {|_self| ... } ⇒ Object
- #sanitize_style(value) ⇒ Object
Constructor Details
#initialize(options, styles = []) ⇒ StyleBuilder
Returns a new instance of StyleBuilder.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/maglove/engine/style_builder.rb', line 7 def initialize(, styles = []) @options = {} @styles = {} # expand margins if styles.include?(:margin) styles |= [:margin_top, :margin_right, :margin_bottom, :margin_left] styles.reject! { |_style| styles == :margin } end # expand paddings if styles.include?(:padding) styles |= [:padding_top, :padding_right, :padding_bottom, :padding_left] styles.reject! { |_style| styles == :padding } end # build sanitized options .each do |k, v| @options[sanitize_style(k)] = v end # add initial styles add_multi(styles) end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/maglove/engine/style_builder.rb', line 4 def @options end |
#styles ⇒ Object
Returns the value of attribute styles.
5 6 7 |
# File 'lib/maglove/engine/style_builder.rb', line 5 def styles @styles end |
Instance Method Details
#add(style, value = nil, template = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/maglove/engine/style_builder.rb', line 44 def add(style, value = nil, template = nil) style = sanitize_style(style) # handle empty value field if (!value || value.empty?) && !(!(@options[style]) || @options[style].empty?) value = @options[style] end # apply template if value && !value.empty? && template && !template.empty? value = ERB.new(template).result(binding) end @styles[style] = value if value && !value.empty? end |
#add_multi(styles) ⇒ Object
38 39 40 41 42 |
# File 'lib/maglove/engine/style_builder.rb', line 38 def add_multi(styles) styles.each do |style| add(style) end end |
#process {|_self| ... } ⇒ Object
32 33 34 35 36 |
# File 'lib/maglove/engine/style_builder.rb', line 32 def process(&block) yield self if block_given? result = @styles.map { |k, v| "#{k}: #{v};" }.join(' ') result.empty? ? nil : result end |
#sanitize_style(value) ⇒ Object
60 61 62 63 64 |
# File 'lib/maglove/engine/style_builder.rb', line 60 def sanitize_style(value) value.to_s.gsub(/([A-Z]+)([A-Z][a-z])/, '\1-\2') .gsub(/([a-z\d])([A-Z])/, '\1-\2') .tr('_', '-').downcase end |