Class: Vars

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/vars.rb,
lib/vars/options.rb,
lib/vars/version.rb

Defined Under Namespace

Classes: Options

Constant Summary collapse

VERSION =
"0.0.4".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Vars

Returns a new instance of Vars.



16
17
18
# File 'lib/vars.rb', line 16

def initialize(opts = {})
  @options = opts.is_a?(Options) ? opts : Options.new(opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing(name, *args, &block)
  super unless hash.key?(name.to_s)
  hash.fetch(name.to_s)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
# File 'lib/vars.rb', line 20

def [](key)
  hash[key.to_s]
end

#resolve_template(template_file, output_file) ⇒ Object



41
42
43
44
45
# File 'lib/vars.rb', line 41

def resolve_template(template_file, output_file)
  ::File.open(output_file, "w") do |f|
    f.write(::ERB.new(::File.read(template_file), nil, "-").result(__binding__))
  end
end

#resolve_templates(template_path, output_path, excludes: []) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vars.rb', line 24

def resolve_templates(template_path, output_path, excludes: [])
  template_path = ::Pathname.new(template_path)
  output_path   = ::Pathname.new(output_path)
  excludes      = excludes.map(&:to_s)
  template_path.glob("**/*").each do |template_file|
    next if template_file.directory?

    filename = template_file.basename(".erb")
    next unless ([filename.to_s, template_file.basename.to_s] & excludes).empty?

    resolve_template(
      template_file,
      output_path.join(template_file.dirname.join(filename).relative_path_from(template_path))
    )
  end
end