oxmlk 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
data/examples/example.rb CHANGED
@@ -25,6 +25,7 @@ class Person
25
25
  ox_attr :alternate, :from => 'alt'
26
26
 
27
27
  ox_elem :name
28
+ ox_elem :lame?
28
29
  ox_elem :contacts, :as => [Number,Email]
29
30
  ox_elem :friends, :as => [Person], :in => :friends
30
31
 
@@ -1,6 +1,6 @@
1
1
  <person category="meat_popsicle" alt="human">
2
2
  <name>Joe</name>
3
- <lame>false</lame>
3
+ <lame>false</lame>
4
4
  <number group="main">3035551212</number>
5
5
  <number group="cell">3035551234</number>
6
6
  <email group="home">test@example.com</email>
@@ -0,0 +1,32 @@
1
+ <Response>
2
+ <Play loop="2">http://foo.com/cowbell.mp3</Play>
3
+ <Say voice="man" loop="4" language="en">Josh</Say>
4
+ <Gather action="/digit.php" method="GET" timeout="10" finishOnKey="4" numDigits="3">
5
+ <Say>Robinson</Say>
6
+ <Play>http://foo.com/cowbell.mp3</Play>
7
+ </Gather>
8
+ <Record action="http://foo.edu/handleRecording.php" method="GET" maxLength="20" finishOnKey="123*"/>
9
+ <Say voice="man" loop="4" language="en">Josh</Say>
10
+ <Redirect>http://www.foo.com/nextInstructions</Redirect>
11
+ <Pause length="10"/>
12
+ <Hangup/>
13
+ <Dial callerid="1234567">
14
+ <Number sendDigits="ww3">344444</Number>
15
+ </Dial>
16
+
17
+
18
+ <Rule name="open" mode="all">
19
+ <Conditions>
20
+ <Schedule mode="all" tz_offset="-7" time="08:00-17:00" year="2009-2020" month="jan-dec" day_of_week="mon-fri" day_of_month="10-20"/>
21
+ <Tags mode="all" include="monkey" exclude="tacobell"/>
22
+ </Conditions>
23
+
24
+ <Match>
25
+ <Tag>open</Tag>
26
+ </Match>
27
+
28
+ <NoMatch>
29
+ <Tag>closed</Tag>
30
+ </NoMatch>
31
+ </Rule>
32
+ </Response>
data/lib/oxmlk/attr.rb CHANGED
@@ -7,15 +7,16 @@ module OxMlk
7
7
  Symbol => :to_sym.to_proc,
8
8
  :bool => proc {|a| fetch_bool(a)})
9
9
 
10
- attr_reader :accessor, :setter,:from, :as, :procs, :tag
10
+ attr_reader :accessor, :setter,:from, :as, :procs, :tag_proc, :tag
11
11
 
12
12
  def initialize(name,o={},&block)
13
13
  name = name.to_s
14
14
  @accessor = name.chomp('?').intern
15
15
  @setter = "#{@accessor}=".intern
16
- @from = o.delete(:from)
17
- @tag = (from || accessor).to_s
18
- @as = o.delete(:as) || (:bool if name.ends_with?('?'))
16
+ @from = o[:from]
17
+ @tag_proc = o[:tag_proc].to_proc rescue proc {|x| x}
18
+ @tag = (from || (@tag_proc.call(accessor.to_s) rescue accessor)).to_s
19
+ @as = o[:as] || (:bool if name.ends_with?('?'))
19
20
  @procs = ([*as].map {|k| PROCS[k]} + [block]).compact
20
21
  end
21
22
 
data/lib/oxmlk/elem.rb CHANGED
@@ -11,11 +11,13 @@ module OxMlk
11
11
  :value => proc {|e| e.value},
12
12
  :bool => proc {|e| fetch_bool(e)})
13
13
 
14
- attr_reader :accessor, :setter, :collection, :from, :as, :in, :procs, :block, :tag
14
+ attr_reader :accessor, :setter, :collection, :from, :as, :in, :procs, :block, :tag_proc, :tag
15
15
 
16
16
  def initialize(name,o={},&blk)
17
- @tag = name.to_s.chomp('?')
18
- @accessor = @tag.intern
17
+ @tag_proc = o[:tag_proc].to_proc rescue proc {|x| x}
18
+ @base_tag = name.to_s.chomp('?')
19
+ @tag = @tag_proc.call(@base_tag) rescue @base_tag
20
+ @accessor = @base_tag.intern
19
21
  @setter = "#{@accessor}=".intern
20
22
  @collection = o[:as].is_a?(Array)
21
23
 
data/lib/oxmlk.rb CHANGED
@@ -27,22 +27,33 @@ module OxMlk
27
27
  end
28
28
 
29
29
  module ClassMethods
30
- attr_accessor :ox_attrs, :ox_elems
30
+ attr_accessor :ox_attrs, :ox_elems, :tag_proc
31
31
 
32
32
  def ox_attr(name,o={})
33
- new_attr = Attr.new(name, o)
33
+ new_attr = Attr.new(name, o.reverse_merge(:tag_proc => @tag_proc))
34
34
  @ox_attrs << new_attr
35
35
  attr_accessor new_attr.accessor
36
36
  end
37
37
 
38
38
  def ox_elem(name,o={})
39
- new_elem = Elem.new(name, o)
39
+ new_elem = Elem.new(name, o.reverse_merge(:tag_proc => @tag_proc))
40
40
  @ox_elems << new_elem
