Class: JsonApiServer::FilterConfig
- Inherits:
-
Object
- Object
- JsonApiServer::FilterConfig
- Defined in:
- lib/json_api_server/filter_config.rb
Overview
Configuration for a filter jsonapi.org/format/#fetching-filtering.
Example filter configuration:
= [
{ id: { type: 'Integer' } },
{ tags: { builder: :pg_jsonb_ilike_array } },
:body,
{ title: { wildcard: :right }},
{ search: { builder: :model_query, method: :search } },
{ created: { col_name: :created_at, type: 'DateTime' } }
]
Instance Attribute Summary collapse
-
#attr ⇒ Object
readonly
Attribute used in queries.
-
#builder ⇒ Object
readonly
(optional) Symbol - the builder class to use for all queries for the attribute.
-
#column_name ⇒ Object
readonly
(optional) If the fitler name is not the same as the database column name, map it.
-
#comparison ⇒ Object
readonly
(optional) Symbol - the builder class to use for ‘=’, ‘<’, ‘>’, ‘>=’, ‘<=’, ‘=’, ‘!<’, ‘!>’, ‘<>’ queries.
-
#default ⇒ Object
readonly
(optional) Symbol - the builder class to use for all other queries.
-
#in ⇒ Object
readonly
(optional) Symbol - the builder class to use for IN queries.
-
#like ⇒ Object
readonly
(optional) Symbol - the builder class to use for LIKE queries.
-
#method ⇒ Object
readonly
(optional) Use with ModelQuery builder which calls a class method on the model.
-
#type ⇒ Object
readonly
(optional) Data type.
-
#wildcard ⇒ Object
readonly
(optional) Symbol - :left, :right or :none.
Class Method Summary collapse
-
.default_type ⇒ Object
Default data type is String unless a filter config specifies.
Instance Method Summary collapse
-
#initialize(config) ⇒ FilterConfig
constructor
A new instance of FilterConfig.
Constructor Details
#initialize(config) ⇒ FilterConfig
Returns a new instance of FilterConfig.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/json_api_server/filter_config.rb', line 45 def initialize(config) if config.respond_to?(:keys) # i.e, c.filter_options = { permitted: [{created: {attr: :created_at, type: DateTime}}] } key, value = config.first @attr = key @column_name = value[:col_name] || @attr @type = value[:type] || self.class.default_type @like = value[:like] @in = value[:in] @comparison = value[:comparison] @default = value[:default] @builder = value[:builder] @wildcard = value[:wildcard] @method = value[:method] else # i.e., c.filter_options = { permitted: [:body] } @attr = @column_name = config @type = self.class.default_type end end |
Instance Attribute Details
#attr ⇒ Object (readonly)
Attribute used in queries. i.e., /path?filter=bar => foo
17 18 19 |
# File 'lib/json_api_server/filter_config.rb', line 17 def attr @attr end |
#builder ⇒ Object (readonly)
(optional) Symbol - the builder class to use for all queries for the attribute.
38 39 40 |
# File 'lib/json_api_server/filter_config.rb', line 38 def builder @builder end |
#column_name ⇒ Object (readonly)
(optional) If the fitler name is not the same as the database column name, map it. i.e., map :created to :created_at. { created: { col_name: :created_at, type: ‘DateTime’ } }
21 22 23 |
# File 'lib/json_api_server/filter_config.rb', line 21 def column_name @column_name end |
#comparison ⇒ Object (readonly)
(optional) Symbol - the builder class to use for ‘=’, ‘<’, ‘>’, ‘>=’, ‘<=’, ‘=’, ‘!<’, ‘!>’, ‘<>’ queries. Defaults to :sql_comparison.
33 34 35 |
# File 'lib/json_api_server/filter_config.rb', line 33 def comparison @comparison end |
#default ⇒ Object (readonly)
(optional) Symbol - the builder class to use for all other queries. Defaults to :sql_eql.
36 37 38 |
# File 'lib/json_api_server/filter_config.rb', line 36 def default @default end |
#in ⇒ Object (readonly)
(optional) Symbol - the builder class to use for IN queries. Defaults to :sql_in if not specified.
30 31 32 |
# File 'lib/json_api_server/filter_config.rb', line 30 def in @in end |
#like ⇒ Object (readonly)
(optional) Symbol - the builder class to use for LIKE queries. Defaults to :sql_like if not specified.
27 28 29 |
# File 'lib/json_api_server/filter_config.rb', line 27 def like @like end |
#method ⇒ Object (readonly)
(optional) Use with ModelQuery builder which calls a class method on the model.
40 41 42 |
# File 'lib/json_api_server/filter_config.rb', line 40 def method @method end |
#type ⇒ Object (readonly)
(optional) Data type. Specify data type class as string. i.e., ‘String’, ‘DateTime’, ‘Time’, ‘BigDecimal’, etc. Defaults to ‘String’.
24 25 26 |
# File 'lib/json_api_server/filter_config.rb', line 24 def type @type end |
#wildcard ⇒ Object (readonly)
(optional) Symbol - :left, :right or :none. Defaults to wildcarding beginning end of string, i.e., “%#value%”,
43 44 45 |
# File 'lib/json_api_server/filter_config.rb', line 43 def wildcard @wildcard end |
Class Method Details
.default_type ⇒ Object
Default data type is String unless a filter config specifies.
67 68 69 |
# File 'lib/json_api_server/filter_config.rb', line 67 def self.default_type String end |