polyrex-objects 0.6.5 → 0.6.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/polyrex-objects.rb +101 -78
- metadata +4 -4
data/lib/polyrex-objects.rb
CHANGED
@@ -1,96 +1,119 @@
|
|
1
|
-
#!/usr/bin/ruby
|
1
|
+
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
# file: polyrex-objects.rb
|
4
4
|
|
5
|
-
require '
|
6
|
-
|
5
|
+
require 'requestor'
|
6
|
+
|
7
|
+
code = Requestor.read('http://rorbuilder.info/r/ruby/') do |x|
|
8
|
+
x.require 'polyrex-createobject'
|
9
|
+
x.require 'rexle'
|
10
|
+
end
|
11
|
+
|
12
|
+
eval code
|
13
|
+
|
7
14
|
|
8
15
|
class PolyrexObjects
|
16
|
+
|
17
|
+
class PolyrexObject
|
18
|
+
|
19
|
+
def initialize(node, id='0')
|
20
|
+
@@id = id
|
21
|
+
@node = node
|
22
|
+
end
|
23
|
+
|
24
|
+
def create(id=nil)
|
25
|
+
id ||= @@id
|
26
|
+
id.succ!
|
27
|
+
@create.id = id
|
28
|
+
|
29
|
+
@create.record = @node.element('records')
|
30
|
+
@create
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_xml(options={})
|
34
|
+
@node.xml(options)
|
35
|
+
end
|
36
|
+
|
37
|
+
def with()
|
38
|
+
yield(self)
|
39
|
+
end
|
40
|
+
end
|
9
41
|
|
10
|
-
def initialize(schema,
|
42
|
+
def initialize(schema, id='0', node=nil)
|
11
43
|
|
12
44
|
@node = node
|
13
45
|
@@id = id
|
14
|
-
|
15
|
-
a = schema.split('/')
|
16
|
-
a.shift
|
17
|
-
@class_names = []
|
18
|
-
|
19
|
-
a.each do |x|
|
20
|
-
name, raw_fields = x.split('[')
|
21
|
-
if raw_fields then
|
22
|
-
fields = raw_fields.chop.split(',').map &:strip
|
23
|
-
@class_names << name.capitalize
|
24
|
-
|
25
|
-
classx = []
|
26
|
-
classx << "class #{name.capitalize}"
|
27
|
-
classx << "def initialize(node, id=nil)"
|
28
|
-
classx << "@@id=id; @node = node;"
|
29
|
-
classx << "@create = PolyrexCreateObject.new('#{schema}')"
|
30
|
-
classx << "end"
|
31
|
-
fields.each do |field|
|
32
|
-
classx << "def #{field}; @node.element('summary/#{field}/text()'); end"
|
33
|
-
classx << "def #{field}=(text); @node.element('summary/#{field}').text = text; end"
|
34
|
-
end
|
35
|
-
classx << "def to_xml(options={}); @node.xml(options); end"
|
36
|
-
classx << "def with(); yield(self); end"
|
37
|
-
classx << "end"
|
38
46
|
|
39
|
-
|
40
|
-
|
41
|
-
|
47
|
+
if schema then
|
48
|
+
a = schema.split('/')
|
49
|
+
a.shift
|
50
|
+
@class_names = []
|
42
51
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
objects = @node.xpath('records/*').map {|record| #{@class_names[i]}.new(record)}
|
49
|
-
|
50
|
-
def objects.records=(node); @node = node; end
|
51
|
-
def objects.records(); @node; end
|
52
|
-
|
53
|
-
def objects.sort_by!(&element_blk)
|
54
|
-
a = @node.xpath('records/*').sort_by &element_blk
|
55
|
-
records = @node.xpath('records')
|
56
|
-
records.delete
|
57
|
-
records = Rexle::Element.new 'records'
|
58
|
-
a.each {|record| records.add record}
|
59
|
-
@node.add records
|
60
|
-
@node.xpath('records/*').map {|record| #{@class_names[i]}.new(record)}
|
61
|
-
end
|
52
|
+
a.each do |x|
|
53
|
+
name, raw_fields = x.split('[')
|
54
|
+
if raw_fields then
|
55
|
+
fields = raw_fields.chop.split(',').map &:strip
|
56
|
+
@class_names << name.capitalize
|
62
57
|
|
63
|
-
|
64
|
-
|
65
|
-
|
58
|
+
classx = []
|
59
|
+
classx << "class #{name.capitalize} < PolyrexObject"
|
60
|
+
classx << "def initialize(node=nil, id='0')"
|
61
|
+
classx << "super(node,id)"
|
62
|
+
classx << "@create = PolyrexCreateObject.new('#{schema}', @@id)"
|
63
|
+
classx << "end"
|
64
|
+
fields.each do |field|
|
65
|
+
classx << "def #{field}; @node.element('summary/#{field}/text()'); end"
|
66
|
+
classx << "def #{field}=(text); @node.element('summary/#{field}').text = text; end"
|
67
|
+
end
|
68
|
+
classx << "end"
|
66
69
|
|
67
|
-
|
68
|
-
@create.id = id || @@id
|
69
|
-
@@id.succ!
|
70
|
-
@create.record = @node.element('records')
|
71
|
-
@create
|
70
|
+
eval classx.join("\n")
|
72
71
|
end
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
72
|
+
end
|
73
|
+
|
74
|
+
# implement the child_object within each class object
|
75
|
+
@class_names[0..-2].reverse.each_with_index do |class_name, k|
|
76
|
+
i = @class_names.length - (k + 1)
|
77
|
+
eval "#{class_name}.class_eval {
|
78
|
+
def records()
|
79
|
+
objects = @node.xpath('records/*').map {|record| #{@class_names[i]}.new(record, @@id)}
|
80
|
+
|
81
|
+
def objects.records=(node); @node = node; end
|
82
|
+
def objects.records(); @node; end
|
83
|
+
|
84
|
+
def objects.sort_by!(&element_blk)
|
85
|
+
a = @node.xpath('records/*').sort_by &element_blk
|
86
|
+
records = @node.xpath('records')
|
87
|
+
records.delete
|
88
|
+
records = Rexle::Element.new 'records'
|
89
|
+
a.each {|record| records.add record}
|
90
|
+
@node.add records
|
91
|
+
@node.xpath('records/*').map {|record| #{@class_names[i]}.new(record)}
|
92
|
+
end
|
92
93
|
|
93
|
-
|
94
|
+
objects.records = @node
|
95
|
+
objects
|
96
|
+
end
|
97
|
+
|
98
|
+
alias #{@class_names[i].downcase} records
|
99
|
+
|
100
|
+
}"
|
101
|
+
end
|
102
|
+
|
103
|
+
@class_names[1..-1].each_with_index do |class_name, k|
|
104
|
+
eval "#{class_name}.class_eval {
|
105
|
+
def parent()
|
106
|
+
#{@class_names[k]}.new(@node.parent.parent, @@id)
|
107
|
+
end
|
108
|
+
}"
|
109
|
+
end
|
110
|
+
|
111
|
+
methodx = @class_names.map do |name|
|
112
|
+
%Q(def #{name.downcase}(); #{name}.new(@node, @@id); end)
|
113
|
+
end
|
114
|
+
|
115
|
+
self.instance_eval(methodx.join("\n"))
|
116
|
+
end
|
94
117
|
end
|
95
118
|
|
96
119
|
def to_a
|
metadata
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
name: polyrex-objects
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.6.
|
5
|
+
version: 0.6.7
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
|
7
|
+
authors:
|
8
|
+
- James Robertson
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-11-21 00:00:00 +00:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|