Sha256: 92561ee5143a9ee839eb8cc3a7a677343e8a6af0e00bc4150eabb3aeb358021c
Contents?: true
Size: 1.1 KB
Versions: 1
Compression:
Stored size: 1.1 KB
Contents
require 'rubygems' module MissileEmitter module Searchable # 搜索条件(klass => {field: scope}) # eg. {Person => {name_like: scope, older_than: scope}} conditions = {} MissileEmitter do |klass, key, *, &block| (conditions[klass] ||= {}.with_indifferent_access)[key] = block end has_underline_method = Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0') define_method :search do |hash| hash.reduce all do |relation, (key, value)| next relation if value.blank? # ignore empty value if filter = conditions.fetch(self, {})[key] relation.extending do # Just for fun :) With Ruby >= 2.7 you can use _1 instead of _. define_method(:_) { value } unless has_underline_method # If RUBY_VERSION < 2.7.0, polyfill the _1 convenient method alias_method :_1, :_ end end.instance_exec(value, &filter) elsif column_names.include?(key.to_s) relation.where key => value else relation end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
missile_emitter-0.3.18 | lib/missile_emitter/searchable.rb |