Class: HomeAssistant::Generator::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/home_assistant/generator/dsl.rb

Overview

dsl class to read config file and evaluate it

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSL

Returns a new instance of DSL.



12
13
14
# File 'lib/home_assistant/generator/dsl.rb', line 12

def initialize
  @component_list = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/home_assistant/generator/dsl.rb', line 20

def method_missing(name, *args, &block)
  super unless args.one?

  klass_name = name.to_s.split('_').collect(&:capitalize).join
  element = if DSL.const_defined?(klass_name) && DSL.const_get(klass_name)
              debug("Defining #{klass_name} instance")
              DSL.const_get(klass_name).new(*args)
            else
              debug("No #{klass_name} class, fallback on basic component")
              Component.new(*args).tap { |c| c.component_class = name }
            end
  component_list << element
  element.instance_eval(&block) if block_given?
  element
end

Instance Attribute Details

#component_listObject (readonly)

Returns the value of attribute component_list.



10
11
12
# File 'lib/home_assistant/generator/dsl.rb', line 10

def component_list
  @component_list
end

Instance Method Details

#debug(message) ⇒ Object



36
37
38
# File 'lib/home_assistant/generator/dsl.rb', line 36

def debug(message)
  $stderr.puts message if ENV['DEBUG']
end

#eval(file_path) ⇒ Object



16
17
18
# File 'lib/home_assistant/generator/dsl.rb', line 16

def eval(file_path)
  instance_eval(File.read(file_path), file_path)
end

#to_sObject



40
41
42
43
44
45
46
# File 'lib/home_assistant/generator/dsl.rb', line 40

def to_s
  component_list.inject(Mash.new) do |mem, component|
    mem[component.component_class] ||= []
    mem[component.component_class] << component.to_h
    mem
  end.to_hash.to_yaml
end