https://github.com/hex0cter/hash2obj
Ruby gem that applies a hash into a Ruby object and return another instance of the same class using the data in the hash.
https://github.com/hex0cter/hash2obj
Last synced: 8 months ago
JSON representation
Ruby gem that applies a hash into a Ruby object and return another instance of the same class using the data in the hash.
- Host: GitHub
- URL: https://github.com/hex0cter/hash2obj
- Owner: hex0cter
- License: mit
- Created: 2016-06-14T17:39:10.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-11-18T09:32:13.000Z (over 9 years ago)
- Last Synced: 2025-09-18T01:28:56.417Z (9 months ago)
- Language: Ruby
- Homepage: https://rubygems.org/gems/hash2obj
- Size: 5.86 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# The Hash2Obj gem for Ruby
[](https://badge.fury.io/rb/hash2obj)
[](https://travis-ci.org/hex0cter/hash2obj)
Hash2Obj (formerly known as clavatar) is a ruby gem that converts a hash into a Ruby object.
During the web development, it is common to transfer json between the browser and the server. When the json from the
browser presents an object, you might need to convert it back into a Ruby object on the server side.
Normally the class you want to convert to has to provide a method or constructor that takes a hash as argument. In that
way you will have to implement the same interface for all the classes.
Hash2Obj provides another solution for this. When you want to convert a json (hash) into the object, you just need to
provide an existing instance of the same class. For example,
When you call
```ruby
Hash2Obj.cast(hash, object)
```
hash2obj will first inspect all the internal data of object and find out which are writable (for instance,
attr_accessor, attr_writer, etc.) and which are not (for instance, attr_reader, private members, etc).
For those that are not writable, hash2obj attempts to call its constructor using the information provided by the hash.
At the moment hash2obj supports the class constructor in any of the following forms:
```ruby
class A
def initialize([param_1[, param_2[, ...]]] [param_x:[, param_y:[, ...]]] [**args])
...
end
end
```
Once a new instance is created, hash2obj just copies all the other accessible data to the new instance.
Below is an example of how to use hash2obj:
```ruby
require 'hash2obj'
module TestModule
class MyKlass
attr_reader :b1
attr_reader :b2
attr_reader :b3
attr_reader :b4
attr_writer :b5
attr_writer :b6
def initialize(b1, b2 = 2, b3:, b4: 4, **args)
@b1 = b1
@b2 = b2
@b3 = b3
@b4 = b4
@b5 = args.fetch :b5, 0
@b6 = args.fetch :b6, 0
end
def get_attr_b5
@b5
end
def get_attr_b6
@b6
end
end
end
old_obj = TestModule::MyKlass.new(1, b3: 3, b5:5)
old_obj.b6 = 6
old_obj.inspect
# => "#"
new_obj = Hash2Obj.cast({b1: 11, b2: 12, b3: 13, b4: 14, b5: 15, b6: 16}, old_obj)
new_obj.inspect
# => "#"
```
## How to install?
From a terminal run
```bash
gem install hash2obj
```
or add the following code into your Gemfiles:
```ruby
gem 'hash2obj'
```
## License
This code is free to use under the terms of the MIT license.
## Contribution
You are more than welcome to raise any issues [here](https://github.com/hex0cter/hash2obj/issues), or create a Pull Request.