con_duxml 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f310021527cf21283951d133438156181a2937b2
4
- data.tar.gz: c7ac7d363ecd3451628a26b84cde8c139c9564fa
3
+ metadata.gz: 2f839d248fdc3df14fc70cce1075fb7b36c218ad
4
+ data.tar.gz: 3f6e928bfb479b302ea96f6acb6dd47e7460fee7
5
5
  SHA512:
6
- metadata.gz: 0f94edd9c559d4515270e928d5f9a4497be68710ae4cc9bdf2bef50f85932886029e2d6f4797fae261e85a905cc50075ab71aa106740c1f7542b1bdd9c921510
7
- data.tar.gz: 0e351f296e4538c421342af885062f6e2040a2b90f0b7063457b7eae9f25659938f392adbdb17af76e41d8c8ba2c911e325b775382f7f2074a6169d9eb0823df
6
+ metadata.gz: eea5385f086c3d75a5dd6afc6d9a34d3045e9e1adb50b66101a1b273c42bf42762ced1d3d908721246afcdb7e34b739adcd26a187b9d5a3ad1d3adccc0c621cb
7
+ data.tar.gz: 143de5258f6a664d9773f64398e48810e21ab988d5d988df632893ec36aa382d37e752e13901fd2473f166a08acea7696fd9197232666a8c99bb3a0b29427f4d
@@ -1,5 +1,5 @@
1
1
  # Copyright (c) 2016 Freescale Semiconductor Inc.
2
- %w(transform array instance link).each do |f| require_relative "con_duxml/#{f}" end
2
+ %w(transform instance link).each do |f| require_relative "con_duxml/#{f}" end
3
3
 
4
4
  module ConDuxml
5
5
  include Duxml
@@ -18,24 +18,14 @@ module ConDuxml
18
18
  # @return [Doc] result of transform; automatically saved to @doc
19
19
  def transform(transforms, doc_or_path, opts={})
20
20
  @doc = Doc.new
21
- transforms = case transforms
22
- when Doc then transforms.root
23
- when Element then transforms
24
- when String then sax(transforms).root
25
- else
26
- end
27
- @src_doc = case doc_or_path
28
- when Doc then doc_or_path
29
- when String then sax doc_or_path
30
- else
31
- end
21
+ transforms = get_doc(transforms).root
22
+ @src_doc = get_doc doc_or_path
32
23
  @src_ns = transforms[:src_ns]
33
24
  src_node = src_doc.locate(add_name_space_prefix(transforms[:source])).first
34
25
  doc.grammar = transforms[:grammar] if transforms[:grammar]
35
26
  doc.history.strict?(false) if opts[:strict].is_a?(FalseClass)
36
27
  add_observer doc.history
37
- a = activate(transforms.first, src_node).first
38
- @doc << a
28
+ @doc << activate(transforms.first, src_node).first
39
29
  end
40
30
 
41
31
  attr_reader :src_doc
@@ -43,15 +33,42 @@ module ConDuxml
43
33
  # instantiation takes a static design file and constructs a dynamic model by identifying certain keyword elements,
44
34
  # executing their method on child nodes, then removing the interpolating keyword element. these are:
45
35
  #
46
- # <array> - is replaced by multiple copies of its direct child or referenced XML nodes, allowing for user-defined variations between each copy
47
36
  # <instance> - when it contains design elements, it is removed but its reference ID is given to all children creating an XML-invisible arbitrary grouping
48
37
  # when it references an XML file, ID or path to an XML element, the target is copied and inserted in place of this element
49
38
  # <link> - referenced XML file or nodes provides namespace, contents, and notification of changes to any direct children of this node @see ConDuxml::Link
50
39
  #
51
- # @param source [String, Doc] XML document or path to one that will provide design content
40
+ # @param doc_or_node [String, Doc, Element] XML document or path to one or XMl Element
52
41
  # @return [Doc] resulting XML document
53
- def instantiate(source)
54
-
42
+ def instantiate(doc_or_node, opts={})
43
+ if doc_or_node.is_a?(Element)
44
+ new_node = doc_or_node.stub
45
+ new_children = doc_or_node.nodes.collect do |src_node|
46
+ if src_node.respond_to?(:nodes)
47
+ src_node.activate.collect do |inst|
48
+ inst.name.match(/con_duxml:/) ? instantiate(src_node) : instantiate(inst)
49
+ end
50
+ else
51
+ src_node.clone
52
+ end
53
+ end.flatten
54
+ if new_children.any?
55
+ new_node << new_children
56
+ end
57
+ new_node
58
+ else
59
+ @src_doc = get_doc doc_or_node
60
+ instance = instantiate(src_doc.root)
61
+ @doc = Doc.new << instance
62
+ end
55
63
  end
56
64
 
65
+ private
66
+ def get_doc(doc_or_path)
67
+ case doc_or_path
68
+ when Doc then doc_or_path
69
+ when String then sax doc_or_path
70
+ when Element then doc_or_path
71
+ else
72
+ end
73
+ end
57
74
  end
