Class: AwesomeXmlDsl::Generator

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

Instance Method Summary collapse

Constructor Details

#initialize(data_source:, template:, version_tag: '<?xml version="1.0" encoding="utf-8"?>') ⇒ Generator

Returns a new instance of Generator.



5
6
7
8
9
10
11
12
13
14
# File 'lib/awesome_xml_dsl/generator.rb', line 5

def initialize(data_source:, template:, version_tag: '<?xml version="1.0" encoding="utf-8"?>')
  @data_source = data_source
  @template = template
  @version_tag = version_tag

  @template_dir = File.dirname @template
  @tags = []
  @generator = self
  @depth = -1
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



37
38
39
40
41
# File 'lib/awesome_xml_dsl/generator.rb', line 37

def method_missing(m, *args, &block)
  return @data_source[m] if @data_source.is_a? Hash

  @data_source.send(m, *args, &block)
end

Instance Method Details

#generateObject



16
17
18
19
# File 'lib/awesome_xml_dsl/generator.rb', line 16

def generate
  instance_eval File.read(@template), @template.to_s
  ([@version_tag] + @tags.map(&:to_xml) + ['']).join "\n"
end

#partial(name, options = {}, context = self, context_options = {}) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/awesome_xml_dsl/generator.rb', line 29

def partial(name, options = {}, context = self, context_options = {})
  file_name = File.join(@template_dir, "_#{name}.xml.rb").to_s

  OptionsParser.parse(options, context_options).each do |parsed_options|
    Partial.new(file_name, context, parsed_options).eval
  end
end

#tag(name, options = {}, &block) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/awesome_xml_dsl/generator.rb', line 21

def tag(name, options = {}, &block)
  OptionsParser.parse(options).each do |parsed_options|
    xml_tag = Tag.new(name, self, 0, parsed_options)
    @tags.push xml_tag
    xml_tag.instance_eval(&block) if block_given?
  end
end