Ruby gem that transforms from JSON, XML, Hash and CSV to an instance class with its attributes.
Classes are created in runtime with their corresponding attributes, as well as the attributes values are set. It also respects the object hierarchy and generates the nested instance classes.
Handy tool to parse and manipulate HTTP responses, files and objects in the format listed above.
Add this line to your application's Gemfile:
gem 'modelize'
And then execute:
$ bundle
Or install it yourself as:
$ gem install modelize
To create the class dynamically and get it instanciated:
Modelize.create(source, options)- source: Object or file's path.
- options:
- format (required): (:xml|:json|:csv|:hash)
- file (false by default):
- true: When in the source is the path for the file containing the data.
- false: When the object is directly passed in the source param.
- separator: Only for CSV! Points the character used as column separator in the CSV file.
- class (by default nil): Custom class that can be passed to be used as instance class root. Useful to have predefined functions and validations.
have a look to the examples to see how it works in each case supported.
source = "{
\"root\" : {
\"key\" : \"value1\",
\"array_key\" : [1, 2, 3],
\"object_key\" : {
\"sub_key\" : 123
}
}
}"
instance = Modelize.create(source, format: :json)
instance.root.key
#=> "value"
instance.root.array_key
#=> [1, 2, 3]
instance.root.object_key.sub_key
#=> 123source = {
"root" => {
"key" => "value1",
"array_key" => [1, 2, 3],
"object_key" => {
"sub_key" => 123
}
}
}
# This format is also supported
# source = {
# root: {
# key: "value1",
# array_key: [1, 2, 3],
# object_key: {
# sub_key: 123
# }
# }
# }
instance = Modelize.create(source, format: :hash)
instance.root.key
#=> "value"
instance.root.array_key
#=> [1, 2, 3]
instance.root.object_key.sub_key
#=> 123Having the following XML file whose path is "path/to/file"
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<key>value1</key>
<array_key>1</array_key>
<array_key>2</array_key>
<array_key>3</array_key>
<object_key>
<sub_key1>123</sub_key1>
</object_key>
</root>Modelize.create("path/to/file", format: :xml, file: true)
instance.root.key
#=> "value"
instance.root.array_key
#=> [1, 2, 3]
instance.root.object_key.sub_key
#=> "123"Note, integers and floats will be returned in string format until fix.
Having a JSON file whose path is "path/to/file"
{
"root" : {
"key" : "value",
"array_key" : [1, 2, 3],
"object_key" : {
"sub_key" : 123
}
}
}Modelize.create("path/to/file", format: :json, file: true)
instance.root.key
#=> "value"
instance.root.array_key
#=> [1, 2, 3]
instance.root.object_key.sub_key
#=> 123Having this CSV file whose path is "path/to/file"
id;name;phone;city;height
1;Tom;123;Valencia;1.78
2;Paul;231;Zurich;1.98Modelize.create("path/to/file", format: :csv, file: true, separator: ";")
instance.first.name
#=> "Tom"
instance.last.height
#=> "1.98"To give also space to some customization you can define your methods in the custom class that you pass.
Given the following source:
source = {
coordinates: {
lat: 31.1234,
lng: -0.3562
}
}Lets pass the class Place to Modelize
class Place
def latlng
"#{coordinates.lat},#{coordinates.lng}"
end
endNow, by making use of this class you can manipulate inner class instances in a predefined way.
place = Modelize.create(source, format: :hash, class: Place)
place.latlng
#=> "31.1234,-0.3562"- Fork it ( https://github.com/[my-github-username]/modelize/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