con_duxml 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea6092d7c51dbf6ce598c68947b1ac8ceddb1cdf
4
- data.tar.gz: a0ad5b0bd5ed055000b4c5a363cc4e1219e94bce
3
+ metadata.gz: be72385bfaedf3c2c5d3420bf97e9477b9ffc143
4
+ data.tar.gz: 87c695867176251b5e9cbbe57345cffaa6d6ec01
5
5
  SHA512:
6
- metadata.gz: 616d12866cc2a18f5fa742958f0cc75711bc4bea6109b5db2aca8c5632814af0260a180a5ad8a24b45637610184bbbdc02cf73663c83da3ef5ad16fe4c8e49da
7
- data.tar.gz: 500f5e62594877ab62199185e85dce2e127bef44ad3dc1906d4b660f20d6fa00f57c403e8a4dcd985e0fa0beba40b65be93a23a97f1adab9b27e92fe9dad9974
6
+ metadata.gz: 5a91a034b74e087b809b5db6daede88759c97b6366516aaa911095bee2ced263c9d17f14fab33ef8a850cf01ac4dc7b9c558bd46ba9a987eca5d9c382526c7ce
7
+ data.tar.gz: 6cf35d65bdbb92876f1bf7ffd1ff0bd5a30ee542903e463b8c9f342b594c21cef0bf79ff859ca4d1f607080ce28e0ffca05a8dee6d5906f627869794bc7b65ce
@@ -0,0 +1,17 @@
1
+ require 'duxml/meta/history/change'
2
+
3
+ include Duxml
4
+ class TransformClass < ChangeClass
5
+ def initialize(xform, src, output)
6
+ @instructions = xform
7
+ @input = src
8
+ @output = output
9
+ end
10
+
11
+ attr_reader :instructions, :input, :output
12
+
13
+ # @return [String] verbal description of transform event
14
+ def description
15
+ "#{output.description} created from #{input.description} by #{instructions.description}"
16
+ end
17
+ end
@@ -0,0 +1,81 @@
1
+ # Copyright (c) 2016 Freescale Semiconductor Inc.
2
+
3
+ # Private is where methods should go that you DO NOT want invoked by a transform file!
4
+ module Private
5
+ private
6
+
7
+ # @param xform [Element] transform element
8
+ # @param _source [Element] XML xform containing content to be transformed
9
+ # @return [Array[Element]] transformed content; always an array of nodes, even if just one
10
+ def activate(xform, _source)
11
+ @source = _source
12
+ get_sources(xform).collect do |src|
13
+ args = get_args(xform, src)
14
+ output = get_method(xform).call(*args)
15
+ changed
16
+ m = methods
17
+ notify_observers(:Transform, xform, src, output)
18
+ changed false
19
+ output
20
+ end
21
+ end
22
+
23
+ # @param xform [Element] transform element
24
+ # @return [Array] array of elements that match @target, which must be a '/'-separated string
25
+ # if transform element has any children that may need the same source target, target_stack.last remains
26
+ # if transform is a leaf, target_stack is popped
27
+ def get_sources(xform)
28
+ if xform[:source]
29
+ source.locate add_name_space_prefix xform[:source]
30
+ else
31
+ [source]
32
+ end
33
+ end
34
+
35
+ # @param xform [Element] transform element
36
+ # @return [Method] resolves reference to actual transform method
37
+ def get_method(xform)
38
+ words = xform.name.split(':').reverse
39
+ method_name = words[0].to_sym
40
+ maudule = self
41
+ maudule = Module.const_get(words[1].constantize) if words[1] and Module.const_defined?(words[1].constantize)
42
+ if maudule == self
43
+ public_method(method_name)
44
+ else
45
+ maudule.public_instance_method(method_name).bind self
46
+ end
47
+ end
48
+
49
+ # @param xform [Element] transform element
50
+ # @param subj [Element] source XML Element
51
+ # @return [Array[String, Element]] string returned by self[:args] is separated by ';' into correctly formatted argument values for transform method
52
+ def get_args(xform, subj)
53
+ args = xform.attributes.keys.sort.collect do |attr|
54
+ if attr.to_s.match(/arg[0-9]*/)
55
+ if xform[attr].include?(',')
56
+ xform[attr].split(',').collect do |s|
57
+ s.match(/'[\s\w]+'/) ? $MATCH.strip[1..-2] : subj.locate(add_name_space_prefix s.strip).first
58
+ end
59
+ elsif xform[attr].include?('/')
60
+ subj.locate(add_name_space_prefix xform[attr]).first
61
+ elsif xform[attr].match(/'[\s\w]+'/)
62
+ $MATCH.strip[1..-2]
63
+ else
64
+ targets = subj.locate(xform[attr])
65
+ targets.empty? ? xform[attr] : targets
66
+ end
67
+ end
68
+ end.compact
69
+ children = xform.nodes.collect do |child|
70
+ activate(child, subj)
71
+ end
72
+ args << children.flatten if children.any?
73
+ args
74
+ end
75
+
76
+ def add_name_space_prefix(str)
77
+ str.split('/').collect do |w|
78
+ w.match(/\w+/) ? "#{src_ns ? src_ns+':' : ''}#{w}" : w
79
+ end.join('/')
80
+ end
81
+ end
@@ -1,74 +1,56 @@
1
1
  # Copyright (c) 2016 Freescale Semiconductor Inc.
