solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

Hash attributes coercion #336

Closed jasonenglish closed 8 years ago

jasonenglish commented 8 years ago

Wondering if it's possible to coerce a hash value to a collection of models.

Example (based on the readme "Hash attributes coercion" section): attribute :results, Hash[String => Array[Person]]

Given this JSON

{
    "element_count": 60,
    "results": {
        "2015-04-06": [
            {
                "id": 1,
                "name": "Jon Snow"
            }
        ]
    }
}

I would like to end up with the MyObject results being a hash of String:Array[Person] values. => #<MyObject:0x007f82049c3e38 @element_count=60, @results=>{"2015-04-06"=>[#<Person:0x007f82049c3e38 @id=1, @name=Jon Snow.....]

Any ideas on how to achieve this?

jasonenglish commented 8 years ago

Ended up just manually doing the coercion

dmitry commented 8 years ago

@jasonenglish any points how did you made that?