Class: Kbt::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/kbt/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, key = nil) ⇒ Template

Returns a new instance of Template.



5
6
7
8
# File 'lib/kbt/template.rb', line 5

def initialize(file_path, key = nil)
  @file_path = file_path.to_s =~ /.yml$/ ? file_path : "#{file_path}.yml"
  @key = key
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



4
5
6
# File 'lib/kbt/template.rb', line 4

def file_path
  @file_path
end

#keyObject (readonly)

Returns the value of attribute key.



4
5
6
# File 'lib/kbt/template.rb', line 4

def key
  @key
end

Instance Method Details

#file_contentObject



14
15
16
# File 'lib/kbt/template.rb', line 14

def file_content
  @file_content ||= File.read(file_path)
end

#keysObject



18
19
20
# File 'lib/kbt/template.rb', line 18

def keys
  key ? key.split('.') : nil
end

#valueObject



22
23
24
25
26
27
# File 'lib/kbt/template.rb', line 22

def value
  return yml unless keys
  keys.inject(yml) do |prev, nxt|
    prev[nxt]
  end
end

#ymlObject



10
11
12
# File 'lib/kbt/template.rb', line 10

def yml
  @yml ||= YAML.load(file_content)
end