Giter Club home page Giter Club logo

Comments (12)

henrik avatar henrik commented on June 5, 2024

Sounds reasonable to me.

What are some examples of what you use this for, out of curiosity? I haven't felt the need myself, or not noticed if I have.

from attr_extras.

monkbroc avatar monkbroc commented on June 5, 2024

The main place where I want to use this are in service objects. They exist mostly for side-effects, but it's good to be able to get to their attributes.

It doesn't make sense to compare two service objects with each other, so using vattr_initialize would a little misleading.

Here's an example.

  class SendMonthlyDigest
    attr_initialize :organization, [:digest_date] do
      @digest_date ||= Date.today
    end

    attr_reader :organization, :digest_date

    def call
      generate_monthly_report
      send_email_to_organization
    end

    private

    def generate_monthly_report
    end

    def send_email_to_organization
    end
  end

I'll implement the code and tests then send a PR.

from attr_extras.

soma avatar soma commented on June 5, 2024

I like the idea, it seems reasonable. I have a little issue with the name you suggested though (which is kind of a problem with more parts of attr_extras), the "p" in pattr_initialize stands for "private", and you suggest rattr_initialize where the r would mean reader, right? I agree vattr_initialize also has this problem. It is confusing in any case!

from attr_extras.

monkbroc avatar monkbroc commented on June 5, 2024

Yes, I agree. pattr_initialize for "public" reader wouldn't work for obvious reasons 😄

What about attr_reader_initialize? It's a little longer, but it conveys the meaning well.

Maybe adding an alias from pattr_initialize to attr_private_initialize and vattr_initialize to attr_value_initialize would fix the confusion you are feeling.

from attr_extras.

soma avatar soma commented on June 5, 2024

Yes, 👍 on more explicit naming!

from attr_extras.

monkbroc avatar monkbroc commented on June 5, 2024

I'd also suggest reordering the readme because the combination methods like pattr_initialize are shown before the simple cases. That was confusing for a first read.

from attr_extras.

henrik avatar henrik commented on June 5, 2024

I'd also suggest reordering the readme because the combination methods like pattr_initialize are shown before the simple cases. That was confusing for a first read.

Ah, that was intentional because we almost always use the higher-level ones in practise. But I can see how it doesn't make for a good introduction. Will fix the order right now.

from attr_extras.

monkbroc avatar monkbroc commented on June 5, 2024

OK. I think the summary paragraph shows clearly the usefulness of the higher-level methods.

About the comment 👍 on more explicit naming, do you want to keep the main name of methods short like pattr_initialize and vattr_initialize and have longer aliases, or would you be open to making the longer names the ones shown in the readme and have aliases for the short ones?

This is what I mean is:

attr_value_initialize :foo, :bar defines initializer, public readers and value object identity: shortcut for

attr_initialize :foo, :bar
attr_value :foo, :bar

The short name vattr_initialize is an alias for attr_value_initialize.

from attr_extras.

henrik avatar henrik commented on June 5, 2024

Aliases aren't a bad idea – I doubt we'd actually use them ourselves, but just having the long forms around would make things a bit more self-documenting.

Maybe the docs could have both in the header/ToC link: vattr_initialize / attr_value_initialize and so on.

Maybe it should be vattr_initialize / value_attr_initialize, pattr_initialize / private_attr_initialize, rattr_initialize / reader_attr_initialize so the short form maps more closely to the long form?

from attr_extras.

monkbroc avatar monkbroc commented on June 5, 2024

Having both in the header/ToC sounds like a good way to make it more self-documenting.

All other methods start with attr, so I would find it more natural to write attr_private_initialize.

from attr_extras.

henrik avatar henrik commented on June 5, 2024

You're right about the naming. Merged!

from attr_extras.

monkbroc avatar monkbroc commented on June 5, 2024

Thanks. Great doing software with you guys!

from attr_extras.

Related Issues (18)

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.