match_point 0.0.9 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  module MatchPoint
2
- VERSION = '0.0.9'
2
+ VERSION = '0.0.10'
3
3
  end
@@ -3,13 +3,28 @@ module MatchPoint
3
3
 
4
4
  SPEC_NODE="MatchPointSpec"
5
5
 
6
- def initialize template, xml, data = {}
6
+ def self.from_xml template, xml
7
+ instance = self.new template
8
+ instance.xml= xml
9
+ instance
10
+ end
11
+
12
+ def self.from_hash template, data
13
+ instance = self.new template
14
+ instance.assign_values data
15
+ instance
16
+ end
17
+
18
+ def initialize template
7
19
  @template= Nokogiri::XML(template)
8
- @xml = Nokogiri::XML(xml)
20
+ @xml = Nokogiri::XML(template)
9
21
  @nodes_paths = {}
10
22
 
11
23
  synchronize_object_to_xml
12
- assign_values data
24
+ end
25
+
26
+ def xml= an_xml
27
+ @xml = Nokogiri::XML(an_xml)
13
28
  end
14
29
 
15
30
  def to_xml
@@ -36,48 +51,52 @@ module MatchPoint
36
51
  @xml.at_xpath a_path
37
52
  end
38
53
 
39
- protected
40
-
41
54
  def assign_values data
42
55
  data.each do |key, value|
43
- specify_value value, node_path(key)
56
+ assign_value value, node_path(key)
44
57
  end
45
58
  end
46
59
 
60
+ protected
61
+
47
62
  def node_path node_name
48
63
  @nodes_paths[node_name]
49
64
  end
50
65
 
51
- def specify_value value, path
66
+ def assign_value value, path
52
67
  @xml.xpath(remove_spec_node_from(path)).first.content = value
53
68
  end
54
69
 
55
70
  def synchronize_object_to_xml
56
71
  nodes_in_template = @template.xpath("//#{SPEC_NODE}")
57
- nodes_in_template.each do | node_in_template |
72
+ nodes_in_template.each do |node_in_template|
58
73
  define_accessors_for node_in_template
59
- @nodes_paths = @nodes_paths.merge ({ node_in_template.text.to_sym => node_in_template.path })
74
+ @nodes_paths = @nodes_paths.merge ({node_in_template.text.to_sym => node_in_template.path})
60
75
  end
61
76
  end
62
-
77
+
63
78
  def define_accessors_for node_in_template
64
- attr_value = @xml.xpath(remove_spec_node_from(node_in_template.path)).text
65
- self.class.add_attr_reader_for node_in_template, attr_value
66
- self.class.add_attr_writer_for node_in_template
79
+ attr_path = remove_spec_node_from(node_in_template.path)
80
+ attr_value = @xml.xpath(attr_path).text
81
+
82
+ add_attr_reader_for node_in_template, attr_path
83
+ add_attr_writer_for node_in_template
67
84
  end
68
85
 
69
86
  def remove_spec_node_from a_path
70
87
  a_path.gsub("/#{SPEC_NODE}", "")
71
88
  end
72
89
 
73
- def self.add_attr_reader_for node_in_template, attr_value
74
- define_method node_in_template.text.to_sym, lambda { attr_value }
90
+ def add_attr_reader_for node_in_template, attr_path
91
+ define_singleton_method node_in_template.text.to_sym, lambda {
92
+ @xml.at_xpath(attr_path).text
93
+ }
75
94
  end
76
95
 
77
- def self.add_attr_writer_for node_in_template
78
- define_method (node_in_template.text).concat('=').to_sym, Proc.new { | new_value |
79
- specify_value new_value, node_in_template.path
80
- self.class.add_attr_reader_for node_in_template, new_value
96
+ def add_attr_writer_for node_in_template
97
+ define_singleton_method (node_in_template.text).concat('=').to_sym, Proc.new { |new_value|
98
+ assign_value new_value, node_in_template.path
99
+ add_attr_reader_for node_in_template, remove_spec_node_from(node_path(node_in_template.text.to_sym))
81
100
  }
82
101
  end
83
102
  end
@@ -6,13 +6,13 @@ describe MatchPoint do
6
6
 
7
7
  let(:template) do
8
8
  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
9
- <TIX version=\"TIX 4.11.0\">
10
- <AccessControl method=\"AccessControlAuthenticate\">
11
- <AuthRequest>
12
- <AuthID><MatchPointSpec>auth_id</MatchPointSpec></AuthID>
13
- </AuthRequest>
14
- </AccessControl>
15
- </TIX>"
9
+ <TIX version=\"TIX 4.11.0\">
10
+ <AccessControl method=\"AccessControlAuthenticate\">
11
+ <AuthRequest>
12
+ <AuthID><MatchPointSpec>auth_id</MatchPointSpec></AuthID>
13
+ </AuthRequest>
14
+ </AccessControl>
15
+ </TIX>"
16
16
  end
17
17
 
18
18
  let(:response_xml) do
@@ -26,8 +26,8 @@ describe MatchPoint do
26
26
  </TIX>"
27
27
  end
28
28
 
29
- let(:response) { MatchPoint::XmlInstance.new(template, response_xml) }
30
- let(:request) { MatchPoint::XmlInstance.new(template, response_xml, {:auth_id=>"NeWVaLue"})}
29
+ let(:response) { MatchPoint::XmlInstance.from_xml(template, response_xml) }
30
+ let(:request) { MatchPoint::XmlInstance.from_hash(template, {:auth_id=>"NeWVaLue"})}
31
31
 
32
32
  it "should get the auth_id from the xml if the template for that field is defined" do
33
33
  response.auth_id.should == "1ua5J1gJz3TryPEMQZkeWFarOagSJvYY"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: match_point
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: