0.0
No commit activity in last 3 years
No release in over 3 years
Predicates and conditions that extends to the ActiveRecord 3
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0

Runtime

 Project Readme

Sakuramochi

Build Status Dependency Status

Sakuramochi is a minimal extensions for Active Record 4.

This is similar to the ar-extensions gem.

Installation

gem 'sakuramochi'
# gem 'sakuramochi', :git => 'git://github.com/mashiro/sakuramochi.git'

Getting started

User.where(:name_contains => 'ai')
# => "SELECT "users".* FROM "users"  WHERE ("users"."name" LIKE '%ai%')"

User.where(:name_contains_any => ['ru', 'ai'])
# => "SELECT "users".* FROM "users"  WHERE (("users"."name" LIKE '%ru%' OR "users"."name" LIKE '%ai%'))"

User.where(:name_contains_all => ['ru', 'ai'])
# => "SELECT "users".* FROM "users"  WHERE (("users"."name" LIKE '%ru%' AND "users"."name" LIKE '%ai%'))"

Predicates

  • contains
  • starts_with, start_with
  • ends_with, end_with
  • in
  • eq, equal, equals
  • gt
  • gte, gteq
  • lt
  • lte, lteq

Conditions

expression = term {("and"|"or") term}
term = ["not"] factor
factor = array | hash | "(" expression ")"
User.where([{:name_contains => 'harune'}, :and, {:name_contains => 'aira'}]) 
# => "SELECT "users".* FROM "users"  WHERE (("users"."name" LIKE '%harune%') AND ("users"."name" LIKE '%aira%'))"

User.where([:not, {:name_end_with => "mion"}])
# => SELECT "users".* FROM "users" WHERE (NOT (("users"."name" LIKE '%mion')))

User.where([:not, :'(', {:name_contains => 'aira'}, :or, {:name_contains => 'rizumu'}, :')', :and, {:age_gte => 14}])
# => SELECT "users".* FROM "users" WHERE (NOT ((("users"."name" LIKE '%aira%') OR ("users"."name" LIKE '%rizumu%'))) AND ("users"."age" >= 14))

Configration

Sakuramochi.configure do |config|
  # simple
  config.add :eq_amamiya,
    :arel_predicate => :eq,
    :converter => proc { |v| "amamiya #{v}" }

  # advanced
  config.add :surrounds_with, :surrounds_with_alias,
    :arel_predicate => :matches,
    :grouping => true,
    :expand => false,
    :converter => proc { |v| "#{v.first}%#{v.last}" },
    :validator => proc { |v| v.is_a?(Enumerable) && v.to_a.size == 2 }
end

User.where(:name_eq_amamiya => 'rizumu')
# => "SELECT "users".* FROM "users"  WHERE "users"."name" = 'amamiya rizumu'"

User.where(:name_surrounds_with => ["ama", "umu"])
# => "SELECT "users".* FROM "users"  WHERE ("users"."name" LIKE 'ama%umu')"

Copyright

Copyright (c) 2011 mashiro