Class: Styles::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/styles/engine.rb

Overview

Takes one or more Stylesheets and applies the rules from them to lines of text.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*stylesheets) ⇒ Engine

Returns a new instance of Engine.



9
10
11
# File 'lib/styles/engine.rb', line 9

def initialize(*stylesheets)
  @stylesheets = [stylesheets].flatten
end

Instance Attribute Details

#stylesheetsObject (readonly)

Returns the value of attribute stylesheets.



7
8
9
# File 'lib/styles/engine.rb', line 7

def stylesheets
  @stylesheets
end

Instance Method Details

#process(line) ⇒ Object

Process a line according to the rules that comprise all of the Stylesheets.

For all the rules that are applicable to this line, find the last defined of each type of property and apply it.

Returns nil if the line is hidden or otherwise should not be displayed.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/styles/engine.rb', line 19

def process(line)
  applicable_rules = rules.find_all { |rule| rule.applicable?(line) }

  simple_properties = {}
  multiple_names_properties = {}
  applicable_rules.each do |rule|
    rule.properties.each do |property|
      if property.class.multiple_names?
        (multiple_names_properties[property.class.to_sym] ||= []) << property
      else
        simple_properties[property.class.to_sym] = property
      end
    end
  end

  properties = simple_properties.values

  multiple_names_properties.keys.each do |basic_name|
    props = multiple_names_properties[basic_name]
    prop_class = props.first.class
    properties << prop_class.new(props)
  end

  line_obj = ::Styles::Line.new(line, properties)

  sub_engines.each do |sub_engine|
    line_obj = sub_engine.process(line_obj)
    return nil if line_obj.text.nil?
  end

  line_obj.to_s
end