Skip to content

Commit

Permalink
Merge pull request #42 from ccarruitero/key_to_convert
Browse files Browse the repository at this point in the history
Add :key_to_convert option

Allows the user to specify a conversion that is different for a single key, rather than all keys.
  • Loading branch information
tjarratt committed Jul 14, 2014
2 parents 85d70ab + ae6825a commit c713b9f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/gyoku/xml_key.rb
Expand Up @@ -21,7 +21,7 @@ def create(key, options = {})
xml_key = xml_key.split(":").last
end

xml_key = key_converter(options).call(xml_key) if Symbol === key
xml_key = key_converter(options, xml_key).call(xml_key) if Symbol === key

if !unqualified && qualify?(options) && !xml_key.include?(":")
xml_key = "#{options[:namespace]}:#{xml_key}"
Expand All @@ -33,8 +33,15 @@ def create(key, options = {})
private

# Returns the formula for converting Symbol keys.
def key_converter(options)
key_converter = options[:key_converter] || :lower_camelcase
def key_converter(options, xml_key)
defined_key = options[:key_to_convert]
if (defined_key != nil) && (defined_key == xml_key)
key_converter = options[:key_converter]
elsif defined_key != nil
key_converter = :lower_camelcase
else
key_converter = options[:key_converter] || :lower_camelcase
end
FORMULAS[key_converter]
end

Expand Down
16 changes: 16 additions & 0 deletions spec/gyoku/hash_spec.rb
Expand Up @@ -12,6 +12,22 @@
expect(to_xml(:some => { :new => "user" })).to eq("<some><new>user</new></some>")
end

it "for a nested Hash with key_converter option" do
expect(to_xml({:some => { :new => "user" }}, {key_converter: :camelcase})).to eq("<Some><New>user</New></Some>")
end

it "for a nested Hash with key_converter and key_to_convert options" do
hash = {:some => { :new => "user", :age => 20 }}
options = {key_converter: :camelcase, key_to_convert: "some"}
result = "<Some><new>user</new><age>20</age></Some>"
expect(to_xml(hash, options)).to eq(result)

hash = {:some => { :new => "user", :age => 20 }}
options = {key_converter: :camelcase, key_to_convert: "new"}
result = "<some><New>user</New><age>20</age></some>"
expect(to_xml(hash, options)).to eq(result)
end

it "for a Hash with multiple keys" do
expect(to_xml(:all => "users", :before => "whatever")).to include(
"<all>users</all>",
Expand Down
14 changes: 14 additions & 0 deletions spec/gyoku/xml_key_spec.rb
Expand Up @@ -20,6 +20,20 @@
expect(create(:lower_camel_case!)).to eq("lowerCamelCase")
end

it "when key_converter is defined, convert symbol to the specified type" do
expect(create(:some_text, {key_converter: :camelcase})).to eq("SomeText")
expect(create(:some_text, {key_converter: :upcase})).to eq("SOME_TEXT")
expect(create(:some_text, {key_converter: :none})).to eq("some_text")
end

it "when key_to_convert is defined, convert only this key" do
options = {key_converter: :camelcase, key_to_convert: 'somekey'}
expect(create(:some_key, options)).to eq("someKey")

options = {key_converter: :camelcase, key_to_convert: 'some_key'}
expect(create(:some_key, options)).to eq("SomeKey")
end

context "with :element_form_default set to :qualified and a :namespace" do
it "adds the given namespace" do
key = create :qualify, :element_form_default => :qualified, :namespace => :v1
Expand Down
27 changes: 26 additions & 1 deletion spec/gyoku_spec.rb
Expand Up @@ -33,13 +33,38 @@
end

it "accepts a key_converter for the Hash keys" do
hash = { :user_name => "finn", :pass_word => "secret" }
xml = Gyoku.xml(hash, {key_converter: :upcase})

expect(xml).to include("<USER_NAME>finn</USER_NAME>")
expect(xml).to include("<PASS_WORD>secret</PASS_WORD>")
end

it "don't converts Strings keys" do
hash = { :user_name => "finn", "pass_word" => "secret" }
xml = Gyoku.xml(hash, :key_converter => :upcase)
xml = Gyoku.xml(hash, {key_converter: :upcase})

expect(xml).to include("<USER_NAME>finn</USER_NAME>")
expect(xml).to include("<pass_word>secret</pass_word>")
end

it "when defined key_to_convert only convert this key" do
hash = { user_name: "finn", pass_word: "secret" }
options = {key_converter: :upcase, key_to_convert: 'user_name'}
xml = Gyoku.xml(hash, options)

expect(xml).to include("<USER_NAME>finn</USER_NAME>")
expect(xml).to include("<passWord>secret</passWord>")
end

it "accepts key_converter for nested hash" do
hash = { user: { user_name: "finn", pass_word: "secret" }}
xml = Gyoku.xml(hash, {key_converter: :upcase})

expect(xml).to include("<USER><USER_NAME>finn</USER_NAME>")
expect(xml).to include("<PASS_WORD>secret</PASS_WORD></USER>")
end

it "does not modify the original Hash" do
hash = {
:person => {
Expand Down

0 comments on commit c713b9f

Please sign in to comment.