Class: Takeoff::Configuration

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

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/takeoff/configuration.rb', line 12

def [](name)
  name ||= default_plan
  
  plan = name.is_a?(Class) ? name : plans[name]

  if plan.nil?
    raise "Plan '#{name}' is unknown"
  end

  unless plan <= Plan::Base
    raise "Plan '#{name}' doesn't inherit from Takeoff::Plan::Base" 
  end

  plan
end

#default_plan(name = nil, &block) ⇒ Object



32
33
34
35
36
37
# File 'lib/takeoff/configuration.rb', line 32

def default_plan(name = nil, &block)
  self.default_plan = name            if name
  default_plan.instance_eval(&block)  if block
  
  @default_plan ||= Plan::Default
end

#default_plan=(plan) ⇒ Object



39
40
41
# File 'lib/takeoff/configuration.rb', line 39

def default_plan=(plan)
  @default_plan = self[plan]
end

#plan(name, plan = nil, based_on: default_plan, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/takeoff/configuration.rb', line 45

def plan(name, plan = nil, based_on: default_plan, &block)
  if plan?(name)
    plan ||= self[name]
  else
    plan ||= Plan.const_set(name.to_s.camelize, Class.new(self[based_on]))
    plans[name] = plan
  end

  plan.instance_eval(&block) if block

  plan
end

#plan?(name) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/takeoff/configuration.rb', line 28

def plan?(name)
  self[name] rescue nil
end

#plansObject



8
9
10
# File 'lib/takeoff/configuration.rb', line 8

def plans
  @plans ||= HashWithIndifferentAccess.new
end