Git Product home page Git Product logo

Comments (10)

ramontayag avatar ramontayag commented on July 26, 2024 1

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.

stebo avatar stebo commented on July 26, 2024

What do you think about simply solving this like here: https://github.com/stebo/storext/commit/9335e5d23c395f509c13f67c4afd7166ee984d3a

from storext.

stebo avatar stebo commented on July 26, 2024

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.

ramontayag avatar ramontayag commented on July 26, 2024

@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.

stebo avatar stebo commented on July 26, 2024

@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.

  1. the two commented cast_proxy lines could be deleted and are not needed here, or?
  2. 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.

ramontayag avatar ramontayag commented on July 26, 2024

Yes I think that will work! return false though, so it doesn't continue.

from storext.

stebo avatar stebo commented on July 26, 2024

Good hint, I included that. Here is the pull request: #26

from storext.

ramontayag avatar ramontayag commented on July 26, 2024

Thanks for the contribution!

from storext.

ramontayag avatar ramontayag commented on July 26, 2024

v2.2.0 has been released with this change

from storext.

mscoutermarsh avatar mscoutermarsh commented on July 26, 2024

😄 😄 awesome @stebo @ramontayag !

from storext.

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.