Skip to content

New ruby hash syntax and SymbolOperator #39

Open
disbelief opened this Issue Aug 28, 2013 · 3 comments

3 participants

@disbelief

Wondering if there's any way to get Plucky's SymbolOperator to work with Ruby's new hash syntax?

For example is there a way to get:

People.where :created_at.lt => Time.now

to use the form:

People.where created_at: Time.now

but somehow apply the .lt operator?

Having the two forms sprinkled throughout a codebase, depending on whether I need an operator or not, is really messy.

@cheald
MongoMapper member
cheald commented Aug 28, 2013

Not as far as I'm aware. #lt is a method that is extended onto Symbol, so unless Ruby can figure out that created_at is a symbol, then calling #lt on it isn't going to have the intended effect. I believe this is a Ruby issue rather than a Plucky issue.

If anyone's aware of a different mechanism, I'd love to hear of it. :)

@disbelief

Thanks, I had figured as much but wanted to make sure I wasn't missing something obvious.

@sethjeffery

You could do something like this:

class GtExt
  def [](val)
    {:$gt => val}
  end
end

$gt = GtExt.new

People.where created_at: $gt[Time.now]

Or you could be really hacky and add it right into Kernel

module Kernel
  def GreaterThan(val)
    { :$gt => val }
  end
end

People.where created_at: GreaterThan Time.now

Right-wing coders might disapprove though. :smile:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.