class OpenAPIRest::QueryBuilder

Rest Query Builder

Attributes

api_model[R]
fields[R]
limit[R]
offset[R]
openapi_path[R]
params[R]
query[R]
sort[R]

Public Class Methods

new(api_model, params) click to toggle source
# File lib/openapi_rest/query_builder.rb, line 15
def initialize(api_model, params)
  @fields = params.fetch(:fields, '')
  @offset = params.fetch(:offset, 0)
  @limit = params.fetch(:limit, 10)
  @sort = params[:sort]
  @embed = params[:embed]
  @query = params.fetch(:query, {})
  @openapi_path = params.fetch(:openapi_path)
  @single = params[:operation] == :squery
  @params = params
  @api_model = api_model

  set_fields

  unless creating?
    [OpenAPIRest::Operations::Filter.new(self),
     OpenAPIRest::Operations::Sort.new(self),
     OpenAPIRest::Operations::Paginate.new(self)].each { |operations| operations.execute }
  end
end

Public Instance Methods

entity() click to toggle source
# File lib/openapi_rest/query_builder.rb, line 50
def entity
  @api_model.type.to_s.downcase.pluralize
end
raw_model() click to toggle source
# File lib/openapi_rest/query_builder.rb, line 54
def raw_model
  @api_model.model
end
resource() click to toggle source
# File lib/openapi_rest/query_builder.rb, line 46
def resource
  entity.to_s.singularize
end
response() click to toggle source
# File lib/openapi_rest/query_builder.rb, line 36
def response
  @response ||= OpenAPIRest::QueryResponse.new(self)
  @response
end
single?()
Alias for: single_result?
single_result?() click to toggle source
# File lib/openapi_rest/query_builder.rb, line 41
def single_result?
  creating? || @single
end
Also aliased as: single?

Private Instance Methods

creating?() click to toggle source
# File lib/openapi_rest/query_builder.rb, line 60
def creating?
  @params[:operation] == :create
end
set_fields() click to toggle source
# File lib/openapi_rest/query_builder.rb, line 64
def set_fields
  permitted = OpenAPIRest::ApiParameters.new(api_model: @api_model,
                                             openapi_path: @openapi_path).response_permitted_params
  @fields = fields.length > 0 ? fields.split(',').select { |s| permitted.include?(s.to_sym) } : permitted
end