representative 0.3.3 → 0.3.4
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.
- data/Rakefile +4 -13
- data/lib/representative/abstract_xml.rb +34 -1
- data/lib/representative/base.rb +1 -10
- data/lib/representative/version.rb +1 -1
- data/spec/representative/nokogiri_spec.rb +2 -2
- data/spec/representative/xml_behaviour.rb +34 -3
- data/spec/representative/xml_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -7
- metadata +5 -6
data/Rakefile
CHANGED
@@ -4,20 +4,11 @@ require 'bundler'
|
|
4
4
|
|
5
5
|
Bundler::GemHelper.install_tasks
|
6
6
|
|
7
|
-
require "
|
7
|
+
require "rspec/core/rake_task"
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
spec.spec_opts << "-Du"
|
13
|
-
spec.spec_opts << "--color"
|
9
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
10
|
+
t.pattern = 'spec/**/*_spec.rb'
|
11
|
+
t.rspec_opts = ["--colour", "--format", "nested"]
|
14
12
|
end
|
15
13
|
|
16
14
|
task :default => :spec
|
17
|
-
|
18
|
-
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
19
|
-
spec.libs << 'lib' << 'spec'
|
20
|
-
spec.spec_files = FileList['spec/**/*_spec.rb']
|
21
|
-
spec.rcov = true
|
22
|
-
end
|
23
|
-
|
@@ -4,6 +4,11 @@ module Representative
|
|
4
4
|
|
5
5
|
class AbstractXml < Base
|
6
6
|
|
7
|
+
def initialize(subject = nil, options = {})
|
8
|
+
super(subject, options)
|
9
|
+
@naming_strategy = options[:naming_strategy] || :dasherize
|
10
|
+
end
|
11
|
+
|
7
12
|
# Generate an element.
|
8
13
|
#
|
9
14
|
# With two arguments, it generates an element with the specified text content.
|
@@ -64,7 +69,7 @@ module Representative
|
|
64
69
|
end
|
65
70
|
end
|
66
71
|
|
67
|
-
generate_element(name
|
72
|
+
generate_element(format_name(name), resolved_attributes, content_string, &content_block)
|
68
73
|
|
69
74
|
end
|
70
75
|
|
@@ -112,6 +117,34 @@ module Representative
|
|
112
117
|
def empty
|
113
118
|
Representative::EMPTY
|
114
119
|
end
|
120
|
+
|
121
|
+
private
|
122
|
+
|
123
|
+
attr_reader :naming_strategy
|
124
|
+
|
125
|
+
def format_name(name)
|
126
|
+
name = name.to_s
|
127
|
+
case naming_strategy
|
128
|
+
when :camelcase
|
129
|
+
name.camelcase(:lower)
|
130
|
+
when :dasherize
|
131
|
+
name.dasherize
|
132
|
+
when Symbol
|
133
|
+
name.send(naming_strategy)
|
134
|
+
else
|
135
|
+
naming_strategy.to_proc.call(name)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def resolve_attributes(attributes)
|
140
|
+
if attributes
|
141
|
+
attributes.inject({}) do |resolved, (name, value_generator)|
|
142
|
+
resolved_value = resolve_value(value_generator)
|
143
|
+
resolved[format_name(name)] = resolved_value unless resolved_value.nil?
|
144
|
+
resolved
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
115
148
|
|
116
149
|
end
|
117
150
|
|
data/lib/representative/base.rb
CHANGED
@@ -7,6 +7,7 @@ module Representative
|
|
7
7
|
def initialize(subject = nil, options = {})
|
8
8
|
@subjects = [subject]
|
9
9
|
@inspector = options[:inspector] || ObjectInspector.new
|
10
|
+
@naming_strategy = options[:naming_strategy]
|
10
11
|
end
|
11
12
|
|
12
13
|
# Return the current "subject" of representation.
|
@@ -48,16 +49,6 @@ module Representative
|
|
48
49
|
value_generator
|
49
50
|
end
|
50
51
|
end
|
51
|
-
|
52
|
-
def resolve_attributes(attributes)
|
53
|
-
if attributes
|
54
|
-
attributes.inject({}) do |resolved, (name, value_generator)|
|
55
|
-
resolved_value = resolve_value(value_generator)
|
56
|
-
resolved[name.to_s.dasherize] = resolved_value unless resolved_value.nil?
|
57
|
-
resolved
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
52
|
|
62
53
|
end
|
63
54
|
|
@@ -6,8 +6,8 @@ require "representative/xml_behaviour"
|
|
6
6
|
|
7
7
|
describe Representative::Nokogiri do
|
8
8
|
|
9
|
-
def r
|
10
|
-
@representative ||= Representative::Nokogiri.new(@subject)
|
9
|
+
def r(options = {})
|
10
|
+
@representative ||= Representative::Nokogiri.new(@subject, options)
|
11
11
|
end
|
12
12
|
|
13
13
|
def resulting_xml
|
@@ -12,17 +12,39 @@ shared_examples_for "an XML Representative" do
|
|
12
12
|
|
13
13
|
describe "#element" do
|
14
14
|
|
15
|
+
before do
|
16
|
+
@subject.full_name = "Fredrick"
|
17
|
+
end
|
18
|
+
|
15
19
|
it "generates an element with content extracted from the subject" do
|
16
20
|
r.element :name
|
17
21
|
resulting_xml.should == %(<name>Fred</name>)
|
18
22
|
end
|
19
23
|
|
20
24
|
it "dasherizes the property name" do
|
21
|
-
@subject.full_name = "Fredrick"
|
22
25
|
r.element :full_name
|
23
26
|
resulting_xml.should == %(<full-name>Fredrick</full-name>)
|
24
27
|
end
|
25
28
|
|
29
|
+
context "with naming_strategy :camelcase" do
|
30
|
+
|
31
|
+
it "applies the naming strategy to produce element names" do
|
32
|
+
r(:naming_strategy => :camelcase).element :full_name
|
33
|
+
resulting_xml.should == %(<fullName>Fredrick</fullName>)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
context "with a custom naming_strategy" do
|
39
|
+
|
40
|
+
it "applies the naming strategy to produce element names" do
|
41
|
+
biff = lambda { |name| name.upcase }
|
42
|
+
r(:naming_strategy => biff).element :full_name
|
43
|
+
resulting_xml.should == %(<FULL_NAME>Fredrick</FULL_NAME>)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
26
48
|
describe "with attributes" do
|
27
49
|
|
28
50
|
it "generates attributes on the element" do
|
@@ -73,15 +95,24 @@ shared_examples_for "an XML Representative" do
|
|
73
95
|
|
74
96
|
end
|
75
97
|
|
76
|
-
describe "with a
|
98
|
+
describe "with a Symbol argument" do
|
77
99
|
|
78
|
-
it "calls the
|
100
|
+
it "calls the named method to generate a value" do
|
79
101
|
r.element :name, :width
|
80
102
|
resulting_xml.should == %(<name>200</name>)
|
81
103
|
end
|
82
104
|
|
83
105
|
end
|
84
106
|
|
107
|
+
describe "with a value argument that supports #to_proc" do
|
108
|
+
|
109
|
+
it "calls the Proc on the subject to generate a value" do
|
110
|
+
r.element :name, (lambda { |x| x.width * 3 })
|
111
|
+
resulting_xml.should == %(<name>600</name>)
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
85
116
|
describe "with value argument :self" do
|
86
117
|
|
87
118
|
it "doesn't alter the subject" do
|
@@ -10,8 +10,8 @@ describe Representative::Xml do
|
|
10
10
|
@xml = Builder::XmlMarkup.new
|
11
11
|
end
|
12
12
|
|
13
|
-
def r
|
14
|
-
@representative ||= Representative::Xml.new(@xml, @subject)
|
13
|
+
def r(options = {})
|
14
|
+
@representative ||= Representative::Xml.new(@xml, @subject, options)
|
15
15
|
end
|
16
16
|
|
17
17
|
def resulting_xml
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: representative
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.3.
|
5
|
+
version: 0.3.4
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Mike Williams
|
@@ -10,8 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
14
|
-
default_executable:
|
13
|
+
date: 2011-05-26 00:00:00 Z
|
15
14
|
dependencies:
|
16
15
|
- !ruby/object:Gem::Dependency
|
17
16
|
name: activesupport
|
@@ -76,7 +75,7 @@ dependencies:
|
|
76
75
|
requirements:
|
77
76
|
- - ~>
|
78
77
|
- !ruby/object:Gem::Version
|
79
|
-
version:
|
78
|
+
version: 2.5.0
|
80
79
|
type: :development
|
81
80
|
version_requirements: *id006
|
82
81
|
- !ruby/object:Gem::Dependency
|
@@ -142,7 +141,6 @@ files:
|
|
142
141
|
- spec/representative/xml_spec.rb
|
143
142
|
- spec/spec_helper.rb
|
144
143
|
- Rakefile
|
145
|
-
has_rdoc: true
|
146
144
|
homepage: http://github.com/mdub/representative
|
147
145
|
licenses: []
|
148
146
|
|
@@ -166,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
166
164
|
requirements: []
|
167
165
|
|
168
166
|
rubyforge_project:
|
169
|
-
rubygems_version: 1.
|
167
|
+
rubygems_version: 1.7.2
|
170
168
|
signing_key:
|
171
169
|
specification_version: 3
|
172
170
|
summary: Builds XML and JSON representations of your Ruby objects
|
@@ -178,3 +176,4 @@ test_files:
|
|
178
176
|
- spec/representative/xml_spec.rb
|
179
177
|
- spec/spec_helper.rb
|
180
178
|
- Rakefile
|
179
|
+
has_rdoc:
|