Class: YamlMaster

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml_master.rb,
lib/yaml_master/version.rb

Defined Under Namespace

Classes: EmbeddedMethods, KeyFetchError

Constant Summary collapse

VERSION =
"0.3.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_or_filename) ⇒ YamlMaster

Returns a new instance of YamlMaster.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/yaml_master.rb', line 21

def initialize(io_or_filename)
  embedded_methods = EmbeddedMethods.new(self)
  embedded_methods_binding = embedded_methods.instance_eval { binding }
  data =
    if io_or_filename.is_a?(IO)
      ERB.new(io_or_filename.read).result(embedded_methods_binding)
    else
      @master_path = File.expand_path(io_or_filename)
      ERB.new(File.read(io_or_filename)).result(embedded_methods_binding)
    end

  @master = YAML.load(data)
  raise "yaml_master key is necessary on toplevel" unless @master["yaml_master"]
  raise "data key is necessary on toplevel" unless @master["data"]
end

Instance Attribute Details

#masterObject (readonly)

Returns the value of attribute master.



19
20
21
# File 'lib/yaml_master.rb', line 19

def master
  @master
end

#master_pathObject (readonly)

Returns the value of attribute master_path.



19
20
21
# File 'lib/yaml_master.rb', line 19

def master_path
  @master_path
end

Instance Method Details

#generate(key, output = nil, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/yaml_master.rb', line 37

def generate(key, output = nil, options = {})
  puts "gen: #{output}" if options[:verbose]
  yaml = YAML.dump(fetch_data_from_master(key))

  return yaml unless output

  File.open(output, 'w') do |f|
    f.write(yaml)
  end
end

#generate_all(options = {}) ⇒ Object



48
49
50
51
52
# File 'lib/yaml_master.rb', line 48

def generate_all(options = {})
  @master["yaml_master"].each do |key, output|
    generate(key, output, options)
  end
end