Module: MasterView::DirectiveHelpers
- Included in:
- DirectiveBase, MasterViewListener, Renderer
- Defined in:
- lib/masterview/directive_helpers.rb
Defined Under Namespace
Modules: DirectivePriorities
Constant Summary collapse
- CRLF =
"\r\n"- ERB_START =
'<% '- ERB_EVAL =
'<%= '- ERB_END =
' %>'
Instance Method Summary collapse
-
#delete_last_in_parent(tag, full_string) ⇒ Object
set the last occurence to empty string returns true if found and set to empty string.
-
#find_last_in_parent(tag, full_string) ⇒ Object
find the last string that fully matches exactly the parent tags content string array It looks for something that has been output as a unit in the array not a substring returns the ref to the string which you can operate on using replace.
-
#find_string_val_in_string_hash(str, key_or_sym) ⇒ Object
find a hash value from inside a simple str containing a hash non-evaling, looks for a :key => ‘foo/bar’ returning foo/bar string.
-
#lowercase_attribute_keys(attributes) ⇒ Object
return attributes with lowercase keys.
-
#lowercase_attribute_keys_and_values(attributes) ⇒ Object
return attributes with lowercase keys and values.
-
#merge_into_embedded_hash(full_string, hash_arg, hash_to_merge) ⇒ Object
merge hash_to_merge values into the hash contained in the full_string, hash_arg is zero based index of which hash this needes to be merged to if there are multiple ones.
-
#parse(str) ⇒ Object
parse into array of strings, containing the various arguments without evaling looks for %q{}, %q[], hash, array, function call using (), values deliminated by commas, it does not handle embedded quotes yet.
-
#parse_eval_into_array(value) ⇒ Object
returns an array of args by parsing and evaling the str value passed in uses evaling, so can’t have any variables only simple strings, numbers, booleans.
-
#parse_eval_into_hash(value, default_key) ⇒ Object
returns a hash, for values that are not already part of hash it adds them using default_key uses evaling so it cannot have any variables or non-simple types.
-
#remove_prepended_strings(full_string) ⇒ Object
remove any strings that were prepended to the hashes, typically these are overridden by other values, so we need to strip them off leaving only the hashes, returns a string with only hashes.
Instance Method Details
#delete_last_in_parent(tag, full_string) ⇒ Object
set the last occurence to empty string returns true if found and set to empty string
44 45 46 47 48 49 |
# File 'lib/masterview/directive_helpers.rb', line 44 def delete_last_in_parent(tag, full_string) str = find_last_in_parent(tag, full_string) found = !str.nil? str.replace('') if found found end |
#find_last_in_parent(tag, full_string) ⇒ Object
find the last string that fully matches exactly the parent tags content string array It looks for something that has been output as a unit in the array not a substring returns the ref to the string which you can operate on using replace
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/masterview/directive_helpers.rb', line 28 def find_last_in_parent(tag, full_string) ret = nil parent = tag.parent unless parent.nil? parent.content.reverse.each do |str| if str == full_string ret = str break end end end ret end |
#find_string_val_in_string_hash(str, key_or_sym) ⇒ Object
find a hash value from inside a simple str containing a hash non-evaling, looks for a :key => ‘foo/bar’ returning foo/bar string
53 54 55 56 57 58 59 |
# File 'lib/masterview/directive_helpers.rb', line 53 def find_string_val_in_string_hash(str, key_or_sym) key = key_or_sym.to_s m = str.match( Regexp.new( Regexp.escape(key)+"\s*=>\s*'([^']*)'" ) ) #try single quote m = str.match( Regexp.new( Regexp.escape(key)+"\s*=>\s*\"([^\"]*)\"" ) ) if m.nil? #try double quote return nil if m.nil? m[1] end |
#lowercase_attribute_keys(attributes) ⇒ Object
return attributes with lowercase keys
162 163 164 165 166 |
# File 'lib/masterview/directive_helpers.rb', line 162 def lowercase_attribute_keys(attributes) lcattrs = {} attributes.each { |k,v| lcattrs[k.downcase] = v } lcattrs end |
#lowercase_attribute_keys_and_values(attributes) ⇒ Object
return attributes with lowercase keys and values
169 170 171 172 173 |
# File 'lib/masterview/directive_helpers.rb', line 169 def lowercase_attribute_keys_and_values(attributes) lcattrs = {} attributes.each { |k,v| lcattrs[k.downcase] = v.downcase } lcattrs end |
#merge_into_embedded_hash(full_string, hash_arg, hash_to_merge) ⇒ Object
merge hash_to_merge values into the hash contained in the full_string, hash_arg is zero based index of which hash this needes to be merged to if there are multiple ones.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/masterview/directive_helpers.rb', line 131 def (full_string, hash_arg, hash_to_merge) return full_string if hash_to_merge.empty? full_string ||= "" sorted_hash_to_merge = hash_to_merge.sort { |a,b| a.to_s <=> b.to_s } #sort, remember the keys might be symbols so use to_s str_to_merge = sorted_hash_to_merge.collect{ |h,v| "#{h.inspect} => #{v.inspect}" }.join(', ') hashes = full_string.scan( /(\{?[^{}]+=>[^{}]+\}?)\s*,?\s*/ ).flatten hash_str = hashes[hash_arg] #be careful to use methods to update string in place or else put back in hash if hash_str.nil? hashes.each do |v| #make sure each prior hash has brackets, since we are adding a hash unless v.index '}' v.insert(0, '{') v.insert(-1, '}') end end hashes[hash_arg] = hash_str = "" end closing_brack = hash_str.index '}' if closing_brack hash_str.insert(closing_brack, ', '+str_to_merge) else hash_str << ', ' unless hash_str.empty? hash_str << str_to_merge end hashes.join(', ') end |
#parse(str) ⇒ Object
parse into array of strings, containing the various arguments without evaling looks for %q{}, %q[], hash, array, function call using (), values deliminated by commas, it does not handle embedded quotes yet
114 115 116 117 118 119 |
# File 'lib/masterview/directive_helpers.rb', line 114 def parse(str) return [] if str.nil? || str.strip.empty? s = str.strip args = [] s.scan( /(%q"[^"]*"|%q\{[^}]*\}|%q\[[^\]]*\]|\{?\s*\S+\s*=>[^{}]+\}?|\[[^\]]*\]|[\w\.@]+\s*\([^\)]*\)|'[^']*'|[^,]+)\s*,?\s*/ ).flatten end |
#parse_eval_into_array(value) ⇒ Object
returns an array of args by parsing and evaling the str value passed in uses evaling, so can’t have any variables only simple strings, numbers, booleans
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/masterview/directive_helpers.rb', line 63 def parse_eval_into_array(value) return [] if value.nil? || value.empty? val = value.strip args = [] until val.empty? if val =~ /^[:'"%\[{&*]/ #starts with quote or ruby lang char v = nil val = '{'+val+'}' if val =~ /^:/ #starts with colon, assume hash so wrap with brackets eval 'v = '+ val #rest is all evaled if v.is_a? Array args += v else args << v end break else unquoted_string = val.slice!( /^[^,]+/ ) #pull off everything up to a comma unquoted_string.strip! args.push unquoted_string val.slice!( /^,/ ) #strip off comma if exists val.strip! end end args end |
#parse_eval_into_hash(value, default_key) ⇒ Object
returns a hash, for values that are not already part of hash it adds them using default_key uses evaling so it cannot have any variables or non-simple types
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/masterview/directive_helpers.rb', line 91 def parse_eval_into_hash(value, default_key) h = {} a = parse_eval_into_array(value) a.each do |v| if v.is_a?(Hash) h.merge!(v) else #it adds any additional non-hash args using default key, if key,val exists, it changes to array and appends prev = h[default_key] if prev.nil? #nil just add it h[default_key] = v elsif prev.is_a?(Array) #was array, concat h[default_key] = prev+v else #anything else, make it into array h[default_key] = [prev, v] end end end h end |
#remove_prepended_strings(full_string) ⇒ Object
remove any strings that were prepended to the hashes, typically these are overridden by other values, so we need to strip them off leaving only the hashes, returns a string with only hashes
123 124 125 126 127 |
# File 'lib/masterview/directive_helpers.rb', line 123 def remove_prepended_strings(full_string) return full_string if full_string.nil? || full_string.strip.empty? hashes = full_string.scan( /(\{?)\s*(\S+\s*=>.*)/ ).flatten hashes.join.strip end |