Class: Paraphrase::Query
- Inherits:
-
Object
- Object
- Paraphrase::Query
- Includes:
- ActiveModel
- Defined in:
- lib/paraphrase/query.rb
Instance Attribute Summary collapse
-
#mappings ⇒ Array<Paraphrase::Mapping>
readonly
Mappings for query.
-
#params ⇒ HashWithIndifferentAccess
readonly
Filtered parameters based on keys defined in
mappings. -
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
-
.inherited(klass) ⇒ Object
Set
mappingson inheritance to ensure they're unique per subclass. -
.keys ⇒ Array<Symbol>
Keys mapped to scopes.
- .map(*keys, options) ⇒ Object
-
.param(query_param, &block) ⇒ Object
Define a method on
ParamsFilterto process the raw value of the query param. -
.scope(scope_name, &block) ⇒ Object
Define a scope on
Repository.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Look up a
paramsvalue. -
#default_relation ⇒ ActiveRecord::Relation
Return an
ActiveRecord::Relationcorresponding to the source class determined from thesourceclass attribute that defaults to the name of the class. -
#initialize(query_params, relation = nil) ⇒ Query
constructor
Filters out parameters irrelevant to the query and sets the base scope for to begin the chain.
- #keys ⇒ Object
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.
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
#mappings ⇒ Array<Paraphrase::Mapping> (readonly)
Returns mappings for query.
17 |
# File 'lib/paraphrase/query.rb', line 17 class_attribute :mappings, instance_writer: false |
#params ⇒ HashWithIndifferentAccess (readonly)
Returns filtered parameters based on keys
defined in mappings.
27 28 29 |
# File 'lib/paraphrase/query.rb', line 27 def params @params end |
#result ⇒ Object (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 |
.keys ⇒ Array<Symbol>
Keys mapped to scopes
44 45 46 |
# File 'lib/paraphrase/query.rb', line 44 def self.keys mappings.flat_map(&:keys) end |
.map(*keys, options) ⇒ Object
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) = keys. scope_name = [:to] if mappings.any? { |mapping| mapping.name == scope_name } raise DuplicateMappingError.new(scope_name) end mappings << Mapping.new(keys, ) 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
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
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
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_relation ⇒ ActiveRecord::Relation
Return an ActiveRecord::Relation corresponding to the source class
determined from the source class attribute that defaults to the name of
the class.
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 |
#keys ⇒ Object
133 134 135 |
# File 'lib/paraphrase/query.rb', line 133 def keys self.class.keys end |