DocumentBuilder
This is a small set of modules for building up xpath based document attributes from a nokogir document object.
Installation
Add this line to your application's Gemfile:
gem 'document_builder'
And then execute:
$ bundle
Or install it yourself as:
$ gem install document_builder
Usage
Documents can be built using the Model and Collection modules as seen in the
following example
class ListItem
include DocumentBuilder::Model
property :name, type: TextProperty
end
class SomeDocument
include DocumentBuilder::Model
root "//root"
collection :collection, selector: "//collection//inner-list-item", type: ListItem
collection :outer_collection, selector: "outer-list-item", type: ListItem
property :property, selector: "//property", type: TextProperty
end
require 'document_builder'
require 'nokogiri'
DOCUMENT = " <root>\n <collection>\n <inner-list-item>Inner Item 1</inner-list-item>\n <inner-list-item>Inner Item 2</inner-list-item>\n </collection>\n <outer-list-item>Outer Item 1</outer-list-item>\n <outer-list-item>Outer Item 2</outer-list-item>\n <property>Some Property</property>\n </root>\n"
document = Nokogiri::XML(DOCUMENT)
some_document = SomeDocument.new(document)
=> #<SomeDocument:0x3fd4799693ac> Attributes: {
"collection": [
{ "name": "Inner Item 1" },
{ "name": "Inner Item 2" }
],
"outer_collection": [
{ "name": "Outer Item 1" },
{ "name": "Outer Item 2" }
],
"property": "Some Property"
}
some_document.property
=> "Some Property"
Contributing
- Fork it ( https://github.com/[my-github-username]/document_builder/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request