Class: MasterView::DirectiveBaseOld

Inherits:
Object
  • Object
show all
Includes:
DirectiveHelpers, DirectiveMetadata, PluginLoadTracking
Defined in:
lib/masterview/deprecated/directive_base.rb

Overview

THIS CLASS IS DEPRECATED!!! UPGRADE DIRECTIVES TO USE NEW DirectiveBase

Base class for directive implementations.

The standard technique for implementing a directive is to subclass DirectiveBase. The builtin MasterView directives are implemented in module namespace MasterView::Directives.

If you create a directive implementation class elsewhere in the class hierarchy, it is recommended that you include the DirectiveHelpers mixin. If you do not include the PluginLoadTracking mixin, you will need to manually register your directive class using the MasterView.register_directive service. either the PluginLoadTracking mixin or

– TODO: add docs here on responsibilities and techniques for implementing directives. Subclass DirectiveBase, define within MasterView::Directives module namespace.

mumble - class methods:

attr_name is the directive's attribute markup name - def. is class name
full_attr_name(ns) is the qualified name (name-space prefixed)

Directive can optionally implement class method :on_load to initialize itself prior to processed for the directive registry

TODO: establish protocol convention for additional namespaces

mumble: :global_directive? predicate indicates… what?? (inline erb)

Directives implement priority to control the processing order when multiple directive attributes are used on a template document element.

TODO: document the priority hierarchy and convention for level usage

Discuss operational context: describe how/when MasterView parser invokes a directive handler in the course of parsing the elements and attributes of a template document node hierarchy. Notion of directive call stack; what state is the world in and information is available to a directive when invoked; what services are available in order to produce some effect on the results of the template parse.

Two general flavors on content directives: those which operate on attribute values of the containing element and those which operate on the entire containing element, sometimes by supplying or modifying its content, in other cases by replacing the template element with something else.

Third flavor: eval-only directive. Expression which is evaluate for its effect on the directive processing context or the overall state of processing the template document element tree.

Directive implementation typically wants to implement either or both of the methods stag(dcs) and etag(dcs) to hook up its processing on the start/end tags of the element on which the attribute is defined.

