Class: InvoiceCreator::ConfigReader

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/invoice_creator/config_reader.rb

Instance Method Summary collapse

Constructor Details

#initializeConfigReader

Returns a new instance of ConfigReader.



9
10
11
# File 'lib/invoice_creator/config_reader.rb', line 9

def initialize
  @yaml = YAML.load_file(File.expand_path("../../../config.yml", __FILE__))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Hacky way to avoid having to define a method for each of the config properties. Here just for fun and to make it easier to play around with the code. This causes undesireable downstream effects, i.e. calling ‘respond_to?(:anything)` would return true, which it’s fine in this codebase/project



25
26
27
# File 'lib/invoice_creator/config_reader.rb', line 25

def method_missing(method, *args, &block)
  settings.public_send(method) || data.public_send(method) || super
end

Instance Method Details

#dataObject



17
18
19
# File 'lib/invoice_creator/config_reader.rb', line 17

def data
  @data ||= OpenStruct.new(@yaml["data"])
end

#respond_to_missing?(method_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/invoice_creator/config_reader.rb', line 29

def respond_to_missing?(method_name, *args)
  true
end

#settingsObject



13
14
15
# File 'lib/invoice_creator/config_reader.rb', line 13

def settings
  @settings ||= OpenStruct.new(@yaml["settings"])
end