xforms 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/xforms/control.rb +15 -1
- data/lib/xforms/form.rb +9 -0
- data/lib/xforms/form_parser.rb +13 -1
- data/lib/xforms/version.rb +1 -1
- data/spec/data/xform1.xml +10 -1
- data/spec/form_spec.rb +30 -0
- metadata +9 -9
data/lib/xforms/control.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
1
3
|
module XForms
|
2
4
|
|
3
5
|
class ::Object
|
@@ -20,6 +22,10 @@ module XForms
|
|
20
22
|
def itext id
|
21
23
|
form.itext[I18n.locale.to_s][id] || ''
|
22
24
|
end
|
25
|
+
|
26
|
+
def now
|
27
|
+
Time.now.utc.iso8601
|
28
|
+
end
|
23
29
|
end
|
24
30
|
|
25
31
|
module Label
|
@@ -37,6 +43,7 @@ module XForms
|
|
37
43
|
attr_accessor :constraint
|
38
44
|
attr_accessor :constraint_message
|
39
45
|
attr_accessor :relevant
|
46
|
+
attr_accessor :type
|
40
47
|
include Label
|
41
48
|
|
42
49
|
def initialize(form)
|
@@ -45,11 +52,18 @@ module XForms
|
|
45
52
|
end
|
46
53
|
|
47
54
|
def value=(value)
|
55
|
+
value = Integer(value).to_s if type == 'int'
|
48
56
|
form.model_instance.xpath(binding).first.children = value
|
49
57
|
end
|
50
58
|
|
51
59
|
def value
|
52
|
-
form.model_instance.xpath(binding).first.inner_text
|
60
|
+
data = form.model_instance.xpath(binding).first.inner_text
|
61
|
+
return data unless type
|
62
|
+
|
63
|
+
case type
|
64
|
+
when 'int' then Integer(data)
|
65
|
+
else data
|
66
|
+
end
|
53
67
|
end
|
54
68
|
|
55
69
|
def readonly
|
data/lib/xforms/form.rb
CHANGED
@@ -4,10 +4,12 @@ module XForms
|
|
4
4
|
attr_accessor :itext
|
5
5
|
attr_accessor :controls
|
6
6
|
attr_accessor :namespaces
|
7
|
+
attr_accessor :calculates
|
7
8
|
|
8
9
|
def initialize
|
9
10
|
@controls = []
|
10
11
|
@namespaces = {}
|
12
|
+
@calculates = []
|
11
13
|
end
|
12
14
|
|
13
15
|
def self.parse_file(path)
|
@@ -19,5 +21,12 @@ module XForms
|
|
19
21
|
def self.parse(input)
|
20
22
|
FormParser.new(input).parse
|
21
23
|
end
|
24
|
+
|
25
|
+
def recalculate
|
26
|
+
calculates.each do |calc|
|
27
|
+
value = calc[:value].dyn_value
|
28
|
+
model_instance.xpath(calc[:binding]).first.children = value.to_s
|
29
|
+
end
|
30
|
+
end
|
22
31
|
end
|
23
32
|
end
|
data/lib/xforms/form_parser.rb
CHANGED
@@ -16,6 +16,7 @@ module XForms
|
|
16
16
|
parse_xforms_element
|
17
17
|
end
|
18
18
|
end
|
19
|
+
@form.recalculate
|
19
20
|
@form
|
20
21
|
end
|
21
22
|
|
@@ -47,8 +48,18 @@ module XForms
|
|
47
48
|
:readonly => @reader.attribute('readonly'),
|
48
49
|
:constraint => @reader.attribute('constraint'),
|
49
50
|
:constraint_message => @reader.attribute("#{jr_prefix}:constraintMsg"),
|
50
|
-
:relevant => @reader.attribute('relevant')
|
51
|
+
:relevant => @reader.attribute('relevant'),
|
52
|
+
:type => parse_type(@reader.attribute('type'))
|
51
53
|
}
|
54
|
+
|
55
|
+
if @reader.attribute('calculate')
|
56
|
+
@form.calculates << { :binding => @reader.attribute('nodeset'), :value => XPathRef.new(@form, @reader.attribute('calculate')) }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def parse_type type
|
61
|
+
return type if type.nil?
|
62
|
+
type.split(':').last
|
52
63
|
end
|
53
64
|
|
54
65
|
def jr_prefix
|
@@ -92,6 +103,7 @@ module XForms
|
|
92
103
|
control.constraint = binding[:constraint]
|
93
104
|
control.constraint_message = binding[:constraint_message]
|
94
105
|
control.relevant = XPathRef.new(@form, binding[:relevant]) if binding[:relevant]
|
106
|
+
control.type = binding[:type]
|
95
107
|
end
|
96
108
|
|
97
109
|
read_descendants do
|
data/lib/xforms/version.rb
CHANGED
data/spec/data/xform1.xml
CHANGED
@@ -1,17 +1,24 @@
|
|
1
1
|
<?xml version='1.0' encoding='utf-8'?>
|
2
|
-
<xform xmlns:xf="http://www.w3.org/2002/xforms" xmlns:x="http://tempuri.org/" xmlns:jr="http://openrosa.org/javarosa">
|
2
|
+
<xform xmlns:xf="http://www.w3.org/2002/xforms" xmlns:x="http://tempuri.org/" xmlns:jr="http://openrosa.org/javarosa" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
3
3
|
<xf:model>
|
4
4
|
<xf:instance>
|
5
5
|
<x:data>
|
6
6
|
<x:foo>123</x:foo>
|
7
7
|
<x:bar />
|
8
8
|
<x:baz />
|
9
|
+
<x:calc1 />
|
10
|
+
<x:calc2 />
|
11
|
+
<x:calc3 />
|
9
12
|
</x:data>
|
10
13
|
</xf:instance>
|
11
14
|
|
12
15
|
<xf:bind id="bind1" nodeset="x:data/x:baz" constraint=". < 10" jr:constraintMsg="Error Message" />
|
13
16
|
<xf:bind id="bind2" nodeset="x:data/x:foo" readonly="true()" />
|
14
17
|
<xf:bind id="bind3" nodeset="x:data/x:bar" relevant="/x:data/x:foo = 321" />
|
18
|
+
<xf:bind id="bind4" nodeset="x:data/x:foo" type="xsd:int" />
|
19
|
+
<xf:bind id="calc1" nodeset="x:data/x:calc1" calculate="/x:data/x:foo * 2" />
|
20
|
+
<xf:bind id="calc2" nodeset="x:data/x:calc2" calculate="/x:data/x:foo" />
|
21
|
+
<xf:bind id="calc3" nodeset="x:data/x:calc3" calculate="now()" />
|
15
22
|
</xf:model>
|
16
23
|
|
17
24
|
<xf:input ref="x:data/x:foo">
|
@@ -49,4 +56,6 @@
|
|
49
56
|
|
50
57
|
<xf:input bind="bind3" />
|
51
58
|
|
59
|
+
<xf:input bind="bind4" />
|
60
|
+
|
52
61
|
</xform>
|
data/spec/form_spec.rb
CHANGED
@@ -100,4 +100,34 @@ describe "Form" do
|
|
100
100
|
form.controls[0].should be_relevant
|
101
101
|
end
|
102
102
|
|
103
|
+
it "input type int" do
|
104
|
+
form.controls[6].type.should == 'int'
|
105
|
+
end
|
106
|
+
|
107
|
+
it "get input value with int type" do
|
108
|
+
form.controls[6].value.should == 123
|
109
|
+
end
|
110
|
+
|
111
|
+
it "set input value with int type" do
|
112
|
+
form.controls[6].value = '01'
|
113
|
+
model.xpath('x:data/x:foo').inner_text.should == '1'
|
114
|
+
end
|
115
|
+
|
116
|
+
it "set int value to integer input" do
|
117
|
+
form.controls[6].value = 1
|
118
|
+
model.xpath('x:data/x:foo').inner_text.should == '1'
|
119
|
+
end
|
120
|
+
|
121
|
+
it "execute2 calc1 binding" do
|
122
|
+
model.xpath('x:data/x:calc1').inner_text.should == '246.0'
|
123
|
+
end
|
124
|
+
|
125
|
+
it "executes calc2 binding" do
|
126
|
+
model.xpath('x:data/x:calc2').inner_text.should == '123'
|
127
|
+
end
|
128
|
+
|
129
|
+
it "executes calc3 binding with now function" do
|
130
|
+
model.xpath('x:data/x:calc3').inner_text.should == Time.now.utc.iso8601
|
131
|
+
end
|
132
|
+
|
103
133
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xforms
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-03-30 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
17
|
-
requirement: &
|
17
|
+
requirement: &70324868262480 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70324868262480
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: nokogiri
|
28
|
-
requirement: &
|
28
|
+
requirement: &70324868262040 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *70324868262040
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: i18n
|
39
|
-
requirement: &
|
39
|
+
requirement: &70324868261600 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,7 +44,7 @@ dependencies:
|
|
44
44
|
version: '0'
|
45
45
|
type: :runtime
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *70324868261600
|
48
48
|
description: This gem can be used to parse and fill XForms 1.1
|
49
49
|
email:
|
50
50
|
- jwajnerman@manas.com.ar
|
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
89
|
version: '0'
|
90
90
|
requirements: []
|
91
91
|
rubyforge_project: xforms
|
92
|
-
rubygems_version: 1.8.
|
92
|
+
rubygems_version: 1.8.11
|
93
93
|
signing_key:
|
94
94
|
specification_version: 3
|
95
95
|
summary: XForms 1.1 implementation
|