Giter Site home page Giter Site logo

Comments (7)

jestraw avatar jestraw commented on August 12, 2024 1

For what it's worth (and I've been using representable since 2014 or so), the documentation for nested implies that it's a way of accommodating some structure of the document you are parsing which doesn't match your object structure. In my (apparently inaccurate) mental model of what is going on, nested is just for handling parsing or rendering of the document. It was extremely surprising to have it suddenly acting like property and trying to assign a null value to a non-existent attribute on the represented object!

from representable.

jestraw avatar jestraw commented on August 12, 2024

I've created a file to test this behavior in different combinations: https://gist.github.com/jestraw/7a88cf42984db2e5e3d560ea58970a11

Running this file against 3.1.1 gives the following output:

JsonSongRepresenter
{                                        
  "title": "json-with-details",          
  "details": {                           
    "track": 1                           
  }                                      
}                                        
#<struct Song title="json-with-details", track=1, length=nil>
                                         
{                                        
  "title": "json-empty-details",         
  "details": { }                         
}                                        
#<struct Song title="json-empty-details", track=nil, length=nil>

{
  "title": "json-null-details",
  "details": null
}
undefined method `details=' for #<struct Song title="json-null-details", track=nil, length=nil>

HashSongRepresenter
{"title"=>"hash-with-details", "details"=>{"track"=>1}}
#<struct Song title="hash-with-details", track=1, length=nil>

{"title"=>"hash-empty-details", "details"=>{}}
#<struct Song title="hash-empty-details", track=nil, length=nil>

{"title"=>"hash-null-details", "details"=>nil}
undefined method `details=' for #<struct Song title="hash-null-details", track=nil, length=nil>

JsonHashSongRepresenter
{"title"=>"hash-with-details", "details"=>{"track"=>1}}
#<struct Song title="hash-with-details", track=1, length=nil>

{"title"=>"hash-empty-details", "details"=>{}}
#<struct Song title="hash-empty-details", track=nil, length=nil>

{"title"=>"hash-null-details", "details"=>nil}
undefined method `details=' for #<struct Song title="hash-null-details", track=nil, length=nil>

from representable.

apotonick avatar apotonick commented on August 12, 2024

Hi @jestraw! The problem here is that Song doesn't have a details= method!

from representable.

jestraw avatar jestraw commented on August 12, 2024

Yes, I know. But why is nested trying to assign to the represented object?

from representable.

apotonick avatar apotonick commented on August 12, 2024

That's the design of Representable, in Reform (also using Representable), we have "populators" to solve this problem. When parsing, it simply writes the read values on represented. You'd probably need the :class option here. Hit me up on https://trailblazer.zulipchat.com if you need assistance! 🍻

It won't help you now but I will soon start working on Representable 4 which is, exactly like Reform 3, completely based on Trailblazer::Activity meaning you can customize it much easier.

from representable.

apotonick avatar apotonick commented on August 12, 2024

@jestraw nested will read the nested fragment, may or may not create a nested "target" object for that and than assign that to represented - in your mental model, what would Rpr do with the parsed fragment instead?

from representable.

jestraw avatar jestraw commented on August 12, 2024

Fair enough! I think the actual document I was trying to parse was weird -- it was essentially relating a kind of left outer join where the joined table was empty by presenting the key for the join with a nil value. Since the document was already parsed to a hash, I could have done .compact and gotten rid of the problem, but that wouldn't have looked as nice as just doing for_collection. I ended up with some if directives on the nested to skip parsing if the value was nil, which does work.

from representable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.