pauldix-sax-machine 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
data/lib/sax-machine.rb
CHANGED
@@ -29,6 +29,26 @@ module SAXMachine
|
|
29
29
|
attr_reader options[:as] unless instance_methods.include?(options[:as].to_s)
|
30
30
|
attr_writer options[:as] unless instance_methods.include?("#{options[:as]}=")
|
31
31
|
end
|
32
|
+
|
33
|
+
def columns
|
34
|
+
sax_config.top_level_elements
|
35
|
+
end
|
36
|
+
|
37
|
+
def column(sym)
|
38
|
+
columns.select{|c| c.column == sym}[0]
|
39
|
+
end
|
40
|
+
|
41
|
+
def data_class(sym)
|
42
|
+
column(sym).data_class
|
43
|
+
end
|
44
|
+
|
45
|
+
def required?(sym)
|
46
|
+
column(sym).required?
|
47
|
+
end
|
48
|
+
|
49
|
+
def column_names
|
50
|
+
columns.map{|e| e.column}
|
51
|
+
end
|
32
52
|
|
33
53
|
def elements(name, options = {})
|
34
54
|
options[:as] ||= name
|
@@ -2,7 +2,7 @@ module SAXMachine
|
|
2
2
|
class SAXConfig
|
3
3
|
|
4
4
|
class ElementConfig
|
5
|
-
attr_reader :name, :setter
|
5
|
+
attr_reader :name, :setter, :data_class
|
6
6
|
|
7
7
|
def initialize(name, options)
|
8
8
|
@name = name.to_s
|
@@ -28,6 +28,16 @@ module SAXMachine
|
|
28
28
|
else
|
29
29
|
@setter = "#{@as}="
|
30
30
|
end
|
31
|
+
@data_class = options[:class]
|
32
|
+
@required = options[:required]
|
33
|
+
end
|
34
|
+
|
35
|
+
def column
|
36
|
+
@as || @name.to_sym
|
37
|
+
end
|
38
|
+
|
39
|
+
def required?
|
40
|
+
@required
|
31
41
|
end
|
32
42
|
|
33
43
|
def value_from_attrs(attrs)
|
@@ -15,6 +15,10 @@ describe "SAXMachine" do
|
|
15
15
|
document.title = "Title"
|
16
16
|
document.title.should == "Title"
|
17
17
|
end
|
18
|
+
|
19
|
+
it "should allow introspection of the elements" do
|
20
|
+
@klass.column_names.should =~ [:title]
|
21
|
+
end
|
18
22
|
|
19
23
|
it "should not overwrite the setter if there is already one present" do
|
20
24
|
@klass = Class.new do
|
@@ -28,6 +32,28 @@ describe "SAXMachine" do
|
|
28
32
|
document.title = "Title"
|
29
33
|
document.title.should == "Title **"
|
30
34
|
end
|
35
|
+
describe "the class attribute" do
|
36
|
+
before(:each) do
|
37
|
+
@klass = Class.new do
|
38
|
+
include SAXMachine
|
39
|
+
element :date, :class => DateTime
|
40
|
+
end
|
41
|
+
@document = @klass.new
|
42
|
+
@document.date = DateTime.now.to_s
|
43
|
+
end
|
44
|
+
it "should be available" do
|
45
|
+
@klass.data_class(:date).should == DateTime
|
46
|
+
end
|
47
|
+
end
|
48
|
+
describe "the required attribute" do
|
49
|
+
it "should be available" do
|
50
|
+
@klass = Class.new do
|
51
|
+
include SAXMachine
|
52
|
+
element :date, :required => true
|
53
|
+
end
|
54
|
+
@klass.required?(:date).should be_true
|
55
|
+
end
|
56
|
+
end
|
31
57
|
|
32
58
|
it "should not overwrite the accessor when the element is not present" do
|
33
59
|
document = @klass.new
|