Module: Private
- Includes:
- Observable
- Included in:
- Transform
- Defined in:
- lib/con_duxml/transform/transform_private.rb
Overview
Private is where methods should go that you DO NOT want invoked by a transform file!
Instance Method Summary collapse
-
#activate(xform, src) ⇒ Array[Element]
Transformed content; always an array of nodes, even if just one.
- #add_name_space_prefix(str) ⇒ Object
-
#get_args(xform, src) ⇒ Array[String, Element]
String returned by self is separated by ‘;’ into correctly formatted argument values for transform method.
-
#get_method(xform) ⇒ Method
Resolves reference to actual transform method.
-
#get_sources(xform) ⇒ Array
Array of elements that match @target, which must be a ‘/’-separated string if transform element has any children that may need the same source target, target_stack.last remains if transform is a leaf, target_stack is popped.
- #normalize_arg(str, src) ⇒ Object
-
#separate_enumerables(str, src) ⇒ Array[String]
Returns separated values as array of strings.
Instance Method Details
#activate(xform, src) ⇒ Array[Element]
Returns transformed content; always an array of nodes, even if just one.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/con_duxml/transform/transform_private.rb', line 11 def activate(xform, src) @source = src get_sources(xform).collect do |src| args = get_args(xform, src) meth = get_method(xform) a = meth.arity if a == -1 or args.size == a or args.size.between?(-1 - a, 0 - a) output = meth.call(*args) else output = '' end changed notify_observers(:Transform, xform, src, output) changed false output.add_observer doc.history if output.respond_to?(:add_observer) if output.respond_to?(:nodes) and doc.history.strict? raise Exception, doc.history.latest.description unless doc.grammar.validate output end output end end |
#add_name_space_prefix(str) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/con_duxml/transform/transform_private.rb', line 113 def add_name_space_prefix(str) str str.split('/').collect do |w| w.match(/\w+/) ? "#{src_ns ? src_ns+':' : ''}#{w}" : w end.join('/') end |
#get_args(xform, src) ⇒ Array[String, Element]
Returns string returned by self is separated by ‘;’ into correctly formatted argument values for transform method.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/con_duxml/transform/transform_private.rb', line 62 def get_args(xform, src) args = xform.attributes.keys.sort.collect do |attr| if attr.to_s.match(/arg[0-9]*/) arg_str = xform[attr].strip case arg_str when /,/ then separate_enumerables(arg_str, src) when /^([\w]+): (\S.*)$/, /^([\S]+) => (\S.*)$/ then {$1.to_sym => normalize_arg($2, src)} when /^'(.+)' => (.+)$/ then {$1 => normalize_arg($2, src)} when /\// then src.locate(add_name_space_prefix arg_str).first when /^'([\s\w]+)'$/ then $1 else # arg is path to node target = src.locate(add_name_space_prefix arg_str).first target or '' end end end.compact children = xform.nodes.collect do |child| activate(child, src) end args << children.flatten if children.any? args end |
#get_method(xform) ⇒ Method
Returns resolves reference to actual transform method.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/con_duxml/transform/transform_private.rb', line 47 def get_method(xform) words = xform.name.split(':').reverse method_name = words[0].to_sym maudule = self maudule = Module.const_get(words[1].constantize) if words[1] and Module.const_defined?(words[1].constantize) if maudule == self public_method(method_name) else maudule.public_instance_method(method_name).bind self end end |
#get_sources(xform) ⇒ Array
Returns array of elements that match @target, which must be a ‘/’-separated string if transform element has any children that may need the same source target, target_stack.last remains if transform is a leaf, target_stack is popped.
37 38 39 40 41 42 43 |
# File 'lib/con_duxml/transform/transform_private.rb', line 37 def get_sources(xform) if xform[:source] source.locate add_name_space_prefix xform[:source] else [source] end end |
#normalize_arg(str, src) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/con_duxml/transform/transform_private.rb', line 85 def normalize_arg(str, src) case str when /'.+'/ then str[1..-2] when /^[0-9]$/ then str.to_i when 'true' then true when 'false' then false else src.locate(add_name_space_prefix str).first end end |
#separate_enumerables(str, src) ⇒ Array[String]
Returns separated values as array of strings
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/con_duxml/transform/transform_private.rb', line 97 def separate_enumerables(str, src) words = str.split(',').collect do |w| w.strip end h = {} a = [] words.each do |s| case s when /^'([\s\w]+)'$/ then a << $1 when /^(\w+): (\S.*)$/, /^(\w+) => (\S.*)$/ then h[$1.to_sym] = normalize_arg($2, src) when /^'(.+)' => (\S.*)$/ then h[$1] = normalize_arg($2, src) else a << normalize_arg(s, src) end end a.empty? ? h : a end |