lib/mobility/backends/sequel/container/jsonb_query_methods.rb in mobility-0.5.1 vs lib/mobility/backends/sequel/container/jsonb_query_methods.rb in mobility-0.6.0
- old
+ new
@@ -4,31 +4,38 @@
Sequel.extension :pg_json, :pg_json_ops
module Mobility
module Backends
- class Sequel::Container::JsonbQueryMethods < Sequel::QueryMethods
- include Sequel::PgQueryMethods
- attr_reader :column_name
+ module Sequel
+ class Container::JsonbQueryMethods < QueryMethods
+ include PgQueryMethods
+ attr_reader :column_name
- def initialize(attributes, options)
- super
- @column_name = options[:column_name]
- define_query_methods
- end
+ def initialize(attributes, options)
+ super
+ @column_name = options[:column_name]
+ define_query_methods
+ end
- private
+ def matches(key, locale)
+ build_op(column_name)[locale][key.to_s]
+ end
- def matches(key, value, locale)
- build_op(column_name)[locale][key.to_s] =~ value.to_json
- end
+ def exists(key, locale)
+ build_op(column_name).has_key?(locale) & build_op(column_name)[locale].has_key?(key.to_s)
+ end
- def has_locale(key, locale)
- build_op(column_name).has_key?(locale) & build_op(column_name)[locale].has_key?(key.to_s)
- end
+ def quote(value)
+ value && value.to_json
+ end
- def build_op(key)
- ::Sequel.pg_jsonb_op(key)
+ private
+
+ def build_op(key)
+ ::Sequel.pg_jsonb_op(key)
+ end
end
+ Container.private_constant :JsonbQueryMethods
end
end
end