Giter Club home page Giter Club logo

Comments (3)

acook avatar acook commented on September 3, 2024

I can see the advantage of going in, expecting values to be stored in instance variables, but there's plenty of cases where that wouldn't be enough, Struct just being one example.

Perhaps something like this for both methods and generic operations:

Perpetuity.generate_mapper_for Article do
  attribute :title, accessor: :title
  attribute :body,  via: lambda{|article| article.content(:unescaped) }
end

I'd see the individual methods implemented before the lambda.

from perpetuity.

jgaskins avatar jgaskins commented on September 3, 2024

Yep. It'd be pretty nice to have other options to store/retrieve attributes. I like the ivars as the default (since I'd bet that's the most common). I originally thought about passing blocks instead of lambdas:

Perpetuity.generate_mapper_for Article do
  attribute :title do |article|
    article.title
  end
  # ... or ...
  attribute(:title) { |article| article.title }
end

Aesthetically, I'm not sure I like either one of these, though. Using do/end on separate lines breaks up the declarations. Using parentheses to override precedence for the curly-brace block would make it look different from other lines.

Also, it wouldn't account for storing the attributes. Something like the :accessor attribute would take care of that, but if we pass in procs, blocks, or lambdas (oh my) we'll need to allow two different ones to allow for deserialization as well as serialization.

Perpetuity.generate_mapper_for Article do
  attribute :body, reader: proc { |article| article.title }, writer: proc { |article, value| article.title = value }
end

The upside of something like this is that we can get rid of the hardcoded instance variable access and supply default procs instead (which technically would still hardcode the instance variable access, but at least it'd be overridable :-)).

from perpetuity.

jgaskins avatar jgaskins commented on September 3, 2024

Hmm … It seems some of the things I mentioned here might conflict with what we've discussed in #15. Maybe using procs might not be a good idea and we should stick to letting users specify whether to use ivars or accessor methods.

from perpetuity.

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.