module MasterView # establish the module into which directives define themselves module Directives #:nodoc end class DirectiveBase include PluginLoadTracking include DirectiveHelpers # Register a class manually without regard to whether it inherits from DirectiveBase. # Classes which derive from DirectiveBase will automatically be registered as they are # loaded. def self.register_directive(directive_class) self.register_class(directive_class) end def initialize(attribute_value) @attribute_value = attribute_value end #if this method exists, it will be called by renderer to save directive_call_stack before each method call def save_directive_call_stack(directive_call_stack) @directive_call_stack = directive_call_stack end # returns the full attribute name which is the mv_ns prefix + attr_name if that method has been provided, otherwise # it default to the class name (without any module prefix) and it downcases the first letter def self.full_attr_name(namespace_prefix) namespace_prefix + (self.respond_to?(:attr_name) ? self.attr_name : self.name.split(':').last.downcase_first_letter) end #get the directives attribute value string def attr_value @attribute_value end #set the directives attribute value string def attr_value=(attribute_value) @attribute_value = attribute_value end #get attribute hash from tag def attrs @directive_call_stack.context[:tag].attributes end #set attribute hash for tag def attrs=(attributes) @directive_call_stack.context[:tag].attributes = attributes end #get attribute hash with lowercased keys and values, and cache it def attrs_lckv @attrs_lckv ||= lowercase_attribute_keys_and_values(attrs) end #get attribute hash with lowercased keys (and original values) and cache it def attrs_lck @attrs_lck ||= lowercase_attribute_keys(attrs) end #returns true if the value for lckey of the attribute hash with lowercased keys and values #matches (lowercase) lcmatch string def attr_lckv_matches(lckey, lcmatch) (attrs_lckv[lckey] && attrs_lckv[lckey] == lcmatch.downcase) ? true : false end #output '<% '+str+' %>' def erb(str) ERB_START + str + ERB_END end #output '<%= '+str+' %> def erb_content(str) ERB_EVAL + str + ERB_END end #get tag_name def tag_name @directive_call_stack.context[:tag].tag_name end #set tag_name def tag_name=(tag_name) @directive_call_stack.context[:tag].tag_name = tag_name end #inside characters, cdata, or comment you can call this to get the characters passed def data @directive_call_stack.context[:content_part] end #set the data that will be passed to characters, cdata, or comment directives def data=(data) @directive_call_stack.context[:content_part]=data end # rolled up content from all children of the tag, note this will not be complete until hitting the end tag method :etag def content @directive_call_stack.context[:tag].content end #return rolled up content from all children as string, note this will not be complete until hitting the end tag method :etag def content_str content = @directive_call_stack.context[:tag].content content = content.join if content.respond_to? :join content end # replace the content from all children with a new value def content=(content) @directive_call_stack.context[:tag].content = content end #add single quotes around string def quote(str) '\''+str+'\'' end def remove_strings_from_attr_value! self.attr_value = remove_prepended_strings(attr_value) end #prepend string to attribute value adding a comma if attribute value was not empty def prepend_to_attr_value!(str) return attr_value if str.nil? || str.strip.empty? av = str av << ', ' << attr_value unless attr_value.strip.empty? self.attr_value = av end #append string to attribute value adding a comma if attribute value was not empty def append_to_attr_value!(str) return attr_value if str.nil? || str.strip.empty? av = attr_value av << ', ' unless av.strip.empty? av << str self.attr_value = av end #merge merge_hash into hashes stored in attribute_value string #hash_index is the zero based index of the hash you want to add to def merge_hash_attr_value!(hash_index, merge_hash) self.attr_value = merge_into_embedded_hash(attr_value, hash_index, merge_hash) end #calls non-evaling parse to split into string arguments def parse_attr_value parse(attr_value) end # check for common html options and return the hash def common_html_options(attrs_lck) options = {} options[:class] = attrs_lck['class'] if attrs_lck['class'] options[:style] = attrs_lck['style'] if attrs_lck['style'] options[:tabindex] = attrs_lck['tabindex'] if attrs_lck['tabindex'] options[:accesskey] = attrs_lck['accesskey'] if attrs_lck['accesskey'] options[:disabled] = true if attrs_lck['disabled'] options[:readonly] = true if attrs_lck['readonly'] options end end end