noodlecr 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/node.rb +127 -0
  3. data/lib/noodlecr.rb +19 -0
  4. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 94dbcfc7ce12495c2a7003057e3a0a188ee954f5
4
+ data.tar.gz: 7f4fb01994c7220123bf3ee79431731ecee17c63
5
+ SHA512:
6
+ metadata.gz: 426fe5bbbfd2bc3be02aacde5cdac53bffa6482e722f3c03ffe8b741c2d0490674c59d6eb734453fb9ac4761ba5460b4f0e5ff1eb048c58285ce12770ee9d5d9
7
+ data.tar.gz: ee53be93f1e4e9c7b29e64bfddcc78c37bb12488ff51f254af9f835bc62546f628fca27270da737c540dc4390c1b16b380ecb5bd9ae73b2d6472c8794a6cebba
@@ -0,0 +1,127 @@
1
+ module Noodle
2
+ class Node
3
+ attr_accessor :name, :content
4
+ attr_reader :parent, :childs
5
+ # for next & previous must be going to by the parent node
6
+
7
+ # you need to add name optionnaly you can set value for attributes node
8
+ def initialize(name_node, hash_node = nil)
9
+ @name = name_node
10
+ @values = hash_node || {}
11
+ @next = nil
12
+ @previous = nil
13
+ @parent = nil
14
+ @content = nil
15
+ @childs = []
16
+ end
17
+
18
+ # attr(key) where key is a string :
19
+ # getter method
20
+ # attr(key, value) key=string, value=string :
21
+ # setter method
22
+ # attr(key) key=Hash :
23
+ # setter method for all pair key/value in params value
24
+ def attr(key, value = nil)
25
+ if key.class == "Hash"
26
+ key.each {|keyt, val| @values[keyt] = val}
27
+ elsif value.nil?
28
+ return @values[key] if @values.has_key? key
29
+ else
30
+ @values[key] = value
31
+ end
32
+ puts "#{@values.inspect}"
33
+ return nil
34
+ end
35
+
36
+ # Return all values containt by node
37
+ def listAttr
38
+ data = []
39
+ @values.each {|key, val| data.push key}
40
+ return data
41
+ end
42
+
43
+ # Delete all attr values
44
+ def deleteAttr(key)
45
+ if @values.has_key? key
46
+ @values.delete(key)
47
+ return true
48
+ end
49
+ return false
50
+ end
51
+
52
+ # Set value parent, you need to pass a Noodle::Node class
53
+ def setParent(parent)
54
+ @parent = parent
55
+ end
56
+
57
+ # Create child same parametre that Noodle::Node and this node is adding to child
58
+ def newChild(name, hash_tab = nil)
59
+ nchild = Noodle::Node.new(name, hash_tab)
60
+ addChild(nchild)
61
+ return nchild
62
+ end
63
+
64
+ # Add Noodle::Node to childs' node
65
+ def addChild(child)
66
+ child.setParent(self)
67
+ @childs.push child
68
+ end
69
+
70
+ # jump to the next node if exist return nil if he doesn't exist
71
+ def next
72
+ return (@parent ? @parent.childNext(self) : nil)
73
+ end
74
+
75
+ # jump to the previous node if exist return nil if he doesn't exist
76
+ def previous
77
+ return (@parent ? @parent.childPrevious(self) : nil)
78
+ end
79
+
80
+ # Convert Object and all childs NOT parent in xml
81
+ # Set content, becarefull we can add childs && content set !
82
+ # Content are printing in to_xml if are not null
83
+ def to_xml
84
+ return "<#{@name}#{values_str} />" if content.nil? && @childs.empty?
85
+ data = "<#{@name}#{values_str}>"
86
+ if @content.nil?
87
+ @childs.each do |child|
88
+ data += child.to_xml
89
+ end
90
+ else
91
+ data += @content
92
+ end
93
+ data += "</#{@name}>"
94
+ end
95
+
96
+ private
97
+
98
+ def childNext(node_child)
99
+ return_child(iter_child(node_child) + 1)
100
+ end
101
+
102
+ def childPrevious(node_child)
103
+ return_child(iter_child(node_child) - 1)
104
+ end
105
+
106
+ def values_str
107
+ data = ""
108
+ @values.each do |key, val|
109
+ data += " #{key}=\"#{val}\""
110
+ end
111
+ return data
112
+ end
113
+
114
+ def iter_child(obj_node)
115
+ i = 0
116
+ while @childs.length > i
117
+ return i if @childs[i] = obj_node
118
+ i += 1
119
+ end
120
+ return i
121
+ end
122
+
123
+ def return_child(i)
124
+ return (i < @childs.length && i >= 0 ? @childs[i] : nil)
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,19 @@
1
+ require_relative 'node'
2
+
3
+ # Noodlecr inherit to Noodle::Node you need to read Noodle::Node for understand how we work !
4
+ class Noodlecr < Noodle::Node
5
+ def initialize
6
+ super "superfather"
7
+ @big_father = nil
8
+ end
9
+
10
+ # we override to_xml because Noodlecr is special node without name, Noodlecr is a big father !
11
+ # Becarefull : we return a string value !! Need to rename this method to_s ?
12
+ def to_xml(encoding = false)
13
+ data = "<?xml version=\"1.0\"#{" encoding=\"utf-8\"" if encoding}?>" # useless encoding ? i don't know said me
14
+ @childs.each do |child|
15
+ data += child.to_xml
16
+ end
17
+ return data
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: noodlecr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Clovis Kyndt @manawasp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: With noodlecr you can create easily and most cleanly xml answer
14
+ email: clovis.kyndt@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/node.rb
20
+ - lib/noodlecr.rb
21
+ homepage: https://github.com/Manawasp/noodlecr-gem
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.2.2
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: noodlecr
45
+ test_files: []