Comments (12)
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.
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.
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.
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.
Yes,
from attr_extras.
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.
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.
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.
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.
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.
You're right about the naming. Merged!
from attr_extras.
Thanks. Great doing software with you guys!
from attr_extras.
Related Issues (18)
- License missing from gemspec HOT 2
- Do we want to support BasicObject? HOT 4
- How to use super in attr_initialize? HOT 7
- attr_accessor_initialize ? HOT 4
- Writer that returns `self` HOT 5
- Optimize with class_eval? HOT 1
- Keyword argument defaults HOT 5
- Invalid instance variable names with hash keys
- pattr_initialize method doesn't support the keyword arguments notation correctly HOT 1
- Default values for non-keyword arguments HOT 1
- Warning with Ruby 2.7 HOT 2
- Initializing custom variables HOT 6
- Can't bundle on ruby 3 (sign-post issue, revisit on minitest release) HOT 2
- Ruby 3.0 and support for chaining `private` with `attr_*` methods
- Default array values results in shared values HOT 1
- Missing git tag for 6.2.5 release HOT 1
- Rename pattr_initialize to prattr_initialize HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from attr_extras.