Comments (3)
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.
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.
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)
- RSpec compatibility HOT 2
- Classes in Modules cause Problems HOT 4
- Rails integration HOT 2
- License missing from gemspec HOT 3
- Enumerable predicates on query attributes HOT 3
- load_association! does not make sense HOT 1
- Perpetuity assumes that objects respond_to?(:id). HOT 3
- Fix IdentityMap to return the same instance of the passed-in object HOT 2
- Pass attributes and other mappings to queries
- Need an intermediate form to store serialized data HOT 1
- Intermediate query form HOT 1
- Circular references cause infinite recursion HOT 12
- Load partial associations
- Customized mapping for column and table ? HOT 1
- Error undefined method `human' for Modelname:Class HOT 3
- saving references to classes with attributes that have time datatypes HOT 6
- unable to run performance tests with benchmark HOT 7
- Date ranges do not work HOT 3
- JOIN queries
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 perpetuity.