Class: MasterView::KeywordExpander
- Inherits:
-
Object
- Object
- MasterView::KeywordExpander
- Defined in:
- lib/masterview/keyword_expander.rb
Overview
KeywordExpander is used to hold variables defined to be used in expansion of directive attributes.
Keywords in a directive attribute value are expanded into their variable values in the current template processing contenxt prior to invocation of the directive processing implementation.
Constant Summary collapse
- KW_TEMPLATE_PATH =
– keywords related to path information about the template being processed ++ relative path of the template being processed within the template src dir (does not include the template flie extension)
'{template_path}'
- KW_TEMPLATE_DIR_PATH =
relative path of the directory containing the template being processed within the template src dir. (” if template is directly contained in the templates src dir)
'{template_dir_path}'
- KW_TEMPLATE_BASENAME =
base filename of the template being processed (no file extension)
'{template_basename}'
- KW_EXTENSION =
filename extension of the template being processed (ordinarily ‘.html’)
'{extension}'
- KW_DEFAULT_EXTENSION =
the default extension for generated output (ordinarily ‘.rhthml’)
'{default_extension}'
- TEMPLATE_KEYWORDS =
list of all supported keywords related to the template being processed
[ KW_TEMPLATE_PATH, KW_TEMPLATE_DIR_PATH, KW_TEMPLATE_BASENAME, KW_EXTENSION ]
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#expand_keywords(str) ⇒ Object
expand all keywords, return value.
-
#initialize(hash = {}) ⇒ KeywordExpander
constructor
A new instance of KeywordExpander.
-
#resolveAttrAndDelete(attributes, key) ⇒ Object
finds the attribute using the key deleting it from hash, expands value using current binding which allows KEYWORD substitutions before returning string, return nil if not found.
-
#set(key, value) ⇒ Object
set the value, regenerate a reverse sorted hash value array so that it will replace the longer replacement keys before the shorter ones, so FOO_BAR will be replaced before FOO.
-
#set_template_pathname(template_pathname, default_erb_extension) ⇒ Object
Sets a variety of path related keywords:.
Constructor Details
#initialize(hash = {}) ⇒ KeywordExpander
Returns a new instance of KeywordExpander.
30 31 32 33 |
# File 'lib/masterview/keyword_expander.rb', line 30 def initialize(hash = {}) @hash = hash build_key_value_array_sorted_by_desc_length end |
Instance Method Details
#[](key) ⇒ Object
83 84 85 |
# File 'lib/masterview/keyword_expander.rb', line 83 def [](key) @hash[key] end |
#expand_keywords(str) ⇒ Object
expand all keywords, return value
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/masterview/keyword_expander.rb', line 88 def (str) # maybe consider: add parm check_template_path=false that mv:generate processor would turn on # so that ext. defaulting cleverness only applies in that specific context. # Might be overaggressive (or just unnecessary) to this this on *all* substitution mappings # [DJL 18-Jun-2006] return nil if str.nil? append_default_ext = str.ends_with?(KW_TEMPLATE_PATH) and @hash.has_key?(KW_DEFAULT_EXTENSION) val = str.clone @hash_values_by_desc_key_length.each { |k,v| val.gsub!(k,v) } val << @hash[KW_DEFAULT_EXTENSION] if append_default_ext val end |
#resolveAttrAndDelete(attributes, key) ⇒ Object
finds the attribute using the key deleting it from hash, expands value using current binding which allows KEYWORD substitutions before returning string, return nil if not found
103 104 105 106 107 |
# File 'lib/masterview/keyword_expander.rb', line 103 def resolveAttrAndDelete(attributes, key) attr_value = attributes.delete(key) attr_value = (attr_value) unless attr_value.nil? attr_value end |
#set(key, value) ⇒ Object
set the value, regenerate a reverse sorted hash value array so that it will replace the longer replacement keys before the shorter ones, so FOO_BAR will be replaced before FOO
78 79 80 81 |
# File 'lib/masterview/keyword_expander.rb', line 78 def set(key, value) @hash[key] = value build_key_value_array_sorted_by_desc_length end |
#set_template_pathname(template_pathname, default_erb_extension) ⇒ Object
Sets a variety of path related keywords:
MasterView supports the following keyword expansions for mv:generate, mv:gen_partial, mv:import, and mv:import_render
– The default generated output extension is MasterView::GeneratedFileDefaultExtension, per the masterview config.output_filename_extension setting. ++
For template file one/foo/bar.html
when default generated output extension ‘.rhtml’:
{template_path} == use original masterview template path with default output extension (one/foo/bar.rhtml)
{template_path}.ext == use original masterview template path with the specified extension (one/foo/bar.ext)
{template_dir_path} == direct_parent_dirname (one/foo)
{template_basename} == basename (bar)
{extension} == extension (.html)
For example: with MasterView template file one/two/three.html
and default generated output extension ‘.rhtml’
mv:generate="{template_path}" expands to mv:generate="one/two/three.rhtml" (default generation output extension used)
mv:generate="{template_path}.rcss" expands to mv:generate="one/two/three.rcss"
mv:generate="{template_dir_path}/{template_basename}" expands to mv:generate="one/two/three" (no extension)
mv:generate="{template_dir_path}/{template_basename}.rcss" expands to mv:generate="one/two/three.rcss"
mv:generate="{template_dir_path}/{template_basename}-bar.rtxt" expands to mv:generate="one/two/three-bar.rtxt"
mv:generate="somewhere/{template_basename}-bar{extension}" expands to mv:generate="somewhere/three-bar.html"
Note: All path values use forward slashes
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/masterview/keyword_expander.rb', line 63 def set_template_pathname(template_pathname, default_erb_extension) return if template_pathname.nil? pn = Pathname.for_path(template_pathname) default_erb_extension = '' if default_erb_extension.nil? @hash[KW_TEMPLATE_PATH] = convert_pathname_to_s(pn.path_no_ext) #defer decision: + default_erb_extension @hash[KW_TEMPLATE_DIR_PATH] = convert_pathname_to_s(pn.dirname) @hash[KW_TEMPLATE_BASENAME] = convert_pathname_to_s(pn.basename(pn.extname)) @hash[KW_EXTENSION] = pn.extname @hash[KW_DEFAULT_EXTENSION] = default_erb_extension build_key_value_array_sorted_by_desc_length end |