Class: AppBuilder::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/app_builder/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, source_path) ⇒ Environment

Returns a new instance of Environment.



6
7
8
9
# File 'lib/app_builder/environment.rb', line 6

def initialize(name, source_path)
  @name        = name
  @source_path = source_path
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/app_builder/environment.rb', line 3

def name
  @name
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



4
5
6
# File 'lib/app_builder/environment.rb', line 4

def source_path
  @source_path
end

Instance Method Details

#[](key) ⇒ Object



24
25
26
# File 'lib/app_builder/environment.rb', line 24

def [](key)
  hash.fetch(key.to_s, nil)
end

#create_file(template_path, output_path) ⇒ Object



11
12
13
14
15
# File 'lib/app_builder/environment.rb', line 11

def create_file(template_path, output_path)
  File.open(output_path, "w") do |f|
    f.write(ERB.new(File.read(template_path)).result(template_binding))
  end
end

#sourceObject



33
34
35
# File 'lib/app_builder/environment.rb', line 33

def source
  @source ||= YAML.load(ERB.new(File.read(source_path)).result(binding))
end

#template_bindingObject



17
18
19
20
21
22
# File 'lib/app_builder/environment.rb', line 17

def template_binding
  env = source.fetch(name.to_s)
  Class.new.tap { |klass|
    klass.define_method(:env) { env }
  }.instance_eval { binding }
end

#to_hashObject Also known as: hash



28
29
30
# File 'lib/app_builder/environment.rb', line 28

def to_hash
  source.fetch(name.to_s)
end