Skip to content

Commit

Permalink
Refactor using Plugins::Sequel::Query
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Jun 8, 2018
1 parent 7a604fa commit 7d6284c
Show file tree
Hide file tree
Showing 22 changed files with 225 additions and 355 deletions.
36 changes: 25 additions & 11 deletions lib/mobility/backends/sequel.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
module Mobility
module Backends
module Sequel
def setup_query_methods(query_methods)
setup do |attributes, options|
extend(Module.new do
define_method ::Mobility.query_method do
super().with_extend(query_methods.new(attributes, options))
end
end)
end
end

def self.included(backend_class)
backend_class.include(Backend)
backend_class.extend(self)
backend_class.extend(ClassMethods)
end

module ClassMethods
# @param [Symbol] name Attribute name
# @param [Symbol] locale Locale
def [](name, locale)
build_op(name.to_s, locale)
end

# @param [String] _attr Attribute name
# @param [Symbol] _locale Locale
# @return Op for this translated attribute
def build_op(_attr, _locale)
raise NotImplementedError
end

# @param [Sequel::Dataset] dataset Dataset to prepare
# @param [Object] predicate Predicate
# @param [Symbol] locale Locale
# @param [String] query_method
# @return [Sequel::Dataset] Prepared dataset
def prepare_dataset(dataset, _predicate, _locale, _query_method)
dataset
end
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/mobility/backends/sequel/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class Sequel::Column
include Sequel
include Column

require 'mobility/backends/sequel/column/query_methods'

# @!group Backend Accessors
# @!macro backend_reader
def read(locale, _options = nil)
Expand All @@ -34,7 +32,10 @@ def each_locale
available_locales.each { |l| yield(l) if present?(l) }
end

setup_query_methods(QueryMethods)
def self.build_op(attr, locale)
::Sequel::SQL::QualifiedIdentifier.new(model_class.table_name,
Column.column_name_for(attr, locale))
end

private

Expand Down
29 changes: 0 additions & 29 deletions lib/mobility/backends/sequel/column/query_methods.rb

This file was deleted.

30 changes: 18 additions & 12 deletions lib/mobility/backends/sequel/container.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# frozen_string_literal: true
require "mobility/backends/sequel/json"
require "mobility/backends/sequel/jsonb"

module Mobility
module Backends
=begin
Expand All @@ -8,9 +12,6 @@ module Backends
class Sequel::Container
include Sequel

require 'mobility/backends/sequel/container/json_query_methods'
require 'mobility/backends/sequel/container/jsonb_query_methods'

# @!method column_name
# @return [Symbol] (:translations) Name of translations column
option_reader :column_name
Expand Down Expand Up @@ -58,8 +59,6 @@ def each_locale
end
end

backend_class = self

setup do |attributes, options|
column_name = options[:column_name]
before_validation = Module.new do
Expand All @@ -76,13 +75,6 @@ def each_locale

plugin :defaults_setter
attributes.each { |attribute| default_values[attribute.to_sym] = {} }

query_methods = backend_class.const_get("#{options[:column_type].capitalize}QueryMethods")
extend(Module.new do
define_method ::Mobility.query_method do
super().with_extend(query_methods.new(attributes, options))
end
end)
end

private
Expand All @@ -104,6 +96,20 @@ def set_attribute_translation(locale, value)
end

class InvalidColumnType < StandardError; end

def self.build_op(attr, locale)
klass = const_get("#{options[:column_type].upcase}Op")
klass.new(klass.new(column_name.to_sym)[locale.to_s]).get_text(attr)
end

class JSONOp < ::Sequel::Postgres::JSONOp; end

class JSONBOp < Jsonb::JSONBOp
def to_question
left = @value.args[0].value
JSONBOp === left ? ::Sequel.&(super, left.to_question) : super
end
end
end
end
end
11 changes: 8 additions & 3 deletions lib/mobility/backends/sequel/hstore.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'mobility/backends/sequel/pg_hash'

