Class: Confection::DSL

Inherits:
Module
  • Object
show all
Defined in:
lib/confection/dsl.rb

Overview

The File class is used to evaluate the configuration file.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, file = nil) ⇒ DSL

Initialize new DSL object.

Parameters:

  • store (Store)

    The configuration storage instance.

  • file (String) (defaults to: nil)

    Configuration file to load.



32
33
34
35
36
37
38
39
40
# File 'lib/confection/dsl.rb', line 32

def initialize(store, file=nil)
  @_store   = store
  @_file    = file

  @_contest = Object.new
  @_context.extend self

  @_options = {}     
end

Class Method Details

.parse(store, file) ⇒ Object

TODO:

Does the exception rescue make sense here?

Create new DSL instance and parse file.



12
13
14
15
16
17
18
19
20
21
# File 'lib/confection/dsl.rb', line 12

def self.parse(store, file)
  dsl = new(store, file)
  begin
    text = File.read(file)
  rescue => e
    raise e if $DEBUG
    warn e.message
  end
  dsl.instance_eval(text, file)
end

Instance Method Details

#config(tool, *args, &block) ⇒ Object

TODO:

Clean this code up.

Configure a tool.

Examples:

config :rake, "*.rake"
profile :cov do
  config :qed, "qed/simplecov.rb"
end

Parameters:

  • tool (Symbol)

    The name of the tool to configure.

  • opts (Hash)

    Configuration options.

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/confection/dsl.rb', line 85

def config(tool, *args, &block)
  case args.first
  when Symbol
    profile = args.shift
  when String
    profile = args.shift unless args.first.index("\n")
  end

  case args.first
  when Hash
    data = args.shift
    from = data[:from] # special key
  when Proc
    data = args.shift
    # TODO convert data into OpenHash like object
  when String
    data = args.shift
    data = data.tabto(0)
  end

  raise ArgumentError, "too many arguments"      if args.first
  raise SyntaxError,   "nested profile sections" if profile && @_options[:profile]
  #raise ArgumentError, "use block or :from  setting" if options[:from]  && block

  profile = @_options[:profile] unless profile

  if from
    @_store.import(tool, profile, data, &block)
    data = nil
    return unless block
  end

  #original_state = @_options.dup

  if data && block
    raise ArgumentError, "must use data or block, not both"
  end

  @_store << Config.new(tool, profile, @_context, data, &block)

  #@_options = original_state
end

#import(feature) ⇒ Object

TODO:

Make a core extension ?

Evaluate script directory into current scope.

Raises:

  • (LoadError)


135
136
137
138
139
# File 'lib/confection/dsl.rb', line 135

def import(feature)
  file = Find.load_path(feature).first
  raise LoadError, "no such file -- #{feature}" unless file
  instance_eval(::File.read(file), file) if file
end

#profile(name, options = {}, &block) ⇒ Object

Profile block.

Raises:

  • (SyntaxError)


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/confection/dsl.rb', line 47

def profile(name, options={}, &block)
  raise SyntaxError, "nested tool sections" if @_options[:profile]

  original_state = @_options.dup

  @_options.update(options)  # TODO: maybe be more exacting about this
  @_options[:profile] = name.to_sym

  instance_eval(&block)

  @_options = original_state
end