Comments (10)
Oh hey that's cool! Yeah sure please give it a shot. There's a lot of metaprogramming there (kinda needed to) so I hope you find your way around.
from storext.
What do you think about simply solving this like here: https://github.com/stebo/storext/commit/9335e5d23c395f509c13f67c4afd7166ee984d3a
from storext.
Edit: I just came up that it maybe would be better to add the predicate method for all type of values, not only Booleans (which Rails is also doing for Strings, Integers...), so my commit could be simplified even more by just adding
alias_method "#{attr}?", attr
in line 22...
So you could e.g. also check if a book has a title with
book.title? # => true if title is set
from storext.
@stebo yes look like your commit will work for booleans.
About your second comment: won't that just return the value? For example, if #age
returns 25
, then wouldn't #age?
also return 25? I think we have to test for presence. But then, should an empty string (""
) return true
or false
? Perhaps the least surprising implementation would be anything that is nil
or false
will return false. Anything not nil
or true
will return true. Do I make sense?
from storext.
@ramontayag ahhh of course you are right, did not really made a test for non-boolean values.
So here is a new proposal. We add this method
def storext_define_predicater(column, attr)
define_method "#{attr}?" do
if send(column) && send(column).has_key?(attr.to_s)
store_val = !!read_store_attribute(column, attr)
# storext_cast_proxy.send("#{attr}=", store_val)
else
false
end
# storext_cast_proxy.send("#{attr}")
end
end
and call it in def storext_define_accessor
after storext_define_reader
.
- the two commented cast_proxy lines could be deleted and are not needed here, or?
- the "else false" is necessary because otherwise attributes which are defined without a default value and are not yet existing in the json/hstore Hash, will result in
object.attribute => nil
object.attribute? => nil # and we want false here
I think we could than shorten the method to
def storext_define_predicater(column, attr)
define_method "#{attr}?" do
false unless send(column) && send(column).has_key?(attr.to_s)
!!read_store_attribute(column, attr)
end
end
What do you think?
from storext.
Yes I think that will work! return false
though, so it doesn't continue.
from storext.
Good hint, I included that. Here is the pull request: #26
from storext.
Thanks for the contribution!
from storext.
v2.2.0
has been released with this change
from storext.
😄 😄 awesome @stebo @ramontayag !
from storext.
Related Issues (20)
- Default value based on another value? HOT 2
- `hash': wrong number of arguments (given 1, expected 0) (ArgumentError)
- Replace 'Virtus' with 'dry-types' for Coercion HOT 5
- Encrypt stored data HOT 4
- Any support for responding to Enums? HOT 2
- Use with Ransack HOT 2
- Update single attribute in hash column HOT 4
- Support namespace HOT 3
- Can't dump anonymous class if default value not set. HOT 9
- Boolean type not recognized when using store_attributes inside concern HOT 4
- Empty string is converted to nil automatically HOT 3
- nested ActiveModel objects HOT 5
- 3.0.0 File permissions issue HOT 3
- define conditional attributes HOT 2
- Rails 6.0.0 Support HOT 1
- How do I manage validatations of attributes with Storext? HOT 2
- Audited gem HOT 2
- Support for Rails 6.1 HOT 3
- Using Github Actions instead of Travis?
- Rails 7 support 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 storext.