Class: FolderTemplate::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/folder_template/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



15
16
17
18
# File 'lib/folder_template/context.rb', line 15

def initialize()
  @definitions = Hash.new()
  @values_cache = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object


Support methods for DSL implementation




96
97
98
99
# File 'lib/folder_template/context.rb', line 96

def method_missing( method, *args )
  return super unless args.empty?
  _value_for_key( method )
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



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

def definitions
  @definitions
end

Class Method Details

.load(filename) ⇒ Object



25
26
27
# File 'lib/folder_template/context.rb', line 25

def self.load( filename )
  load_from_content( File.read( filename ), filename )
end

.load_from_content(content, filename) ⇒ Object



29
30
31
32
33
# File 'lib/folder_template/context.rb', line 29

def self.load_from_content( content, filename )
  ctx = new
  ctx.instance_eval( content, filename )
  ctx
end

Instance Method Details

#[](key) ⇒ Object


Hash-like API




41
42
43
# File 'lib/folder_template/context.rb', line 41

def [](key)
  return _value_for_key( key )
end

#[]=(key, value) ⇒ Object



45
46
47
48
49
# File 'lib/folder_template/context.rb', line 45

def []=( key, value )
  @definitions[key] = value
  _clear_values_cache()
  self
end

#eachObject



59
60
61
62
63
64
65
# File 'lib/folder_template/context.rb', line 59

def each
  return enum_for(:each) unless block_given?
  @definitions.keys.each do |k|
    v = _value_for_key(k)
    yield k, v
  end
end

#initialize_copy(other) ⇒ Object



20
21
22
23
# File 'lib/folder_template/context.rb', line 20

def initialize_copy( other )
  @definitions = other.definitions.dup
  @values_cache = {}
end

#keysObject



51
52
53
# File 'lib/folder_template/context.rb', line 51

def keys
  return @definitions.keys
end

#let(key, &definition) ⇒ Object


DSL exposed methods




84
85
86
87
88
# File 'lib/folder_template/context.rb', line 84

def let( key, &definition )
  @definitions[key] = definition
  _clear_values_cache()
  self
end

#merge(h) ⇒ Object



75
76
77
# File 'lib/folder_template/context.rb', line 75

def merge( h )
  self.dup.merge!( h )
end

#merge!(h) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/folder_template/context.rb', line 67

def merge!( h )
  h.each do |k,v|
    @definitions[k] = v
  end
  _clear_values_cache()
  self
end

#valuesObject



55
56
57
# File 'lib/folder_template/context.rb', line 55

def values
  return @definitions.keys.map { |k| _value_for_key(k) }
end