Class: Raka

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

Overview

initialize raka

Constant Summary collapse

Pattern =
Pattern

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, options) ⇒ Raka



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/raka.rb', line 40

def initialize(env, options)
  @env = env
  defaults = {
    output_types: [:csv], input_types: [],
    type_aliases: {},
    scopes: [],
    lang: ['lang/shell'],
    user_lang: []
  }
  @options = options = OpenStruct.new(defaults.merge(options))

  create_logger options.log_level || (ENV['LOG_LEVEL'] || Logger::INFO).to_i

  @options.input_types |= @options.output_types # any output can be used as intermediate
  # specify root of scopes in options, scopes will append to each root
  @scopes = options.scopes.empty? ? [] : [options.scopes]
  @options.lang.each { |path| load File::join(File::dirname(__FILE__), "#{path}/impl.rb") }
  @options.user_lang.each { |path| load path.to_s + '.rb' }

  # These are where the dsl starts
  @options.output_types.each do |ext|
    define_token_creator(ext, @options.type_aliases[ext])
  end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/raka.rb', line 12

def logger
  @logger
end

Instance Method Details

#create_logger(level) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/raka.rb', line 14

def create_logger(level)
  @env.define_singleton_method :logger do
    logger = Logger.new(STDOUT)
    logger.level = level
    logger
  end
end

#define_token_creator(ext, ext_alias = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/raka.rb', line 22

def define_token_creator(ext, ext_alias = nil)
  # closures
  env = @env
  options = @options
  scopes = @scopes
  @env.define_singleton_method(ext_alias || ext) do |*args|
    # Here the compiler are bound with @options so that when we change @options
    # using methods like scope in Rakefile, the subsequent rules defined will honor
    # the new settings
    # clone to fix the scopes when defining rule
    inline_scope_pattern = !args.empty? ? args[0] : nil
    Token.new(
      DSLCompiler.new(env, options), Context.new(ext, scopes.clone),
      [], inline_scope_pattern
    )
  end
end

#scope(*names, &block) ⇒ Object



65
66
67
68
69
# File 'lib/raka.rb', line 65

def scope(*names, &block)
  @scopes.push(names)
  block.call
  @scopes.pop
end