Class: Escalator::EscalatorConfig

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ EscalatorConfig

Returns a new instance of EscalatorConfig.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/escalator/config.rb', line 56

def initialize options={}
  default = {
    input: "asm/main.asm",
    output: "build/main.hex",
  }
  emulator_default = {
    host: "localhost",
    port: 5555,
    protocol: "emu_protocol",
  }

  @config = default.merge options
  @config[:plc] ||= {}
  @config[:plc][:emulator] = @config[:plc][:emulator] ? emulator_default.merge(@config[:plc][:emulator]) : emulator_default

  @config[:default] ||= {}

  @targets = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/escalator/config.rb', line 95

def method_missing(name, *args)
  name = name.to_s unless name.is_a? String
  case name.to_s
  when /(.*)=$/
    @config[$1.to_sym] = args.first
  else
    @config[name.to_sym]
  end
end

Class Method Details

.defaultObject



39
40
41
42
43
# File 'lib/escalator/config.rb', line 39

def default
  @config ||= begin
    load File.join("config", "plc.yml")
  end
end

.load(path) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/escalator/config.rb', line 45

def load path
  h = {}
  if File.exist?(path)
    h = YAML.load(File.read(path))
    h = JSON.parse(h.to_json, symbolize_names: true)
  end
  new h || {}
end

Instance Method Details

#[](key) ⇒ Object



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

def [] key
  @config[key]
end

#target(name = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/escalator/config.rb', line 80

def target name=nil
  name ||= (@config[:default][:target] || :emulator)
  name = name.to_sym if name.is_a? String
  target = @targets[name]
  unless target
    h = @config[:plc][name]
    unless h.nil? || h.empty?
      h = {name:name}.merge h
      target = EscalatorConfigTarget.new h
      @targets[name] = target
    end
  end
  target
end