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
- #exclude(param) ⇒ 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
- #schema ⇒ Object
- #update(param) ⇒ 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
86 87 88 89 90 91 92 93 94 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 86 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
100 101 102 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 100 def count(param) {count: self.result.count} end |
#default_order ⇒ Object
48 49 50 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 48 def default_order self.order(self.result.model.primary_key.to_s) end |
#exclude(param) ⇒ Object
57 58 59 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 57 def exclude(param) self.result.query_transformation(:exclude, param['field'].to_sym, param['value']) end |
#filter(param) ⇒ Object
61 62 63 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 61 def filter(param) self.result.query_transformation(:filter, param['field'].to_sym, param['value']) end |
#first(param) ⇒ Object
124 125 126 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 124 def first(param) self.default_order.first end |
#id_selector ⇒ Object
52 53 54 55 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 52 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
82 83 84 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 82 def instance(param) self.result.filter(self.id_selector => param).first end |
#last(param) ⇒ Object
128 129 130 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 128 def last(param) self.default_order.last end |
#map_ids(param) ⇒ Object
96 97 98 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 96 def map_ids(param) self.default_order.select_map(self.id_selector) end |
#order(param) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 73 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
119 120 121 122 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 119 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 |
#schema ⇒ Object
104 105 106 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 104 def schema Hash[self.result.db.schema(self.result.model.table_name)] end |
#update(param) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 108 def update(param) data = Hash[param.map do |field, value| field = field.to_sym [field, self.result.query_transformation(:update, field, value)] end] if not data.include?(:updated_at) and self.schema.include?(:updated_at) data[:updated_at] = DateTime.now end {updated_count: self.result.update(data)} end |
#validate(transformation) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 32 def validate(transformation) constraints = { exclude: :collection, filter: :collection, with: :collection, order: :collection, instance: :collection, context: :instance, map_ids: :collection, paginate: :collection, first: :collection, last: :collection, count: :collection, update: :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
65 66 67 68 69 70 71 |
# File 'lib/query-interface-server/transformations/sequel_transformer.rb', line 65 def with(param) args = [] if param.has_key?('param') args << param['param'] end self.result.query_transformation(:with, param['field'].to_sym, *args) end |