@@ -18,6 +18,21 @@ module Duxml
18
18
  new_nodes
19
19
  end
20
20
 
21
+ # @return [Array[Element]] instantiated copy of this Element
22
+ def activate
23
+ if name_space == 'duxml'
24
+ maudule = ConDuxml.const_get(simple_name.constantize)
25
+ extend maudule
26
+ activate
27
+ else
28
+ [clone]
29
+ end
30
+ end
31
+
32
+ def simple_name
33
+ name.split(':').last
34
+ end
35
+
21
36
  # @param pattern [several_variants] if String/Symbol or array of such, differences between merged entities' instance vars matching pattern are masked; if pattern is a hash, the key is the instance var, and the value becomes the new value for the merged entity
22
37
  # @param &block [block] groups nodes by &block then merges each group into a single row @see #chunk
23
38
  def merge(pattern=nil, &block)
@@ -5,29 +5,28 @@ module ConDuxml
5
5
  # Instances are copies of another XML element with a distinct set of parameter values
6
6
  # like Objects in relation to a Class
7
7
  module Instance
8
+ # @param target [String] path to target node or file
9
+ # @return [Element] self
8
10
  def ref=(target)
9
- raise Exception unless target.respond_to?(:nodes) or File.exists?(target)
10
- @ref = target
11
+ raise Exception unless doc.locate(target) or File.exists?(target)
12
+ self[:ref] = target
11
13
  end
12
14
 
13
- def resolve_ref(attr=nil)
14
- @ref ||= self[attr || :ref]
15
+ # @return [Element] either root node of referenced Doc or referenced node
16
+ def resolve_ref(attr='ref')
17
+ source = if self[:file]
18
+ path = File.expand_path(File.dirname(doc.path) + '/' + self[:file])
19
+ sax path
20
+ else
21
+ doc
22
+ end
23
+ return source.locate(self[attr]).first if self[attr]
24
+ source.root if self[:file]
15
25
  end
16
26
 
17
- # creates copy of referent (found from context given by 'meta') at this element's location
18
- def instantiate
19
- new_kids = []
20
- target = resolve_ref
21
- if target.nil?
22
- new_kids = nodes
23
- else
24
- new_kids << target.dclone
25
- end
26
- new_kids
27
+ # @return [Array[Element, String]] array (or NodeSet) of either shallow clone of child nodes or referenced nodes @see #ref=
28
+ def activate
29
+ [resolve_ref || nodes].flatten.clone
27
30
  end # def instantiate
28
31
  end # module Instance
29
-
30
- class InstanceClass
31
- include Instance
32
- end
33
32
  end # module Dux
@@ -23,7 +23,7 @@ module Private
23
23
  notify_observers(:Transform, xform, src, output)
24
24
  changed false
25
25
  output.add_observer doc.history if output.respond_to?(:add_observer)
26
- if output.is_a?(Element) and doc.history.strict?
26
+ if output.respond_to?(:nodes) and doc.history.strict?
27
27
  raise Exception, doc.history.latest.description unless doc.grammar.validate output
28
28
  end
29
29
  output
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.3.2
4
+ version: 0.4.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-23 00:00:00.000000000 Z
11
+ date: 2016-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: duxml
@@ -45,7 +45,6 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - lib/con_duxml/array.rb
49
48
  - lib/con_duxml/duxml_ext/element.rb
50
49
  - lib/con_duxml/duxml_ext/linkable.rb
51
50
  - lib/con_duxml/duxml_ext/transform_class.rb
@@ -1,42 +0,0 @@
1
- # Copyright (c) 2016 Freescale Semiconductor Inc.
2
-
3
- require File.expand_path(File.dirname(__FILE__) + '/instance')
4
-
5
- module ConDuxml
6
- # XML object array
7
- # represents a pattern of copies of a this object's children or referents
8
- # differentiates between copies using iterator Parameter
9
- module Array
10
- include Instance
11
- include Enumerable
12
-
13
- # @param block [block] each duplicated node is yielded to block if given
14
- # @return [Array[Element]] flattened array of all duplicated Elements
15
- def instantiate(&block)
16
- size_expr = size.respond_to?(:to_i) ? size.to_i : size.to_s
17
- if size_expr.is_a? Fixnum
18
- new_children = []
19
- size_expr.times do |index|
20
- nodes.each do |node|
21
- new_child = block_given? ? yield(node.dclone, index) : node.dclone
22
- new_children << new_child
23
- end
24
- end
25
- new_children
26
- else
27
- [self]
28
- end
29
- end # def instantiate
30
-
31
- # size can be Fixnum or a Parameter expression
32
- def size
33
- self[:size]
34
- end
35
-
36
- # overriding #each to only traverse children and return self on completion, not Enumerator
37
- def each &block
38
- @children.each &block
39
- self
40
- end
41
- end # class Array
42
- end # module Dux