2
+ require 'duxml'
3
+ require_relative 'duxml_ext/transform_class'
4
+ require_relative 'transform/transform_private'
5
+ include Duxml
2
6
 
3
- # helper methods for converting transform elements into code
7
+ # All public methods can be invoked by a transform element; please hide methods you don't want users to invoke in Private
4
8
  module Transform
5
- @xform
9
+ include Observable
10
+ include Private
11
+
12
+ # most recent XML node from which content has been taken; for constructing relative paths
6
13
  @source
7
14
 
8
- attr_reader :xform, :source
15
+ attr_reader :source
9
16
 
10
- # @param xform [Element] transform element
11
- # @param _source [Element] XML xform containing content to be transformed
12
- # @return [Array[Element]] transformed content; always an array of nodes, even if just one
13
- def activate(xform, _source)
14
- @source = _source
15
- get_sources(xform).collect do |src|
16
- args = get_args(xform, src)
17
- get_method(xform).call(*args)
18
- end
17
+ # @param node [Element] XML node from transform output
18
+ # @return [TransformClass] transform event object from output Doc's history
19
+ def find_xform_event(node)
20
+ @output.history #TODO find transform whose outputs include given node
19
21
  end
20
22
 
21
- # @param xform [Element] transform element
22
- # @return [Array] array of elements that match @target, which must be a '/'-separated string
23
- # if transform element has any children that may need the same source target, target_stack.last remains
24
- # if transform is a leaf, target_stack is popped
25
- def get_sources(xform)
26
- if xform[:source]
27
- source.locate add_name_space_prefix xform[:source]
28
- else
29
- [source]
30
- end
23
+ # @param node [Element] XML node from transform output
24
+ # @return [Element] XML node that contains instructions for transform used to create given @param node
25
+ def find_transform(node)
26
+ find_xform_event(node).instructions
31
27
  end
32
28
 
33
- # @param xform [Element] transform element
34
- # @return [Method] resolves reference to actual transform method
35
- def get_method(xform)
36
- words = xform.name.split(':').reverse
37
- method_name = words[0].to_sym
38
- maudule = Module
39
- maudule = Module.const_get(words[1].constantize) if words[1] and Module.const_defined?(words[1].constantize)
40
- maudule.method(method_name)
29
+ # @param node [Element] XML node from transform output
30
+ # @return [Element] XML node that provided content for transformation i.e. source
31
+ def find_source(node)
32
+ find_xform_event(node).input
41
33
  end
42
34
 
