Module: MasterView::DirectiveHelpers
- Included in:
- Analyzer::Listener, DirectiveBaseOld, TemplateProcessing::Renderer, TemplateProcessing::SAXParserListener, TemplateSpec
- Defined in:
- lib/masterview/directive_helpers.rb
Overview
Mixin services for directive implementation classes.
Subclasses of MasterView::DirectiveBase inherit this mixin.
Constant Summary collapse
- CRLF =
"\r\n"
- ERB_EVAL_START =
start of ERB which is evaluated but does not contribute content to the document
'<% '
- ERB_EVAL_END =
end of ERB which is evaluated but does not contribute content to the document
' -%>'
- ERB_CONTENT_START =
start of ERB whose evaluation results in content in the document
'<%= '
- ERB_CONTENT_END =
end of ERB whose evaluation results in content in the document
' %>'
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.
-
#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,.
-
#quote(str, quote_char = '\'') ⇒ Object
add single quotes around string.
-
#quote_if(str) ⇒ Object
adds single quotes around string if it is a simple word [a-zA-Z0-9_]* otherwise return existing string also quote if empty string.
-
#render_partial_name_to_file_name(render_partial_name, default_extension) ⇒ Object
convert render_partial_name to file_name, ex foo/bar to foo/_bar.rhtml.
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
64 65 66 67 68 69 |
# File 'lib/masterview/directive_helpers.rb', line 64 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
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/masterview/directive_helpers.rb', line 38 def find_last_in_parent(tag, full_string) ret = nil parent = tag.parent unless parent.nil? parent.content.reverse.each do |str| if str.kind_of? Array # if it is a nested array check inside of it str.reverse.each do |s| if s == full_string ret = s break end end break if ret else if str == full_string ret = str break end 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
73 74 75 76 77 78 79 |
# File 'lib/masterview/directive_helpers.rb', line 73 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 |
#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,
83 84 85 |
# File 'lib/masterview/directive_helpers.rb', line 83 def parse(str) AttrStringParser.parse(str) end |
#quote(str, quote_char = '\'') ⇒ Object
add single quotes around string
88 89 90 |
# File 'lib/masterview/directive_helpers.rb', line 88 def quote(str, quote_char='\'') quote_char+str+quote_char end |
#quote_if(str) ⇒ Object
adds single quotes around string if it is a simple word [a-zA-Z0-9_]* otherwise return existing string also quote if empty string
95 96 97 |
# File 'lib/masterview/directive_helpers.rb', line 95 def quote_if(str) (str =~ /^\w*$/) ? quote(str) : str end |
#render_partial_name_to_file_name(render_partial_name, default_extension) ⇒ Object
convert render_partial_name to file_name, ex foo/bar to foo/_bar.rhtml
22 23 24 25 26 27 28 29 |
# File 'lib/masterview/directive_helpers.rb', line 22 def render_partial_name_to_file_name(render_partial_name, default_extension) pathname = Pathname.for_path(render_partial_name) dir_pathname = pathname.dirname base = pathname.basename(pathname.extname).to_s filename = '_'+base filename += default_extension if default_extension path = (dir_pathname+filename).to_s end |