Class: Palmade::Cableguy::Configurator

Inherits:
Object
  • Object
show all
Defined in:
lib/palmade/cableguy/configurator.rb

Direct Known Subclasses

CableConfigurator

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Configurator

Returns a new instance of Configurator.



8
9
10
11
# File 'lib/palmade/cableguy/configurator.rb', line 8

def initialize(*args)
  @args = args
  @sections = { }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/palmade/cableguy/configurator.rb', line 19

def method_missing(method, *args, &block)
  method = method.to_s
  if method[-1,1] == '='
    # this is a setter
    # create a new accessor

    var_name = method[0..-2]
    eval = <<EVAL
def #{var_name}=(val)
@#{var_name} = val
end

def #{var_name}
@#{var_name}
end
EVAL
  self.class.send(:class_eval, eval, __FILE__, __LINE__)
  self.send(method, *args)
elsif block_given?
  update_section(method, *args, &block)

  # create a new section
  eval = <<EVAL
def #{method}(*args, &block)
if block_given?
  update_section(#{method}, *args, &block)
else
  call_section(#{method}, *args)
end
end
EVAL
    self.class.send(:class_eval, eval, __FILE__, __LINE__)
  else
    # generate an error!
    raise ArgumentError, "Section #{method} not defined."
  end
end

Class Method Details

.configure(file_path, *args) ⇒ Object



3
4
5
6
# File 'lib/palmade/cableguy/configurator.rb', line 3

def self.configure(file_path, *args)
  c = Class.new(self).new(*args)
  c.configure(file_path)
end

Instance Method Details

#append_args(*args) ⇒ Object



13
14
15
16
17
# File 'lib/palmade/cableguy/configurator.rb', line 13

def append_args(*args)
  unless args.empty?
    @args += @args
  end
end

#call_section(section, *args) ⇒ Object Also known as: call, include



72
73
74
75
76
77
78
79
80
81
# File 'lib/palmade/cableguy/configurator.rb', line 72

def call_section(section, *args)
  section = prep(section)
  if @sections.include?(section)
    @sections[section].each do |block|
      block.call(*(args + @args))
    end
  else
    raise ArgumentError, "Section #{section} not defined."
  end
end

#configure(file_path) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/palmade/cableguy/configurator.rb', line 62

def configure(file_path)
  if File.exists?(file_path)
    fcontents = File.read(file_path)
    self.instance_eval(fcontents, file_path)
  else
    raise ArgumentError, "File #{file_path} not found"
  end
  self
end

#include?(section) ⇒ Boolean Also known as: has?

Returns:

  • (Boolean)


57
58
59
# File 'lib/palmade/cableguy/configurator.rb', line 57

def include?(section)
  @sections.include?(prep(section))
end