Class: Raka

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

Overview

initialize raka

Constant Summary collapse

Pattern =
Pattern
P =
Pattern

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, options) ⇒ Raka

Returns a new instance of Raka.



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

def initialize(env, options)
  @env = env
  defaults = {
    output_types: [:csv], input_types: nil,
    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

  # if input_types given, obey it, otherwise use all output types as possible input types
  unless @options.input_types
    @options.input_types = @options.output_types # any output can be used as intermediate
  end
  # 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__), "raka/#{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.



14
15
16
# File 'lib/raka.rb', line 14

def logger
  @logger
end

Instance Method Details

#create_logger(level) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/raka.rb', line 16

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



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

def define_token_creator(ext, ext_alias = nil)
  # closures
  env = @env
  options = @options
  scopes = @scopes
  @env.define_singleton_method(ext_alias || ext) do |*args, **kw|
    # 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, **kw
    )
  end
end

#scope(*names, &block) ⇒ Object



70
71
72
73
74
# File 'lib/raka.rb', line 70

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

#stem(path) ⇒ Object



76
77
78
# File 'lib/raka.rb', line 76

def stem(path)
  File.basename(path, File.extname(path))
end