Skip to content

Commit

Permalink
Merge pull request #54 from rlburkes/master
Browse files Browse the repository at this point in the history
Allow unwrap option to whitelist keys.
  • Loading branch information
tjarratt committed Mar 4, 2016
2 parents e3b7d06 + b174cda commit 915585c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ Gyoku.xml({ :camel_case => "key" }, { :key_converter => :camelcase })
# => "<CamelCase>key</CamelCase>"
```

Custom key converters. You can use a lambda/Proc to provide customer key converters.
This is a great way to leverage active support inflections for domain specific acronyms.

``` ruby
# Use camelize lower which will hook into active support if installed.
Gyoku.xml({ acronym_abc: "value" }, key_converter: lambda { |key| key.camelize(:lower) })
# => "<acronymABC>value</acronymABC>"

```

Hash key Strings are not converted and may contain namespaces.

``` ruby
Expand Down Expand Up @@ -152,6 +162,42 @@ Gyoku.xml(
# => "<foo name=\"bar\">gyoku</foo><foo name=\"baz\" some=\"attr\">rocks!</foo>"
```

Unwrapping Arrays. You can specify an optional `unwrap` argument to modify the default Array
behavior. `unwrap` accepts a boolean flag (false by default) or an Array whitelist of keys to unwrap.
``` ruby
# Default Array behavior
Gyoku.xml({
"foo" => [
{:is => 'great' },
{:is => 'awesome'}
]
})
# => "<foo><is>great</is></foo><foo><is>awesome</is></foo>"

# Unwrap Array behavior
Gyoku.xml({
"foo" => [
{:is => 'great' },
{:is => 'awesome'}
]
}, unwrap: true)
# => "<foo><is>great</is><is>awesome</is></foo>"

# Unwrap Array, whitelist.
# foo is not unwrapped, bar is.
Gyoku.xml({
"foo" => [
{:is => 'great' },
{:is => 'awesome'}
],
"bar" => [
{:is => 'rad' },
{:is => 'cool'}
]
}, unwrap: [:bar])
# => "<foo><is>great</is></foo><foo><is>awesome</is></foo><bar><is>rad</is><is>cool</is></bar>"
```

Naturally, it would ignore :content! if tag is self-closing:

``` ruby
Expand Down
10 changes: 7 additions & 3 deletions lib/gyoku/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Array
def self.to_xml(array, key, escape_xml = true, attributes = {}, options = {})

self_closing = options.delete(:self_closing)
unwrap = options[:unwrap] || false
unwrap = unwrap?(options.fetch(:unwrap, false), key)

iterate_with_xml array, key, attributes, options do |xml, item, attrs, index|
if self_closing
Expand Down Expand Up @@ -44,9 +44,9 @@ def self.to_xml(array, key, escape_xml = true, attributes = {}, options = {})
def self.iterate_with_xml(array, key, attributes, options, &block)

xml = Builder::XmlMarkup.new
unwrap = options[:unwrap] || false
unwrap = unwrap?(options.fetch(:unwrap, false), key)

if (unwrap)
if unwrap
xml.tag!(key) { iterate_array(xml, array, attributes, &block) }
else
iterate_array(xml, array, attributes, &block)
Expand Down Expand Up @@ -85,5 +85,9 @@ def self.tag_attributes(attributes, index)
end
end

def self.unwrap?(unwrap, key)
unwrap.kind_of?(::Array) ? unwrap.include?(key.to_sym) : unwrap
end

end
end

0 comments on commit 915585c

Please sign in to comment.