gyoku 0.5.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +7 -0
- data/README.md +2 -13
- data/lib/gyoku.rb +8 -15
- data/lib/gyoku/version.rb +1 -1
- data/lib/gyoku/xml_key.rb +6 -14
- data/spec/gyoku/xml_key_spec.rb +2 -39
- data/spec/gyoku_spec.rb +34 -21
- metadata +4 -4
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
## 1.0.0 (2012-12-17)
|
2
|
+
|
3
|
+
* Refactoring: Removed the global configuration. This should really only affect the
|
4
|
+
`Gyoku.convert_symbols_to` shortcut which was removed as well. If you're using Gyoku
|
5
|
+
with Savon 2.0, there's now an option for that. If you're using Gyoku on itself,
|
6
|
+
you can pass it the `:key_converter` option instead.
|
7
|
+
|
1
8
|
## 0.5.0 (2012-12-15)
|
2
9
|
|
3
10
|
Feature: [#19](https://github.com/savonrb/gyoku/pull/19) adds support for explicit XML attributes.
|
data/README.md
CHANGED
@@ -28,24 +28,13 @@ Gyoku.xml(:lower_camel_case => "key")
|
|
28
28
|
# => "<lowerCamelCase>key</lowerCamelCase>"
|
29
29
|
```
|
30
30
|
|
31
|
-
You can change the default conversion formula.
|
31
|
+
You can change the default conversion formula to :camelcase, :upcase or :none.
|
32
32
|
|
33
33
|
``` ruby
|
34
|
-
Gyoku.
|
35
|
-
|
36
|
-
Gyoku.xml(:camel_case => "key")
|
34
|
+
Gyoku.xml(:camel_case => "key", :key_converter => :camelcase)
|
37
35
|
# => "<CamelCase>key</CamelCase>"
|
38
36
|
```
|
39
37
|
|
40
|
-
And you can also define your own formula.
|
41
|
-
|
42
|
-
``` ruby
|
43
|
-
Gyoku.convert_symbols_to { |key| key.sub(/^prefixed_/, '') }
|
44
|
-
|
45
|
-
Gyoku.xml(:prefixed_attribute => "key")
|
46
|
-
# => "<attribute>key</attribute>"
|
47
|
-
```
|
48
|
-
|
49
38
|
Hash key Strings are not converted and may contain namespaces.
|
50
39
|
|
51
40
|
``` ruby
|
data/lib/gyoku.rb
CHANGED
@@ -2,22 +2,15 @@ require "gyoku/version"
|
|
2
2
|
require "gyoku/hash"
|
3
3
|
|
4
4
|
module Gyoku
|
5
|
-
class << self
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
# Yields this object for configuration.
|
13
|
-
def configure
|
14
|
-
yield self
|
15
|
-
end
|
16
|
-
|
17
|
-
# Sets the formula for converting Symbol keys.
|
18
|
-
def convert_symbols_to(formula = nil, &block)
|
19
|
-
XMLKey.symbol_converter = formula ? formula : block
|
20
|
-
end
|
6
|
+
# Converts a given Hash +key+ with +options+ into an XML tag.
|
7
|
+
def self.xml_tag(key, options = {})
|
8
|
+
XMLKey.create(key, options)
|
9
|
+
end
|
21
10
|
|
11
|
+
# Translates a given +hash+ with +options+ to XML.
|
12
|
+
def self.xml(hash, options = {})
|
13
|
+
Hash.to_xml hash.dup, options
|
22
14
|
end
|
15
|
+
|
23
16
|
end
|
data/lib/gyoku/version.rb
CHANGED
data/lib/gyoku/xml_key.rb
CHANGED
@@ -21,7 +21,7 @@ module Gyoku
|
|
21
21
|
xml_key = xml_key.split(":").last
|
22
22
|
end
|
23
23
|
|
24
|
-
xml_key =
|
24
|
+
xml_key = key_converter(options).call(xml_key) if Symbol === key
|
25
25
|
|
26
26
|
if !unqualified && qualify?(options) && !xml_key.include?(":")
|
27
27
|
xml_key = "#{options[:namespace]}:#{xml_key}"
|
@@ -30,22 +30,14 @@ module Gyoku
|
|
30
30
|
xml_key
|
31
31
|
end
|
32
32
|
|
33
|
-
|
34
|
-
def symbol_converter
|
35
|
-
@symbol_converter ||= FORMULAS[:lower_camelcase]
|
36
|
-
end
|
37
|
-
|
38
|
-
# Sets the +formula+ for converting Symbol keys.
|
39
|
-
# Accepts one of +FORMULAS+ of an object responding to <tt>:call</tt>.
|
40
|
-
def symbol_converter=(formula)
|
41
|
-
formula = FORMULAS[formula] unless formula.respond_to? :call
|
42
|
-
raise ArgumentError, "Invalid symbol_converter formula" unless formula
|
33
|
+
private
|
43
34
|
|
44
|
-
|
35
|
+
# Returns the formula for converting Symbol keys.
|
36
|
+
def key_converter(options)
|
37
|
+
key_converter = options[:key_converter] || :lower_camelcase
|
38
|
+
FORMULAS[key_converter]
|
45
39
|
end
|
46
40
|
|
47
|
-
private
|
48
|
-
|
49
41
|
# Chops special characters from the end of a given +string+.
|
50
42
|
def chop_special_characters(string)
|
51
43
|
["!", "/"].include?(string[-1, 1]) ? string.chop : string
|
data/spec/gyoku/xml_key_spec.rb
CHANGED
@@ -32,51 +32,14 @@ describe Gyoku::XMLKey do
|
|
32
32
|
end
|
33
33
|
|
34
34
|
it "adds a given :namespace after converting the key" do
|
35
|
-
|
36
|
-
|
37
|
-
key = create :username, :element_form_default => :qualified, :namespace => :v1
|
35
|
+
key = create :username, :element_form_default => :qualified, :namespace => :v1, :key_converter => :camelcase
|
38
36
|
key.should == "v1:Username"
|
39
37
|
end
|
40
|
-
|
41
|
-
after { Gyoku::XMLKey.symbol_converter = :lower_camelcase } #reset
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
describe ".symbol_converter" do
|
46
|
-
after { Gyoku::XMLKey.symbol_converter = :lower_camelcase } #reset
|
47
|
-
|
48
|
-
it "returns the default lower_camelcase converter" do
|
49
|
-
Gyoku::XMLKey.symbol_converter.call("snake_case").should == "snakeCase"
|
50
|
-
end
|
51
|
-
|
52
|
-
it "accepts :lower_camelcase" do
|
53
|
-
Gyoku::XMLKey.symbol_converter = :lower_camelcase
|
54
|
-
Gyoku::XMLKey.create(:snake_case).should == "snakeCase"
|
55
|
-
end
|
56
|
-
|
57
|
-
it "accepts :camelcase" do
|
58
|
-
Gyoku::XMLKey.symbol_converter = :camelcase
|
59
|
-
Gyoku::XMLKey.create(:snake_case).should == "SnakeCase"
|
60
|
-
end
|
61
|
-
|
62
|
-
it "accepts :upcase" do
|
63
|
-
Gyoku::XMLKey.symbol_converter = :upcase
|
64
|
-
Gyoku::XMLKey.create(:snake_case).should == "SNAKE_CASE"
|
65
|
-
end
|
66
|
-
|
67
|
-
it "accepts :none" do
|
68
|
-
Gyoku::XMLKey.symbol_converter = :none
|
69
|
-
Gyoku::XMLKey.create(:snake_Case).should == "snake_Case"
|
70
|
-
end
|
71
|
-
|
72
|
-
it "allows to set a custom converter" do
|
73
|
-
Gyoku::XMLKey.symbol_converter = Proc.new { |key| key.upcase }
|
74
|
-
Gyoku::XMLKey.create(:snake_case).should == "SNAKE_CASE"
|
75
38
|
end
|
76
39
|
end
|
77
40
|
|
78
41
|
def create(key, options = {})
|
79
|
-
Gyoku::XMLKey.create
|
42
|
+
Gyoku::XMLKey.create(key, options)
|
80
43
|
end
|
81
44
|
|
82
45
|
end
|
data/spec/gyoku_spec.rb
CHANGED
@@ -2,9 +2,42 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe Gyoku do
|
4
4
|
|
5
|
+
describe ".xml_tag" do
|
6
|
+
it "translates Symbols to lowerCamelCase by default" do
|
7
|
+
tag = Gyoku.xml_tag(:user_name)
|
8
|
+
expect(tag).to eq("userName")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "does not translate Strings" do
|
12
|
+
tag = Gyoku.xml_tag("user_name")
|
13
|
+
expect(tag).to eq("user_name")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "translates Symbols by a given key_converter" do
|
17
|
+
tag = Gyoku.xml_tag(:user_name, :key_converter => :upcase)
|
18
|
+
expect(tag).to eq("USER_NAME")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "does not translates Strings with a given key_converter" do
|
22
|
+
tag = Gyoku.xml_tag("user_name", :key_converter => :upcase)
|
23
|
+
expect(tag).to eq("user_name")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
5
27
|
describe ".xml" do
|
6
28
|
it "translates a given Hash to XML" do
|
7
|
-
|
29
|
+
hash = { :id => 1 }
|
30
|
+
xml = Gyoku.xml(hash, :element_form_default => :qualified)
|
31
|
+
|
32
|
+
xml.should == "<id>1</id>"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "accepts a key_converter for the Hash keys" do
|
36
|
+
hash = { :user_name => "finn", "pass_word" => "secret" }
|
37
|
+
xml = Gyoku.xml(hash, :key_converter => :upcase)
|
38
|
+
|
39
|
+
xml.should include("<USER_NAME>finn</USER_NAME>")
|
40
|
+
xml.should include("<pass_word>secret</pass_word>")
|
8
41
|
end
|
9
42
|
|
10
43
|
it "does not modify the original Hash" do
|
@@ -23,24 +56,4 @@ describe Gyoku do
|
|
23
56
|
end
|
24
57
|
end
|
25
58
|
|
26
|
-
describe ".configure" do
|
27
|
-
it "yields the Gyoku module" do
|
28
|
-
Gyoku.configure { |config| config.should respond_to(:convert_symbols_to) }
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe ".convert_symbols_to" do
|
33
|
-
after { Gyoku.convert_symbols_to(:lower_camelcase) } # reset
|
34
|
-
|
35
|
-
it "accepts a predefined formula" do
|
36
|
-
Gyoku.convert_symbols_to(:camelcase)
|
37
|
-
Gyoku::XMLKey.create(:snake_case).should == "SnakeCase"
|
38
|
-
end
|
39
|
-
|
40
|
-
it "accepts a block" do
|
41
|
-
Gyoku.convert_symbols_to { |key| key.upcase }
|
42
|
-
Gyoku::XMLKey.create(:snake_case).should == "SNAKE_CASE"
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
59
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gyoku
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: builder
|
@@ -100,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
100
|
version: '0'
|
101
101
|
segments:
|
102
102
|
- 0
|
103
|
-
hash:
|
103
|
+
hash: 2627220150066470254
|
104
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
109
|
version: '0'
|
110
110
|
segments:
|
111
111
|
- 0
|
112
|
-
hash:
|
112
|
+
hash: 2627220150066470254
|
113
113
|
requirements: []
|
114
114
|
rubyforge_project: gyoku
|
115
115
|
rubygems_version: 1.8.24
|