Class: Rack::ScriptStackerUtils::SpecSolidifier

Inherits:
BasicObject
Defined in:
lib/rack/scriptstacker.rb

Instance Method Summary collapse

Constructor Details

#initialize(stacker_names, directory = nil) ⇒ SpecSolidifier

Returns a new instance of SpecSolidifier.



77
78
79
80
81
# File 'lib/rack/scriptstacker.rb', line 77

def initialize stacker_names, directory=nil
  @stacker_names = stacker_names
  @specs = ::Hash.new { |hash, key|  hash[key] = [] }
  @directory = directory
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rack/scriptstacker.rb', line 95

def method_missing name, *args
  if !@stacker_names.include? name
    ::Kernel.raise ::ArgumentError.new(
      "Expected one of #{@stacker_names}, but got #{name.inspect}."
    )
  end
  if args.size != 1
    ::Kernel.raise ::ArgumentError.new(
      "Expected a path spec like 'static/css' => 'stylesheets', " +
      "but got #{args.inspect} instead."
    )
  end

  if @directory
    path = "#{@directory}/#{args[0]}"
  else
    path = args[0]
  end

  @specs[name].push ::Rack::ScriptStackerUtils::PathSpec.new(path)
end

Instance Method Details

#call(stack_spec) ⇒ Object



83
84
85
86
# File 'lib/rack/scriptstacker.rb', line 83

def call stack_spec
  instance_eval &stack_spec
  @specs
end

#directory(dir_name, &block) ⇒ Object



88
89
90
91
92
93
# File 'lib/rack/scriptstacker.rb', line 88

def directory dir_name, &block
  SpecSolidifier.new(@stacker_names, dir_name).call(block)
    .each do |stacker_name, specs|
      @specs[stacker_name] += specs
    end
end