41
41
  attr_accessor new_elem.accessor
42
42
  end
43
43
 
44
- def ox_tag(tag=false)
45
- @ox_tag ||= (tag || self).to_s.split('::').last
44
+ def ox_tag(tag=nil,&block)
45
+ raise 'you can only set tag or a block, not both.' if tag && block
46
+
47
+ @base_tag ||= self.to_s.split('::').last
48
+ @ox_tag ||= case tag
49
+ when String
50
+ tag
51
+ when Proc, Symbol, nil
52
+ @tag_proc = (block || tag || :to_s).to_proc
53
+ @tag_proc.call(@base_tag) rescue tag.to_s
54
+ else
55
+ raise 'you passed something weird'
56
+ end
46
57
  end
47
58
 
48
59
  def ox?
@@ -70,6 +81,9 @@ module OxMlk
70
81
  ox
71
82
  end
72
83
 
84
+ def xml_convention(converter=nil)
85
+ @xml_convention = converter
86
+ end
73
87
  end
74
88
 
75
89
  end
data/oxmlk.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{oxmlk}
8
- s.version = "0.3.1"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Josh Robinson"]
12
- s.date = %q{2009-09-02}
12
+ s.date = %q{2009-09-03}
13
13
  s.description = %q{OxMlk gives you a simple way to map you ruby objects to XML and then convert one to the other.}
14
14
  s.email = %q{hexorx@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "examples/twitter.rb",
29
29
  "examples/xml/amazon.xml",
30
30
  "examples/xml/example.xml",
31
+ "examples/xml/phrk.xml",
31
32
  "examples/xml/posts.xml",
32
33
  "examples/xml/twitter.xml",
33
34
  "lib/oxmlk.rb",
@@ -48,6 +48,10 @@ describe OxMlk::Attr do
48
48
  it 'should default to name cleaned up' do
49
49
  ox_attr(:name).tag.should == 'name'
50
50
  end
51
+
52
+ it 'should apply :tag_proc if set' do
53
+ ox_attr(:name, :tag_proc => :upcase.to_proc).tag.should == 'NAME'
54
+ end
51
55
  end
52
56
  end
53
57
 
@@ -124,6 +124,10 @@ describe OxMlk::Elem do
124
124
  it 'should add :in + / to all items in array of ox objects' do
125
125
  ox_elem(:name, :as => [OxKlass,OxKlass], :in => :friends).xpath.should == 'friends/name|friends/name'
126
126
  end
127
+
128
+ it 'should be :tag_proc.call(name) if :tag_proc is set' do
129
+ ox_elem(:name, :tag_proc => :upcase).xpath.should == 'NAME'
130
+ end
127
131
  end
128
132
  end
129
133
 
data/spec/oxmlk_spec.rb CHANGED
@@ -24,7 +24,7 @@ describe OxMlk::InstanceMethods do
24
24
  end
25
25
 
26
26
  it 'should set elements' do
27
- @oxml.children.map(&:name).should == ['name','number','number','email','friends']
27
+ @oxml.children.map(&:name).should == ['name','lame','number','number','email','friends']
28
28
  end
29
29
 
30
30
  it 'should set content to text node' do
@@ -84,7 +84,6 @@ describe OxMlk::ClassMethods do
84
84
  end
85
85
 
86
86
  describe '#from_xml' do
87
-
88
87
  it 'should return an instance of class' do
89
88
  @klass.from_xml('<person/>').should be_a(@klass)
90
89
  end
@@ -125,7 +124,7 @@ describe OxMlk::ClassMethods do
125
124
 
126
125
  describe '#ox_elems' do
127
126
  it 'should return a list of elements' do
128
- @klass.ox_elems.size.should == 3
127
+ @klass.ox_elems.size.should == 4
129
128
  end
130
129
  end
131
130
 
@@ -137,6 +136,33 @@ describe OxMlk::ClassMethods do
137
136
  it 'should default to class name with module removed' do
138
137
  Test::Example.ox_tag.should == 'Example'
139
138
  end
139
+
140
+ describe 'procs and blocs' do
141
+ before(:each) do
142
+ @klass = Class.new do
143
+ include OxMlk
144
+
145
+ def self.to_s
146
+ 'ExampleTest'
147
+ end
148
+ end
149
+ end
150
+
151
+ it 'should apply proc if set' do
152
+ @klass.ox_tag(proc {|x| x.upcase}).should == 'EXAMPLETEST'
153
+ end
154
+
155
+ it 'should apply :sym.to_proc if set' do
156
+ @klass.ox_tag(:downcase).should == 'exampletest'
157
+ end
158
+
159
+ it 'should apply block if passed' do
160
+ @klass.ox_tag do |x|
161
+ x.underscore
162
+ end
163
+ @klass.ox_tag.should == 'example_test'
164
+ end
165
+ end
140
166
  end
141
167
  end
142
168
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oxmlk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Robinson
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-02 00:00:00 -06:00
12
+ date: 2009-09-03 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -63,6 +63,7 @@ files:
63
63
  - examples/twitter.rb
64
64
  - examples/xml/amazon.xml
65
65
  - examples/xml/example.xml
66
+ - examples/xml/phrk.xml
66
67
  - examples/xml/posts.xml
67
68
  - examples/xml/twitter.xml
68
69
  - lib/oxmlk.rb