Class: Palmade::Cableguy::TemplateBinding

Inherits:
Object
  • Object
show all
Defined in:
lib/palmade/cableguy/templatebinding.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cable, cabler, cabling, target) ⇒ TemplateBinding

Returns a new instance of TemplateBinding.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/palmade/cableguy/templatebinding.rb', line 13

def initialize(cable, cabler, cabling, target)
  @cable = cable
  @cabler = cabler
  @cabling = cabling
  @target = target
  @location = @cabler.location
  @arg_hash = @cable.args[2]
  @db = @cabler.db
  @key_prefix = []
  @reserved_keys = ['target', 'location']
end

Instance Attribute Details

#arg_hashObject (readonly)

Returns the value of attribute arg_hash.



6
7
8
# File 'lib/palmade/cableguy/templatebinding.rb', line 6

def arg_hash
  @arg_hash
end

#cableObject (readonly)

Returns the value of attribute cable.



3
4
5
# File 'lib/palmade/cableguy/templatebinding.rb', line 3

def cable
  @cable
end

#cablerObject (readonly)

Returns the value of attribute cabler.



4
5
6
# File 'lib/palmade/cableguy/templatebinding.rb', line 4

def cabler
  @cabler
end

#cablingObject (readonly)

Returns the value of attribute cabling.



5
6
7
# File 'lib/palmade/cableguy/templatebinding.rb', line 5

def cabling
  @cabling
end

#dbObject (readonly)

Returns the value of attribute db.



9
10
11
# File 'lib/palmade/cableguy/templatebinding.rb', line 9

def db
  @db
end

#locationObject (readonly)

Returns the value of attribute location.



8
9
10
# File 'lib/palmade/cableguy/templatebinding.rb', line 8

def location
  @location
end

#output_bufferObject

Returns the value of attribute output_buffer.



11
12
13
# File 'lib/palmade/cableguy/templatebinding.rb', line 11

def output_buffer
  @output_buffer
end

#reserved_keysObject (readonly)

Returns the value of attribute reserved_keys.



10
11
12
# File 'lib/palmade/cableguy/templatebinding.rb', line 10

def reserved_keys
  @reserved_keys
end

#targetObject (readonly)

Returns the value of attribute target.



7
8
9
# File 'lib/palmade/cableguy/templatebinding.rb', line 7

def target
  @target
end

Instance Method Details

#fill_key_prefix(key) ⇒ Object



80
81
82
83
84
# File 'lib/palmade/cableguy/templatebinding.rb', line 80

def fill_key_prefix(key)
  key.split('.').each do |k|
    @key_prefix << k
  end
end

#get(key, group = nil) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/palmade/cableguy/templatebinding.rb', line 45

def get(key, group = nil)
  if !@key_prefix.empty?
    key = join_keys(key)
  end

  @db.get(key, group)
end

#get_children(key, group = nil, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/palmade/cableguy/templatebinding.rb', line 53

def get_children(key, group = nil, &block)
  if block_given?
    fill_key_prefix(key)

    yield @db.get_children(key, group)
    @key_prefix.clear
  else
    @db.get_children(key, group)
  end
end

#get_each_child(key, group = nil, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/palmade/cableguy/templatebinding.rb', line 64

def get_each_child(key, group = nil, &block)
  if block_given?
    fill_key_prefix(key)

    children = @db.get_children(key, group)

    children.each do |c|
      yield c
    end

    @key_prefix.clear
  else
    @db.get_children(key, group)
  end
end

#get_if_key_exists(key, group = nil) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/palmade/cableguy/templatebinding.rb', line 37

def get_if_key_exists(key, group = nil)
  if !@key_prefix.empty?
    key = join_keys(key)
  end

  @db.get_if_key_exists(key, group)
end

#has_key?(key, group = nil) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
# File 'lib/palmade/cableguy/templatebinding.rb', line 29

def has_key?(key, group = nil)
  if !@key_prefix.empty?
    key = join_keys(key)
  end

  @db.has_key?(key, group)
end

#install(source_file, target_file) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/palmade/cableguy/templatebinding.rb', line 109

def install(source_file, target_file)
  parsed = parse(source_file)

  File.open(target_file, 'w') do |f|
    f.write(parsed)
  end
  target_file
end

#join_keys(key) ⇒ Object



25
26
27
# File 'lib/palmade/cableguy/templatebinding.rb', line 25

def join_keys(key)
  "#{@key_prefix.join('.')}.#{key}"
end

#parse(file_path) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/palmade/cableguy/templatebinding.rb', line 86

def parse(file_path)
  Palmade::Cableguy.require_erb
  fcontents = File.read(file_path)

  parsed = ERB.new(fcontents, nil, "-%>", "@output_buffer").result(binding)
  parsed = special_parse(parsed, [ '{', '}' ], false)
end

#special_parse(parsed, delim = [ '{', '}' ], cabling_only = false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/palmade/cableguy/templatebinding.rb', line 94

def special_parse(parsed, delim = [ '{', '}' ], cabling_only = false)
  delim0 = "\\#{delim[0]}"
  delim1 = "\\#{delim[1]}"

  parsed = parsed.gsub(/#{delim0}(.+)#{delim1}/) do |match|
    found = $1

    if @reserved_keys.include?(found)
      eval_ret = self.send(found)
    else
      eval_ret = get(found)
    end
  end
end