Sequel.extension :pg_hstore, :pg_hstore_ops

module Mobility
module Backends
=begin
Expand All @@ -11,8 +13,6 @@ module Backends
=end
module Sequel
class Hstore < PgHash
require 'mobility/backends/sequel/hstore/query_methods'

# @!group Backend Accessors
# @!macro backend_reader
# @!method read(locale, options = {})
Expand All @@ -24,7 +24,12 @@ def write(locale, value, options = {})
end
# @!endgroup

setup_query_methods(QueryMethods)
def self.build_op(attr, locale)
column_name = column_affix % attr
HStoreOp.new(column_name.to_sym)[locale.to_s]
end

class HStoreOp < ::Sequel::Postgres::HStoreOp; end
end
end
end
Expand Down
34 changes: 0 additions & 34 deletions lib/mobility/backends/sequel/hstore/query_methods.rb

This file was deleted.

11 changes: 8 additions & 3 deletions lib/mobility/backends/sequel/json.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'mobility/backends/sequel/pg_hash'

Sequel.extension :pg_json, :pg_json_ops

module Mobility
module Backends
=begin
Expand All @@ -11,8 +13,6 @@ module Backends
=end
module Sequel
class Json < PgHash
require 'mobility/backends/sequel/json/query_methods'

# @!group Backend Accessors
#
# @!method read(locale, options = {})
Expand All @@ -31,7 +31,12 @@ class Json < PgHash
# @return [String,Integer,Boolean] Updated value
# @!endgroup

setup_query_methods(QueryMethods)
def self.build_op(attr, locale)
column_name = column_affix % attr
JSONOp.new(column_name.to_sym).get_text(locale.to_s)
end

class JSONOp < ::Sequel::Postgres::JSONOp; end
end
end
end
Expand Down
34 changes: 0 additions & 34 deletions lib/mobility/backends/sequel/json/query_methods.rb

This file was deleted.

35 changes: 32 additions & 3 deletions lib/mobility/backends/sequel/jsonb.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true
require 'mobility/backends/sequel/pg_hash'

Sequel.extension :pg_json, :pg_json_ops

module Mobility
module Backends
=begin
Expand All @@ -11,8 +14,6 @@ module Backends
=end
module Sequel
class Jsonb < PgHash
require 'mobility/backends/sequel/jsonb/query_methods'

# @!group Backend Accessors
#
# @!method read(locale, **options)
Expand All @@ -31,7 +32,35 @@ class Jsonb < PgHash
# @return [String,Integer,Boolean] Updated value
# @!endgroup

setup_query_methods(QueryMethods)
def self.build_op(attr, locale)
column_name = column_affix % attr
JSONBOp.new(column_name.to_sym).get_text(locale.to_s)
end

class JSONBOp < ::Sequel::Postgres::JSONBOp
def to_dash_arrow
column = @value.args[0].value
locale = @value.args[1]
::Sequel.pg_jsonb_op(column)[locale]
end

def to_question
column = @value.args[0].value
locale = @value.args[1]
::Sequel.pg_jsonb_op(column).has_key?(locale)
end

def =~(other)
case other
when Integer, Hash
to_dash_arrow =~ other.to_json
when NilClass
~to_question
else
super
end
end
end
end
end
end
Expand Down
34 changes: 0 additions & 34 deletions lib/mobility/backends/sequel/jsonb/query_methods.rb

This file was deleted.

2 changes: 0 additions & 2 deletions lib/mobility/backends/sequel/key_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ def self.configure(options)
include Mobility::Sequel::ColumnChanges.new(attributes)
end

setup_query_methods(QueryMethods)

# Returns translation for a given locale, or initializes one if none is present.
# @param [Symbol] locale
# @return [Mobility::Sequel::TextTranslation,Mobility::Sequel::StringTranslation]
Expand Down
Loading

0 comments on commit 7d6284c

Please sign in to comment.