Class: SassExtractor::Visitor

Inherits:
Sass::Tree::Visitors::Base
  • Object
show all
Defined in:
lib/sass-extractor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefixes) ⇒ Visitor

Returns a new instance of Visitor.



48
49
50
# File 'lib/sass-extractor.rb', line 48

def initialize(prefixes)
  @prefixes = prefixes
end

Class Method Details

.visit(root, prefixes) ⇒ Object



44
45
46
# File 'lib/sass-extractor.rb', line 44

def self.visit(root, prefixes)
  new(prefixes).send(:visit, root)
end

Instance Method Details

#build_hash {|result| ... } ⇒ Object

Yields:

  • (result)


73
74
75
# File 'lib/sass-extractor.rb', line 73

def build_hash
  result = {}; yield result; result
end

#each_property(rule) ⇒ Object



77
78
79
80
81
# File 'lib/sass-extractor.rb', line 77

def each_property(rule)
  rule.children.each do |property|
    yield property.resolved_name, property.resolved_value
  end
end

#extract_properties(rule) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/sass-extractor.rb', line 65

def extract_properties(rule)
  build_hash do |result|
    each_property(rule) do |name, value|
      result[name] = value if interesting? name
    end
  end
end

#interesting?(name) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/sass-extractor.rb', line 83

def interesting?(name)
  @prefixes.any? { |prefix| name.start_with? prefix }
end

#visit_directive(node) ⇒ Object



56
57
# File 'lib/sass-extractor.rb', line 56

def visit_directive(node)
end

#visit_root(node) ⇒ Object



52
53
54
# File 'lib/sass-extractor.rb', line 52

def visit_root(node)
  yield.compact
end

#visit_rule(node) ⇒ Object



59
60
61
62
63
# File 'lib/sass-extractor.rb', line 59

def visit_rule(node)
  selector = node.resolved_rules.to_a.join.gsub(/\s+/, " ")
  properties = extract_properties(node)
  [selector, properties] unless properties == {}
end