yore 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,177 +0,0 @@
1
- require 'net/http'
2
- require 'rexml/document'
3
-
4
- module XmlUtils
5
-
6
- BASIC_HEADER = '<?xml version="1.0"?>'
7
-
8
- def self.get_url_root(url)
9
- xml = Net::HTTP.get(URI(url))
10
- return nil if !xml
11
- xdoc = REXML::Document.new(xml)
12
- return nil if !xdoc
13
- root = xdoc.root
14
- end
15
-
16
- def self.get_xml_root(xml)
17
- xdoc = REXML::Document.new(xml)
18
- return nil if !xdoc
19
- root = xdoc.root
20
- end
21
-
22
- def self.get_file_root(aFilename)
23
- return nil unless File.exists?(aFilename)
24
- get_xml_root(MiscUtils.string_from_file(aFilename))
25
- end
26
-
27
- def self.single_node(node,xpath,default=nil)
28
- return default if node.nil? || xpath.nil? || xpath==''
29
- val = REXML::XPath.first(node,xpath)
30
- return val.nil? ? default : val
31
- end
32
-
33
- def self.peek_node(node,xpath,default=nil)
34
- return default if node.nil? || xpath.nil? || xpath==''
35
- val = REXML::XPath.first(node,xpath)
36
- return val.nil? ? default : val.to_s
37
- end
38
-
39
- def self.peek_node_value(aNode,aXPath,aDefault=nil)
40
- node = single_node(aNode,aXPath)
41
- return node.to_s if node.is_a?(REXML::Attribute)
42
- return node.nil? ? aDefault : node.text()
43
- end
44
-
45
- # convert <root><a>one</a><b>two</b></root> to {'a' => 'one', 'b' => 'two'}
46
- def self.hash_from_elements(aXMLRoot)
47
- result = {}
48
- aXMLRoot.elements.each{|e| result[e.name] = e.text}
49
- return result
50
- end
51
-
52
- # given '<tag a="1" b="2">textblah</tag>' returns {:name=>"tag", :text=>"textblah", "a"=>"1", "b"=>"2"}
53
- def self.hash_from_tag(aTagString)
54
- result = {}
55
- tag = get_xml_root(aTagString)
56
- return nil if !tag
57
- tag.attributes.each_attribute {|attr| result[attr.expanded_name] = attr.value }
58
- result[:name] = tag.name
59
- result[:text] = tag.text if tag.text
60
- return result
61
- end
62
-
63
- # given {:name=>"tag", :text=>"textblah", "a"=>"1", "b"=>"2"} returns '<tag a="1" b="2">textblah</tag>'
64
- def self.tag_from_hash(aHash,aName=nil,aText=nil)
65
- aName ||= aHash[:name]
66
- aText ||= aHash[:text]
67
- result = "<#{aName}"
68
- aHash.each {|k,v| result += " #{k}=\"#{encode(v.to_s)}\"" if k.is_a? String}
69
- result += aText ? " >#{aText}</#{aName}>" : " />"
70
- end
71
-
72
- def self.add_xml_from_string(aString,aNode)
73
- return nil unless xdoc = REXML::Document.new('<?xml version="1.0" encoding="UTF-8"?>'+aString)
74
- r = xdoc.root
75
- r.remove()
76
- r.parent = nil
77
- aNode.add_element(r)
78
- return r
79
- end
80
-
81
- def self.hash_to_xml(aHash,aRootName,aDocHeader=true)
82
- xdoc = REXML::Document.new(BASIC_HEADER)
83
- root = xdoc.add_element(aRootName)
84
- aHash.each do |n,v|
85
- root.add_element(n).add_text(v)
86
- end
87
- return xdoc
88
- end
89
-
90
- def self.read_simple_items(aRoot,aParentXPath=nil)
91
- result = {}
92
- xp = aParentXPath ? File.join(aParentXPath,'Item') : 'Item'
93
- REXML::XPath.each(aRoot, xp) do |item|
94
- result[item.attribute('Name').to_s] = item.text
95
- end
96
- return result
97
- end
98
-
99
- def self.quick_write_simple_items(aHash,aParent)
100
- return "<#{aParent} />\n" if !aHash || aHash.empty?
101
- result = "<#{aParent}>\n"
102
- aHash.each {|key,value| result += "\t<Item Name=\"#{key.to_s}\">#{value.to_s}</Item>\n" }
103
- result += "<#{aParent}/>\n"
104
- return result
105
- end
106
-
107
- # reads the simple items format given either a filename or xml node
108
- def self.read_config_values(aXmlConfig)
109
- xmlRoot = aXmlConfig.is_a?(REXML::Element) ? aXmlConfig : get_file_root(aXmlConfig)
110
- return read_simple_items(xmlRoot,'/Config/SimpleItems')
111
- end
112
-
113
- # Takes a node or xml string and writes it out formatted nicely.
114
- # aOutput may be given eg. a stream or nil can be given to get a returned string
115
- def self.format_nicely(aXml,aOutput=nil)
116
- aXml = REXML::Document.new(aXml) unless aXml.is_a?(REXML::Element)
117
- f = REXML::Formatters::Pretty.new(2,true)
118
- f.compact = true
119
- f.width = 120
120
- aOutput ||= ''
121
- f.write(aXml,aOutput)
122
- return aOutput
123
- end
124
-
125
- def self.encode(aString)
126
- result = aString.clone;
127
- result.gsub!('&','&amp;')
128
- result.gsub!('<','&lt;')
129
- result.gsub!('>','&gt;')
130
- result.gsub!('"','&quot;')
131
- result.gsub!("'",'&apos;')
132
- result.gsub!(/[\x80-\xFF]/) {|c| "&#x#{'%X' % c[0]};"}
133
- return result
134
- end
135
-
136
- def self.hash_to_deflist(aHash,aBuilder=nil)
137
- aBuilder ||= Builder::XmlMarkup.new(:indent => 2)
138
- aBuilder.dl do
139
- aHash.each do |k,v|
140
- aBuilder.dt(k.to_s)
141
- aBuilder.dd(v.to_s)
142
- end
143
- end
144
- end
145
-
146
- def self.data_to_table(aRowHashes,aCaption=nil,aColNames=nil,aBuilder=nil)
147
- aBuilder ||= Builder::XmlMarkup.new(:indent => 2)
148
- aBuilder.table do
149
- if aCaption.is_a? String
150
- aBuilder.caption(aCaption)
151
- elsif aCaption.is_a? Hash
152
- aBuilder.caption do
153
- XmlUtils.hash_to_deflist(aCaption,aBuilder)
154
- end
155
- end
156
- aColNames ||= aRowHashes.first.keys
157
- aBuilder.thead do
158
- aBuilder.tr do
159
- aColNames.each do |name|
160
- aBuilder.td(name.to_s)
161
- end
162
- end
163
- end
164
- aBuilder.tbody do
165
- aRowHashes.each do |row|
166
- aBuilder.tr do
167
- aColNames.each do |name|
168
- aBuilder.td(row[name].to_s)
169
- end
170
- end
171
- end
172
- end
173
- end
174
- end
175
-
176
- end
177
-
data/lib/yore.orig.rb DELETED
@@ -1,6 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__)) unless
2
- $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
-
4
- module Yore
5
- VERSION = '0.0.4'
6
- end
data/script/console DELETED
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # File: script/console
3
- irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
-
5
- libs = " -r irb/completion"
6
- # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
- # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
- libs << " -r #{File.dirname(__FILE__) + '/../lib/yore.rb'}"
9
- puts "Loading yore gem"
10
- exec "#{irb} #{libs} --simple-prompt"
data/script/destroy DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
- APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
-
4
- begin
5
- require 'rubigen'
6
- rescue LoadError
7
- require 'rubygems'
8
- require 'rubigen'
9
- end
10
- require 'rubigen/scripts/destroy'
11
-
12
- ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
- RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
- RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
- APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
-
4
- begin
5
- require 'rubigen'
6
- rescue LoadError
7
- require 'rubygems'
8
- require 'rubigen'
9
- end
10
- require 'rubigen/scripts/generate'
11
-
12
- ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
- RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
- RubiGen::Scripts::Generate.new.run(ARGV)