When a directive attribute is used on a template document element, the directive class is instantiated with the attribute_value provided to its constructure. All directives used on an element are sorted into processing order according to their priority (default is Medium. The directive processor is invoked when the element start tag is encountered and when the element end tag is completed, allowing the implementation to control when and how its processing is hooked up to effect the template output. ++

Constant Summary

Constants included from DirectiveHelpers

MasterView::DirectiveHelpers::CRLF, MasterView::DirectiveHelpers::ERB_CONTENT_END, MasterView::DirectiveHelpers::ERB_CONTENT_START, MasterView::DirectiveHelpers::ERB_EVAL_END, MasterView::DirectiveHelpers::ERB_EVAL_START

Constants included from DirectiveMetadata

MasterView::DirectiveMetadata::DEBUG_MD_INSTALLATION, MasterView::DirectiveMetadata::DEBUG_MD_PROPS, MasterView::DirectiveMetadata::DOC_PROPERTY_NAMES, MasterView::DirectiveMetadata::DirectiveMetadataRegistry, MasterView::DirectiveMetadata::HARDENED_PROPERTY_NAMES, MasterView::DirectiveMetadata::MARKUP_PROPERTY_NAMES, MasterView::DirectiveMetadata::PROCESSING_PROPERTY_NAMES, MasterView::DirectiveMetadata::PrioritiesMap, MasterView::DirectiveMetadata::PriorityNames, MasterView::DirectiveMetadata::STANDARD_PROPERTY_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DirectiveHelpers

#delete_last_in_parent, #find_last_in_parent, #find_string_val_in_string_hash, #parse, #quote, #quote_if, #render_partial_name_to_file_name

Methods included from DirectiveMetadata

#attribute_name, #attribute_qname, get_priority_label, get_priority_name, included, #namespace, #namespace_name, #namespace_prefix, #priority, validate_metadata_props!, validate_xml_identifier

Methods included from PluginLoadTracking

included

Constructor Details

#initialize(attribute_value) ⇒ DirectiveBaseOld

Construct a directive processor for the attribute_value of a directive attribute on a document template element.



134
135
136
# File 'lib/masterview/deprecated/directive_base.rb', line 134

def initialize(attribute_value)
  @attribute_value = attribute_value
end

Class Method Details

.attr_nameObject

DEPRECATED- superseded by DirectiveMetadata attribute_name

Returns the attribute name of the directive.

Use full_attr_name to obtain the fully-qualified name of the directive attribute with the qualifying namespace prefix.

The default attribute name of a directive is formed from the simple class name (without any module prefix qualifier), with the first character downcased.



122
123
124
# File 'lib/masterview/deprecated/directive_base.rb', line 122

def self.attr_name()
  self.default_attr_name()
end

.default_attr_nameObject

DEPRECATED- superseded by DirectiveMetadata default_directive_name



127
128
129
# File 'lib/masterview/deprecated/directive_base.rb', line 127

def self.default_attr_name()  #:nodoc:
  self.name.split(':').last.downcase_first_letter
end

.full_attr_name(namespace_prefix) ⇒ Object

DEPRECATED- superseded by DirectiveMetadata attribute_qname

Returns the fully qualified attribute name of the directive, consisting of the directive namespace and the directive attribute name.

The default MasterView namespace_prefix is used if the directive does not specify a separate namespace.

– TODO: clarify the mechanism by which alternate namespaces are defined. Is this done by a code value or configured as part of the directory path specifications, or some combination thereof to allow ovverides in the event of namespace collisions? ++



106
107
108
109
# File 'lib/masterview/deprecated/directive_base.rb', line 106

def self.full_attr_name( namespace_prefix )
  #TODO: fix this so that directives can override to define their own namespace separate from mv:
  namespace_prefix + self.attr_name
end

.register_directive(directive_class) ⇒ Object

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.



84
85
86
87
88
89
# File 'lib/masterview/deprecated/directive_base.rb', line 84

def self.register_directive(directive_class)
    #ISSUE: do we really need both PluginLoadTracking.register_class
    #and DirectiveBase.register_directive, in addition to MasterView.register_directive???
    #[DJL 04-Jul-2006]
  MasterView.register_directive(directive_class)
end

Instance Method Details

#append_to_attr_value!(str) ⇒ Object

append string to attribute value adding a comma if attribute value was not empty



255
256
257
258
259
260
261
# File 'lib/masterview/deprecated/directive_base.rb', line 255

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

#attr_lckv_matches(lckey, lcmatch) ⇒ Object

returns true if the value for lckey of the attribute hash with lowercased keys and values matches (lowercase) lcmatch string



175
176
177
# File 'lib/masterview/deprecated/directive_base.rb', line 175

def attr_lckv_matches(lckey, lcmatch)
  (attrs_lckv[lckey] && attrs_lckv[lckey] == lcmatch.downcase) ? true : false
end

#attr_valueObject

Returns the directive’s attribute value string being processed.



144
145
146
# File 'lib/masterview/deprecated/directive_base.rb', line 144

def attr_value
  @attribute_value
end

#attr_value=(attribute_value) ⇒ Object

Set the directive’s attribute value string to be processed.



149
150
151
# File 'lib/masterview/deprecated/directive_base.rb', line 149

def attr_value=(attribute_value)
  @attribute_value = attribute_value
end

#attrsObject

get attribute hash from tag



154
155
156
# File 'lib/masterview/deprecated/directive_base.rb', line 154

def attrs
  @directive_call_stack.context[:tag].attributes
end

#attrs=(attributes) ⇒ Object

set attribute hash for tag



159
160
161
# File 'lib/masterview/deprecated/directive_base.rb', line 159

def attrs=(attributes)
  @directive_call_stack.context[:tag].attributes = attributes
end

#attrs_lckObject

get attribute hash with lowercased keys (and original values) and cache it



169
170
171
# File 'lib/masterview/deprecated/directive_base.rb', line 169

def attrs_lck
  @attrs_lck ||= lowercase_attribute_keys(attrs)
end

#attrs_lckvObject

get attribute hash with lowercased keys and values, and cache it



164
165
166
# File 'lib/masterview/deprecated/directive_base.rb', line 164

def attrs_lckv
  @attrs_lckv ||= lowercase_attribute_keys_and_values(attrs)
end

#common_html_options(attrs_lck) ⇒ Object

check for common html options and return the hash



275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/masterview/deprecated/directive_base.rb', line 275

def common_html_options(attrs_lck)
  ####CHANGED: new DirectiveBase provides more extensive list
  options = {}
  options[:id] = attrs_lck['id'] if attrs_lck['id']
  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

#contentObject

rolled up content from all children of the tag, note this will not be complete until hitting the end tag method :etag



226
227
228
# File 'lib/masterview/deprecated/directive_base.rb', line 226

def content
  @directive_call_stack.context[:tag].content
end

#content=(content) ⇒ Object

replace the content from all children with a new value



238
239
240
# File 'lib/masterview/deprecated/directive_base.rb', line 238

def content=(content)
  @directive_call_stack.context[:tag].content = content
end

#content_strObject

return rolled up content from all children as string, note this will not be complete until hitting the end tag method :etag



231
232
233
234
235
# File 'lib/masterview/deprecated/directive_base.rb', line 231

def content_str
  content = @directive_call_stack.context[:tag].content
  content = content.join if content.respond_to? :join
  content
end

#dataObject

inside characters, cdata, or comment you can call this to get the characters passed



216
217
218
# File 'lib/masterview/deprecated/directive_base.rb', line 216

def data
  @directive_call_stack.context[:content_part]
end

#data=(data) ⇒ Object

set the data that will be passed to characters, cdata, or comment directives



221
222
223
# File 'lib/masterview/deprecated/directive_base.rb', line 221

def data=(data)
  @directive_call_stack.context[:content_part]=data
end

#erb(str) ⇒ Object

DEPRECATED - going away output ‘<% ’str‘ %>’



181
182
183
184
# File 'lib/masterview/deprecated/directive_base.rb', line 181

def erb(str) #:nodoc:
  #ISSUE: convert clients to erb_eval and drop.  Ya oughta have a point of view. [DJL 04-Jul-2006]
  ERB_EVAL_START + str + ERB_EVAL_END
end

#erb_content(str) ⇒ Object

Compose an Erb expression which produces content in the containing document. The expression may also have effects on the processing context of the template document

output ‘<%= ’str‘ %>



192
193
194
# File 'lib/masterview/deprecated/directive_base.rb', line 192

def erb_content(str)
  ERB_CONTENT_START + str + ERB_CONTENT_END
end

#erb_eval(str) ⇒ Object

Compose an Erb expression which is evaluated for its effect on the processing context but does not produce content in the containing document.

output ‘<% ’str‘ %>’



201
202
203
# File 'lib/masterview/deprecated/directive_base.rb', line 201

def erb_eval(str)
  ERB_EVAL_START + str + ERB_EVAL_END
end

#lowercase_attribute_keys(attributes) ⇒ Object

DEPRECATED return attributes with lowercase keys



334
335
336
337
338
# File 'lib/masterview/deprecated/directive_base.rb', line 334

def lowercase_attribute_keys(attributes)
  lcattrs = {}
  attributes.each { |k,v| lcattrs[k.downcase] = v }
  lcattrs
end

#lowercase_attribute_keys_and_values(attributes) ⇒ Object

DEPRECATED return attributes with lowercase keys and values



342
343
344
345
346
# File 'lib/masterview/deprecated/directive_base.rb', line 342

def lowercase_attribute_keys_and_values(attributes)
  lcattrs = {}
  attributes.each { |k,v| lcattrs[k.downcase] = v.downcase }
  lcattrs
end

#merge_hash_attr_value!(hash_index, merge_hash) ⇒ Object

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



265
266
267
# File 'lib/masterview/deprecated/directive_base.rb', line 265

def merge_hash_attr_value!(hash_index, merge_hash)
  self.attr_value = merge_into_embedded_hash(attr_value, hash_index, merge_hash)
end

#merge_into_embedded_hash(full_string, hash_arg, hash_to_merge) ⇒ Object

DEPRECATED 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.



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/masterview/deprecated/directive_base.rb', line 302

def merge_into_embedded_hash(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[0].to_s <=> b[0].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_attr_valueObject

calls non-evaling parse to split into string arguments



270
271
272
# File 'lib/masterview/deprecated/directive_base.rb', line 270

def parse_attr_value
  parse(attr_value)
end

#prepend_to_attr_value!(str) ⇒ Object

prepend string to attribute value adding a comma if attribute value was not empty



247
248
249
250
251
252
# File 'lib/masterview/deprecated/directive_base.rb', line 247

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

#remove_prepended_strings(full_string) ⇒ Object

deprecated methods from DirectiveHelpers DEPRECATED 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



293
294
295
296
297
# File 'lib/masterview/deprecated/directive_base.rb', line 293

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

#remove_strings_from_attr_value!Object



242
243
244
# File 'lib/masterview/deprecated/directive_base.rb', line 242

def remove_strings_from_attr_value!
  self.attr_value = remove_prepended_strings(attr_value)
end

#save_directive_call_stack(directive_call_stack) ⇒ Object

if this method exists, it will be called by renderer to save directive_call_stack before each method call



139
140
141
# File 'lib/masterview/deprecated/directive_base.rb', line 139

def save_directive_call_stack(directive_call_stack)
  @directive_call_stack = directive_call_stack
end

#symbolize_sort_and_serialize_hash_to_str(hash) ⇒ Object

DEPRECATED using hash, symbolize keys, sort keys, serialize to string



350
351
352
353
354
355
356
# File 'lib/masterview/deprecated/directive_base.rb', line 350

def symbolize_sort_and_serialize_hash_to_str(hash)
  symbolized = {}
  hash.each{ |k,v| symbolized[k.to_sym] = v } #symbolize
  sorted = symbolized.sort{ |a,b| a[0].to_s <=> b[0].to_s } # sort the keys
  sorted_strings = sorted.collect{ |k,v| "#{k.inspect} => '#{v}'"} # create strings
  sorted_strings.join(', ') # finally combine them
end

#tag_nameObject

get tag_name



206
207
208
# File 'lib/masterview/deprecated/directive_base.rb', line 206

def tag_name
  @directive_call_stack.context[:tag].tag_name
end

#tag_name=(tag_name) ⇒ Object

set tag_name



211
212
213
# File 'lib/masterview/deprecated/directive_base.rb', line 211

def tag_name=(tag_name)
  @directive_call_stack.context[:tag].tag_name = tag_name
end