Class: Palmade::Cableguy::TemplateBinding
- Inherits:
-
Object
- Object
- Palmade::Cableguy::TemplateBinding
- Defined in:
- lib/palmade/cableguy/templatebinding.rb
Instance Attribute Summary collapse
-
#arg_hash ⇒ Object
readonly
Returns the value of attribute arg_hash.
-
#cable ⇒ Object
readonly
Returns the value of attribute cable.
-
#cabler ⇒ Object
readonly
Returns the value of attribute cabler.
-
#cabling ⇒ Object
readonly
Returns the value of attribute cabling.
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#output_buffer ⇒ Object
Returns the value of attribute output_buffer.
-
#reserved_keys ⇒ Object
readonly
Returns the value of attribute reserved_keys.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #fill_key_prefix(key) ⇒ Object
- #get(key, group = nil) ⇒ Object
- #get_children(key, group = nil, &block) ⇒ Object
- #get_each_child(key, group = nil, &block) ⇒ Object
- #get_if_key_exists(key, group = nil) ⇒ Object
- #has_key?(key, group = nil) ⇒ Boolean
-
#initialize(cable, cabler, cabling, target) ⇒ TemplateBinding
constructor
A new instance of TemplateBinding.
- #install(source_file, target_file) ⇒ Object
- #join_keys(key) ⇒ Object
- #parse(file_path) ⇒ Object
- #special_parse(parsed, delim = [ '{', '}' ], cabling_only = false) ⇒ Object
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_hash ⇒ Object (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 |
#cable ⇒ Object (readonly)
Returns the value of attribute cable.
3 4 5 |
# File 'lib/palmade/cableguy/templatebinding.rb', line 3 def cable @cable end |
#cabler ⇒ Object (readonly)
Returns the value of attribute cabler.
4 5 6 |
# File 'lib/palmade/cableguy/templatebinding.rb', line 4 def cabler @cabler end |
#cabling ⇒ Object (readonly)
Returns the value of attribute cabling.
5 6 7 |
# File 'lib/palmade/cableguy/templatebinding.rb', line 5 def cabling @cabling end |
#db ⇒ Object (readonly)
Returns the value of attribute db.
9 10 11 |
# File 'lib/palmade/cableguy/templatebinding.rb', line 9 def db @db end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
8 9 10 |
# File 'lib/palmade/cableguy/templatebinding.rb', line 8 def location @location end |
#output_buffer ⇒ Object
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_keys ⇒ Object (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 |
#target ⇒ Object (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
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 |