Class: Paraphrase::Query

Inherits:
Object
  • Object
show all
Includes:
ActiveModel
Defined in:
lib/paraphrase/query.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveModel

#model_name, #persisted?, #to_key, #to_model, #to_param

Constructor Details

#initialize(query_params, relation = nil) ⇒ Query

Filters out parameters irrelevant to the query and sets the base scope for to begin the chain.

Parameters:

  • query_params (Hash)

    query parameters

  • relation (ActiveRecord::Relation) (defaults to: nil)

    relation object to apply methods to



103
104
105
106
107
108
109
# File 'lib/paraphrase/query.rb', line 103

def initialize(query_params, relation = nil)
  @params = filter_params(query_params || {})

  @result = mappings.inject(relation || default_relation) do |result, mapping|
    repository.chain(result, mapping, @params)
  end
end

Instance Attribute Details

#mappingsArray<Paraphrase::Mapping> (readonly)

Returns mappings for query.

Returns:



17
# File 'lib/paraphrase/query.rb', line 17

class_attribute :mappings, instance_writer: false

#paramsHashWithIndifferentAccess (readonly)

Returns filtered parameters based on keys defined in mappings.

Returns:

  • (HashWithIndifferentAccess)

    filtered parameters based on keys defined in mappings



27
28
29
# File 'lib/paraphrase/query.rb', line 27

def params
  @params
end

#resultObject (readonly)

Returns the value of attribute result.



27
# File 'lib/paraphrase/query.rb', line 27

attr_reader :params, :result

Class Method Details

.inherited(klass) ⇒ Object

Set mappings on inheritance to ensure they're unique per subclass



30
31
32
33
34
35
36
37
38
39
# File 'lib/paraphrase/query.rb', line 30

def self.inherited(klass)
  klass.mappings = []
  klass.source = klass.to_s.sub(/Query$/, '')

  klass.params_filter = Class.new(Paraphrase::ParamsFilter)
  klass.const_set(:ParamsFilter, klass.params_filter)

  klass.repository = Class.new(Paraphrase::Repository)
  klass.const_set(:Repository, klass.repository)
end

.keysArray<Symbol>

Keys mapped to scopes

Returns:

  • (Array<Symbol>)


44
45
46
# File 'lib/paraphrase/query.rb', line 44

def self.keys
  mappings.flat_map(&:keys)
end

.map(*keys, options) ⇒ Object

Add a Mapping instance to #mappings. Defines a reader for each key to read from #params.

Maps a key to a scope

Parameters:

  • keys (Array<Symbol>)

    query params to be mapped to the scope

  • options (Hash)

    options to configure Mapping instance

Options Hash (options):

  • :to (Symbol, Array<Symbol>)

    scope to map query params to

  • :whitelist (true, Symbol, Array<Symbol>)

    lists all or a subset of param keys as optional



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/paraphrase/query.rb', line 58

def self.map(*keys)
  options = keys.extract_options!
  scope_name = options[:to]

  if mappings.any? { |mapping| mapping.name == scope_name }
    raise DuplicateMappingError.new(scope_name)
  end

  mappings << Mapping.new(keys, options)

  keys.each do |key|
    define_method(key) { params[key] }

    params_filter.class_eval do
      define_method(key) { params[key] }
    end
  end
end

.param(query_param, &block) ⇒ Object

Define a method on ParamsFilter to process the raw value of the query param

Parameters:

  • query_param (Symbol)

    query param to process

  • block (Proc)

    block to process the specified query_param



82
83
84
85
86
# File 'lib/paraphrase/query.rb', line 82

def self.param(query_param, &block)
  params_filter.class_eval do
    define_method(query_param, &block)
  end
end

.scope(scope_name, &block) ⇒ Object

Define a scope on Repository

Parameters:

  • scope_name (Symbol)

    name of the scope specified in map

  • block (Proc)

    body of the scope to be defined



92
93
94
95
96
# File 'lib/paraphrase/query.rb', line 92

def self.scope(scope_name, &block)
  repository.class_eval do
    define_method(scope_name, &block)
  end
end

Instance Method Details

#[](key) ⇒ Object

Look up a params value

Parameters:

  • key (String, Symbol)

    key to read



114
115
116
117
118
119
120
# File 'lib/paraphrase/query.rb', line 114

def [](key)
  unless keys.include?(key.to_sym)
    raise UndefinedKeyError.new(key, keys)
  end

  params[key]
end

#default_relationActiveRecord::Relation

Return an ActiveRecord::Relation corresponding to the source class determined from the source class attribute that defaults to the name of the class.

Returns:

  • (ActiveRecord::Relation)


127
128
129
130
# File 'lib/paraphrase/query.rb', line 127

def default_relation
  klass = self.class.source.to_s.constantize
  klass.default_paraphrase_relation
end

#keysObject

See Also:



133
134
135
# File 'lib/paraphrase/query.rb', line 133

def keys
  self.class.keys
end