Class: Stylist::StylesheetCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/stylist/stylesheet_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStylesheetCollection

Returns a new instance of StylesheetCollection.



7
8
9
# File 'lib/stylist/stylesheet_collection.rb', line 7

def initialize
  reset!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

by default, route missing methods to the collection hash otherwise hit the :all array



84
85
86
87
88
89
90
# File 'lib/stylist/stylesheet_collection.rb', line 84

def method_missing(name, *args, &block)
  if @collection.respond_to? name
    @collection.send(name, *args, &block)
  else
    @collection[:all].send(name, *args, &block)
  end
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



3
4
5
# File 'lib/stylist/stylesheet_collection.rb', line 3

def collection
  @collection
end

Instance Method Details

#append(*args) ⇒ Object Also known as: <<, push



19
20
21
22
23
24
25
# File 'lib/stylist/stylesheet_collection.rb', line 19

def append(*args)
  options = args.extract_options!.symbolize_keys!
  media = options.delete(:media) || configuration.default_media

  @collection[media] |= args.flatten.compact
  self
end

#configurationObject



5
# File 'lib/stylist/stylesheet_collection.rb', line 5

def configuration; ::Stylist.configuration; end

#defaultObject



15
16
17
# File 'lib/stylist/stylesheet_collection.rb', line 15

def default
  @collection[configuration.default_media]
end

#filtered_stylesheet_name(stylesheet) ⇒ Object



71
72
73
74
75
76
# File 'lib/stylist/stylesheet_collection.rb', line 71

def filtered_stylesheet_name(stylesheet)
  name = stylesheet.to_s
  name.gsub!(/^[+-]*/, '').gsub!(/[+-]*$/, '')

  return stylesheet.is_a?(Symbol) ? name.to_sym : name
end

#manipulate(*args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/stylist/stylesheet_collection.rb', line 46

def manipulate(*args)
  return self if args.empty?
  
  options = args.extract_options!.symbolize_keys!
  media = options.delete(:media) || configuration.default_media

  args.flatten.compact.each do |stylesheet|
    name_as_string = stylesheet.to_s.clone
    stylesheet = filtered_stylesheet_name(stylesheet)
  
    case
      when name_as_string.starts_with?('+')
        append(stylesheet, :media => media)
      when name_as_string.ends_with?('+')
        prepend(stylesheet, :media => media)
      when name_as_string.starts_with?('-')
        remove(stylesheet, :media => media)
      else
        append(stylesheet, :media => media)
    end
  end

  self
end

#prepend(*args) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/stylist/stylesheet_collection.rb', line 30

def prepend(*args)
  options = args.extract_options!.symbolize_keys!
  media = options.delete(:media) || configuration.default_media

  @collection[media] = args.flatten.compact | @collection[media]
  self
end

#process!Object



78
79
80
# File 'lib/stylist/stylesheet_collection.rb', line 78

def process!
  configuration.processors.each { |processor| processor.process!(@collection) }
end

#remove(*args) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/stylist/stylesheet_collection.rb', line 38

def remove(*args)
  options = args.extract_options!.symbolize_keys!
  media = options.delete(:media) || configuration.default_media

  @collection[media] -= args
  self
end

#reset!Object



11
12
13
# File 'lib/stylist/stylesheet_collection.rb', line 11

def reset!
  @collection = ::ActiveSupport::OrderedHash.new { |h, k| h[k] = [] }
end