class Mamiya::DSL

Attributes

hooks[R]
tasks[R]

Public Class Methods

add_hook(name, attributes={}) click to toggle source

Add hook point with name name. This defines method with same name in class to call and define hooks.

# File lib/mamiya/dsl.rb, line 49
def self.add_hook(name, attributes={})
  define_method(name) do |*args, &block|
    @hooks[name] ||= []

    if block
      hook_name = args.shift if args.first.kind_of?(String)
      options = args.pop if args.last.kind_of?(Hash)

      hook = {block: block, options: options || {}, name: hook_name}
      case args.first
      when :overwrite
        @hooks[name] = [hook]
      when :prepend
        @hooks[name][0,0] = [hook]
      else
        @hooks[name] << hook
      end

    else
      matcher = Mamiya::Util::LabelMatcher::Simple.new(args)
      Proc.new { |*args|
        filtered_hooks = @hooks[name].reject { |hook|
          options = hook[:options]

          (options[:only]   && !matcher.match?(*options[:only]  )) ||
          (options[:except] &&  matcher.match?(*options[:except]))
        }

        if attributes[:chain]
          init = args.shift
          filtered_hooks.inject(init) do |result, hook|
            hook[:block].call(result, *args)
          end
        else
          filtered_hooks.each do |hook|
            hook[:block].call *args
          end
        end
      }
    end
  end
end
defaults() click to toggle source

Returns Hash of default setting variables.

# File lib/mamiya/dsl.rb, line 24
def self.defaults
  @defaults ||= {}
end
new() click to toggle source

Creates new DSL environment.

# File lib/mamiya/dsl.rb, line 12
def initialize
  @variables = {}
  @tasks = {}
  @hooks = {}
  @eval_lock = Mutex.new
  @use_lock = Mutex.new
end
set_default(key, value) click to toggle source

Sets default value value for variable name key. Values set by this method will available for all instances of same class.

# File lib/mamiya/dsl.rb, line 38
def self.set_default(key, value)
  k = key.to_sym
  defaults[k] = value
  self.define_variable_accessor(k)
end

Public Instance Methods

[](key) click to toggle source

(DSL) Retrieve value for key key. Value can be set using DSL#set .

# File lib/mamiya/dsl.rb, line 163
def [](key)
  @variables[key] || self.class.defaults[key]
end
evaluate!(string [, filename [, lineno]]) click to toggle source
evaluate! { block }

Evaluates given string or block in DSL environment.

# File lib/mamiya/dsl.rb, line 98
def evaluate!(str = nil, filename = nil, lineno = nil, &block)
  @eval_lock.synchronize {
    begin
      if block_given?
        self.instance_eval(&block)
      elsif str
        @file = filename if filename

        if str && filename && lineno
          self.instance_eval(str, filename.to_s, lineno)
        elsif str && filename
          self.instance_eval(str, filename.to_s)
        elsif str
          self.instance_eval(str)
        end
      end
    ensure
      @file = nil
    end
  }
  self
end
invoke(name) click to toggle source

(DSL) Invoke task named name.

# File lib/mamiya/dsl.rb, line 175
def invoke(name)
  raise TaskNotDefinedError unless @tasks[name]
  self.instance_eval &@tasks[name]
end
load!(file) click to toggle source

Evaluates specified file file in DSL environment.

# File lib/mamiya/dsl.rb, line 123
def load!(file)
  set :_file, Pathname.new(file)
  evaluate! File.read(file), file, 1
end
load_path() click to toggle source

Returns current load path used by use method.

# File lib/mamiya/dsl.rb, line 182
def load_path
  (@variables[:load_path] ||= []) +
    [
      "#{__dir__}/helpers",
      *(@file ? ["#{File.dirname(@file)}/helpers"] : [])
    ]
end
set(key, value) click to toggle source

(DSL) Set value value for variable named key.

# File lib/mamiya/dsl.rb, line 147
def set(key, value)
  k = key.to_sym
  self.class.define_variable_accessor(key) unless self.methods.include?(k)
  @variables[k] = value
end
set_default(key, value) click to toggle source

(DSL) Set value value for variable named key unless value is present for the variable.

# File lib/mamiya/dsl.rb, line 155
def set_default(key, value)
  k = key.to_sym
  return @variables[k] if @variables.key?(k)
  set(k, value)
end
task(name, &block) click to toggle source

(DSL) Define task named name with given block.

# File lib/mamiya/dsl.rb, line 169
def task(name, &block)
  @tasks[name] = block
end
use(name, options={}) click to toggle source

(DSL) Find file using name from current load_path then load. options will be available as variable options in loaded file.

# File lib/mamiya/dsl.rb, line 131
def use(name, options={})
  helper_file = find_helper_file(name)
  raise HelperNotFound unless helper_file

  @use_lock.lock unless @use_lock.owned? # to avoid lock recursively

  @_options = options
  self.instance_eval File.read(helper_file).prepend("options = @_options; @_options = nil;\n"), helper_file, 1

ensure
  @_options = nil
  @use_lock.unlock if @use_lock.owned?
end