Module: Compressible::Configurable::ClassMethods

Defined in:
lib/compressible/configurable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/compressible/configurable.rb', line 9

def config
  @config
end

Instance Method Details

#add_to_config(type, key, value) ⇒ Object



42
43
44
45
46
47
# File 'lib/compressible/configurable.rb', line 42

def add_to_config(type, key, value)
  raise "Please define a name for the cached #{type.to_s} using ':to => :my_name'" unless key
  item = find_or_create(type, key)
  item[:paths] = value.flatten.collect {|i| asset_name(i)}
  item
end

#configure(value = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/compressible/configurable.rb', line 11

def configure(value = nil)
  raise "invalid config" unless (value.is_a?(String) || value.is_a?(Hash))
  
  config.merge!((value.is_a?(String) ? YAML.load_file(value) : value).recursively_symbolize_keys!)

  # normalize everything to an array
  [:js, :css].each do |type|
    config[type] = [config[type]] unless config[type].is_a?(Array)
  end
  
  self
end

#defaultsObject



28
29
30
31
32
33
34
35
36
# File 'lib/compressible/configurable.rb', line 28

def defaults
  {
    :js => [],
    :css => [],
    :stylesheet_path => defined?(Rails) ? "#{Rails.root}/public/stylesheets" : nil,
    :javascript_path => defined?(Rails) ? "#{Rails.root}/public/javascripts" : nil,
    :read_only => false
  }
end

#define!(*args, &block) ⇒ Object



24
25
26
# File 'lib/compressible/configurable.rb', line 24

def define!(*args, &block)
  Compressible::Builder.define!(*args, &block)
end

#find_or_create(type, key) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/compressible/configurable.rb', line 49

def find_or_create(type, key)
  result = config[type].detect {|i| i[:to].to_s == key.to_s}
  unless result
    result = {:to => key.to_s}
    config[type] << result
  end
  result
end

#inspectObject



62
63
64
# File 'lib/compressible/configurable.rb', line 62

def inspect
  "<#Compressible @config=#{config.inspect}/>"
end

#resetObject



58
59
60
# File 'lib/compressible/configurable.rb', line 58

def reset
  @config = defaults
end