43
- # @param xform [Element] transform element
44
- # @param subj [Element] source XML Element
45
- # @return [Array[String, Element]] string returned by self[:args] is separated by ';' into correctly formatted argument values for transform method
46
- def get_args(xform, subj)
47
- args = xform.attributes.keys.sort.collect do |attr|
48
- if attr.to_s.match(/arg[0-9]/)
49
- if xform[attr].include?(',')
50
- xform[attr].split(',').collect do |s|
51
- if s.match(/'[\s\w]+'/)
52
- $MATCH.strip[1..-2]
53
- else
54
- subj.locate(add_name_space_prefix s.strip).first
55
- end
56
- end
57
- else
58
- subj.locate(add_name_space_prefix xform[attr]).first
59
- end
60
- end
61
- end.compact
62
- children = xform.nodes.collect do |child|
63
- activate(child, subj)
64
- end
65
- args << children.flatten if children.any?
66
- args
35
+ # @param path [String] path to node from @source e.g. 'child/grandchild'; target is expected to return ONE child
36
+ # containing TEXT only
37
+ # @return [String] content of target node
38
+ def content(path)
39
+ @source.locate(add_name_space_prefix path).first.text
67
40
  end
68
41
 
69
- def add_name_space_prefix(str)
70
- str.split('/').collect do |w|
71
- w.match(/\w+/) ? "#{src_ns ? src_ns+':' : ''}#{w}" : w
72
- end.join('/')
42
+ # @param *args [*several_variants] see Duxml::Element#new; the only difference here is that this method has access to the content source
43
+ # so the arguments can pass in the needed data
44
+ # @return [Element] new element to replace old one
45
+ def element(*args)
46
+ Element.new(*args)
47
+ end
48
+
49
+ # @param path [String] path to node from @source
50
+ # @return [Element] a deep copy of the target(s)
51
+ def copy(path)
52
+ @source.locate(add_name_space_prefix path).collect do |node|
53
+ node.dclone
54
+ end
73
55
  end
74
56
  end
data/lib/con_duxml.rb CHANGED
@@ -5,10 +5,6 @@ module ConDuxml
5
5
  include Duxml
6
6
  include Transform
7
7
 
8
- # hash where each key is concatenation of each instantiation of @doc and values are the Doc instance permutations returned by #instantiate
9
- @instances
10
- # double-key hash containing every result of #transform; keys concatenation of source doc's #object_id and transform doc's #object_id
11
- @transformations
12
8
  # namespace prefix for source file
13
9
  @src_ns
14
10
 
@@ -19,7 +15,7 @@ module ConDuxml
19
15
  # also contain directives or links to them
20
16
  # @return [Doc] result of transform; automatically hashed into @transforms
21
17
  def transform(transforms, doc_or_path=nil)
22
- output = Doc.new
18
+ @output = Doc.new
23
19
  transforms = case transforms
24
20
  when Doc then transforms.root
25
21
  when Element then transforms
@@ -33,11 +29,10 @@ module ConDuxml
33
29
  end
34
30
  @src_ns = transforms[:src_ns]
35
31
  source = doc.locate(add_name_space_prefix(transforms[:source])).first
36
- output.grammar = transforms[:grammar] if transforms[:grammar]
32
+ @output.grammar = transforms[:grammar] if transforms[:grammar]
37
33
  a = activate(transforms.first, source).first
38
- output << a
39
- @transformations ||= {}
40
- @transformations[doc.object_id+transforms.object_id] = output
34
+ add_observer @output.history
35
+ @output << a
41
36
  end
42
37
 
43
38
  # instantiation takes a static design file and constructs a dynamic model by identifying certain keyword elements,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: con_duxml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Kong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-21 00:00:00.000000000 Z
11
+ date: 2016-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: duxml
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.2'
33
+ version: '0.4'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.2'
40
+ version: '0.4'
41
41
  description:
42
42
  email:
43
43
  - peter.kong@nxp.com
@@ -48,8 +48,10 @@ files:
48
48
  - lib/con_duxml/array.rb
49
49
  - lib/con_duxml/duxml_ext/element.rb
50
50
  - lib/con_duxml/duxml_ext/linkable.rb
51
+ - lib/con_duxml/duxml_ext/transform_class.rb
51
52
  - lib/con_duxml/instance.rb
52
53
  - lib/con_duxml/link.rb
54
+ - lib/con_duxml/transform/transform_private.rb
53
55
  - lib/con_duxml/transform.rb
54
56
  - lib/con_duxml.rb
55
57
  homepage: http://www.github.com/Ludocracy/con_duxml