Class: QueryInterface::Server::Transformations::SequelTransformer
- Inherits:
-
Object
- Object
- QueryInterface::Server::Transformations::SequelTransformer
- Defined in:
- lib/query-interface-server/transformations/sequel_transformer.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
Returns the value of attribute model.
-
#result ⇒ Object
Returns the value of attribute result.
Instance Method Summary collapse
- #context(param) ⇒ Object
- #count(param) ⇒ Object
- #default_order ⇒ Object
- #filter(param) ⇒ Object
- #first(param) ⇒ Object
- #id_selector ⇒ Object
-
#initialize(dataset) ⇒ SequelTransformer
constructor
A new instance of SequelTransformer.
- #instance(param) ⇒ Object
- #last(param) ⇒ Object
- #map_ids(param) ⇒ Object
- #order(param) ⇒ Object
- #paginate(param) ⇒ Object
- #result_type ⇒ Object
- #run(transformations, add_default_order = true) ⇒ Object
- #validate(transformation) ⇒ Object
- #with(param) ⇒ Object
Constructor Details
#initialize(dataset) ⇒ SequelTransformer
Returns a new instance of SequelTransformer.
7 8 9 10 11 12 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 7 def initialize(dataset) if !dataset.instance_variable_get(:@opts)[:select] dataset = dataset.select_all(dataset.model.table_name) end self.result = dataset end |
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
5 6 7 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 5 def model @model end |
#result ⇒ Object
Returns the value of attribute result.
5 6 7 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 5 def result @result end |
Instance Method Details
#context(param) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 81 def context(param) name = "#{param}_dataset" if self.result.respond_to?(name) result = self.result.send(name) else raise Exception, "method #{name} not found" end result.select_all(result.model.table_name) end |
#count(param) ⇒ Object
95 96 97 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 95 def count(param) {count: self.result.count} end |
#default_order ⇒ Object
47 48 49 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 47 def default_order self.order(self.result.model.primary_key.to_s) end |
#filter(param) ⇒ Object
56 57 58 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 56 def filter(param) self.result.query_transformation(:filter, param['field'].to_sym, param['value']) end |
#first(param) ⇒ Object
104 105 106 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 104 def first(param) self.default_order.first end |
#id_selector ⇒ Object
51 52 53 54 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 51 def id_selector primary_key = self.result.model.primary_key ? self.result.model.primary_key : :id Sequel.qualify(self.result.model.table_name, primary_key) end |
#instance(param) ⇒ Object
77 78 79 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 77 def instance(param) self.result.filter(self.id_selector => param).first end |
#last(param) ⇒ Object
108 109 110 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 108 def last(param) self.default_order.last end |
#map_ids(param) ⇒ Object
91 92 93 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 91 def map_ids(param) self.default_order.select_map(self.id_selector) end |
#order(param) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 68 def order(param) direction = :asc if param.starts_with?('-') param = param[1..-1] direction = :desc end self.result.query_transformation(:order, param.to_sym, direction) end |
#paginate(param) ⇒ Object
99 100 101 102 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 99 def paginate(param) context = self.default_order {total: context.count, objects: context.paginate(param['page'].to_i, param['per_page'].to_i)} end |
#result_type ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 22 def result_type if self.result.is_a?(Sequel::Dataset) :collection elsif self.result.is_a?(Sequel::Model) :instance else :atomic end end |
#run(transformations, add_default_order = true) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 14 def run(transformations, add_default_order=true) transformations.each do |transformation| method, param = self.validate(transformation) self.result = self.send(method, param) end (add_default_order && self.result_type == :collection ? self.default_order : self.result) end |
#validate(transformation) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 32 def validate(transformation) constraints = { filter: :collection, with: :collection,order: :collection, instance: :collection, context: :instance, map_ids: :collection, paginate: :collection, first: :collection, last: :collection, count: :collection } method = transformation['transformation'].to_sym raise UnknownTransformation, "#{method}" unless constraints.has_key?(method) param = transformation['parameter'] unless self.result_type == constraints[method] raise InvalidContextException, "expected: #{description[:accepts]}, got: #{self.result_type}" end return [method, param] end |
#with(param) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 60 def with(param) args = [] if param.has_key?('param') args << param['param'] end self.result.query_transformation(:with, param['field'].to_sym, *args) end |