GithubHelp home page GithubHelp logo

DRY property declaration about oat HOT 2 CLOSED

ismasan avatar ismasan commented on August 25, 2024
DRY property declaration

from oat.

Comments (2)

ismasan avatar ismasan commented on August 25, 2024

This goes slightly against the grain of what I originally set out to do with Oat, in that I wanted serializers to be as explicit as possible in their definition even if that means some duplication. The reason was that serializers should define a clear boundary between your model layer and your app's public HTTP API.

However, I can see how a more automatic mapping from "item" to "schema" attributes can be practical in some cases.

I'm not sure I would overload the properties and property methods to have the extra behaviour, as those methods are meant to be simple to understand and explicit in what the value is. Maybe we could add a separate method whose name describes the behaviour? For example map_properties (or something better, maybe just map). So your serializers would show a clear distinction between the definitions where you explicitly set the value and those where you just want to map them straight from the item.

class UserSerializer < ApplicationSerializer
  schema do
    map_properties :id, :email, :name, :created_at, :updated_at
    property :age, item.years_old
    property :psychic, item.psychic?
    properties do |p|
      p.active item.active?
    end
  end
end

Does that make sense?

from oat.

shekibobo avatar shekibobo commented on August 25, 2024

Yeah, I actually like #map_properties a lot. Much clearer, and it lets us define it on the serializer to work for all adapters.

from oat.

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.