Module: BaseChip::Dsl::InstanceMethods

Defined in:
lib/base_chip/dsl.rb

Instance Method Summary collapse

Instance Method Details

#absolute_path(f) ⇒ Object



389
390
391
392
393
394
395
# File 'lib/base_chip/dsl.rb', line 389

def absolute_path(f)
  if f =~ /^\//
    f
  else
    "#{BaseChip.root}/#{f}"
  end
end

#check_type(setting, value, types) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/base_chip/dsl.rb', line 251

def check_type(setting,value,types)
  return if value.nil?
  if types.is_a? Array
    types.each do |t|
      return if value.is_a? t
    end
  else
    return if value.is_a? type
  end
  raise "setting '#{setting}' is set to #{value.inspect} which is of type '#{value.class}', but should have been set to a #{types.map{|t|"'#{t}'"}.join(' or a ')}"
end

#clearObject



271
272
273
274
275
# File 'lib/base_chip/dsl.rb', line 271

def clear
  self.class.possible_settings   .each_key {|s| self.send s, nil                               }
  self.class.possible_child_types.each_key {|s| (hash = self.send s) and hash.delete_if {true} }
  @configured = false
end

#cloneObject



371
372
373
374
375
# File 'lib/base_chip/dsl.rb', line 371

def clone
  c = super
  c.steal_children(self)
  c
end

#configureObject



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/base_chip/dsl.rb', line 276

def configure
  return if @configured

  if foster ; foster.configure ; @modes += foster.modes end
  if parent ; parent.configure ; @modes += parent.modes end
  @modes.uniq!

  verbose "configuring #{@name}"

  find_parent_values(:foster)
  find_parent_values(:parent)
  
  @base_types.each do |t|
    instance_calls(t.blks)
  end
  instance_calls(@blks)

  @configured = true
end

#deep_configureObject



262
263
264
265
266
267
268
269
270
# File 'lib/base_chip/dsl.rb', line 262

def deep_configure
  configure
  self.class.child_types.each_key do |name|
    next unless hash = self.send(name.to_s.pluralize)
    hash.each_value do |child|
      child.deep_configure
    end
  end
end

#file(f = nil) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/base_chip/dsl.rb', line 376

def file(f=nil)
  if f
    @file = absolute_path(f)
    if File.exist? @file
      instance_eval(File.read(@file),@file)
    else
      fault "file '#{@file}' could not be found for #{self.class_string.downcase} '#{@name}'" +
        (self.parent ? " in #{self.parent.class_string.downcase} '#{self.parent.name}'" : '')
    end
  else
    @file
  end
end

#find_file(file) ⇒ Object

methods that probably shouldn’t be here, common to project and block



334
335
336
337
338
339
340
341
# File 'lib/base_chip/dsl.rb', line 334

def find_file(file) 
  found = nil
  @search_paths ||= ['', @work_dir+'/', @top_dir+'/', @project.project_root+'/'].uniq
  @search_paths.each do |p|
    return found if File.exist?(found = "#{p}#{file}")
  end
  fault "Could not resolve path to file #{file.inspect}.  Attempted the following:\n#{@search_paths.map{|p| "  '#{p}#{file}'"}.join("\n")}"
end

#find_parent_values(parent_type) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/base_chip/dsl.rb', line 304

def find_parent_values(parent_type)
  if parent = self.send(parent_type)
    raise "parent #{parent.name} of type #{parent.class.class_string} has not yet been configured" unless parent.configured
  else 
    return
  end
  self.class.possible_settings.each_key do |name|
    next unless (self.send name).nil?
    self.send name, (parent.send name)
  end
  self.class.possible_child_types.each do |name,type|
    plural = name.to_s.pluralize
    next unless child_defaults = parent.send(plural)
    hash = instance_variable_or_equal(plural,HashWithIndifferentAccess.new)
    child_defaults.each do |name,object|
      object2            = (hash[name] ||= type.new)
      object2.name     ||= name
      object2.parent   ||= self
      object2.blks       = object.blks + object2.blks
      object2.abstract ||= object.abstract
    end
  end
end

#full_nameObject Also known as: task_name



342
343
344
345
346
347
348
349
350
# File 'lib/base_chip/dsl.rb', line 342

def full_name
  if self == BaseChip.project
    nil
  elsif @parent and @parent.full_name
    "#{@parent.full_name}:#{@name}"
  else
    @name
  end
end

#initialize(*args) ⇒ Object



241
242
243
244
245
246
# File 'lib/base_chip/dsl.rb', line 241

def initialize(*args)
  super(*args)
  @blks = []
  @modes = []
  @base_types = []
end

#instance_calls(blks) ⇒ Object



295
296
297
298
299
300
301
302
303
# File 'lib/base_chip/dsl.rb', line 295

def instance_calls(blks)
  blks.each do |b|
    if b.parameters.empty?
      self.instance_exec(&b)
    else
      b.call(self)
    end
  end
end

#instance_variable_or_equal(name, value) ⇒ Object



327
328
329
330
331
# File 'lib/base_chip/dsl.rb', line 327

def instance_variable_or_equal(name,value)
  value = instance_variable_get("@#{name}") || value
          instance_variable_set "@#{name}", value
  value
end

#parent=(parent) ⇒ Object



247
248
249
# File 'lib/base_chip/dsl.rb', line 247

def parent=(parent)
  @parent = parent
end

#steal_children(object) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/base_chip/dsl.rb', line 359

def steal_children(object)
  self.class.child_types.each_key do |name|
    plural = name.to_s.pluralize
    ohash = HashWithIndifferentAccess.new
    if ihash = object.send(plural)
      ihash.each do |key,value|
        ohash[key] = value.clone if value
      end
      self.send("#{plural}=", ohash)
    end
  end
end

#steal_settings(object) ⇒ Object



352
353
354
355
356
357
358
# File 'lib/base_chip/dsl.rb', line 352

def steal_settings(object)
  self.class.settings.each_key do |name|
    if (value = object.send(name))
      self.send(name, value)
    end
  end
end