Class: Clearly::Query::Definition
- Inherits:
-
Object
- Object
- Clearly::Query::Definition
- Includes:
- Compose::Comparison, Compose::Core, Compose::Range, Compose::Special, Compose::Subset, Validate
- Defined in:
- lib/clearly/query/definition.rb
Overview
Validates and represents a model query specification definition.
Instance Attribute Summary collapse
-
#all_fields ⇒ Array<Symbol>
readonly
Available model fields.
-
#associations ⇒ Array<Hash>
readonly
Model associations hierarchy.
-
#associations_flat ⇒ Array<Hash>
readonly
Model associations flat array.
-
#defaults ⇒ Hash
readonly
Defaults.
-
#field_mappings ⇒ Array<Hash>
readonly
Mapped model fields.
-
#joins ⇒ Array<Array<Hash>>
readonly
Associations organised to calculate joins.
-
#model ⇒ ActiveRecord::Base
readonly
Active record model for this definition.
-
#table ⇒ Arel::Table
readonly
Arel table for this definition.
-
#text_fields ⇒ Array<Symbol>
readonly
Available text model fields.
Instance Method Summary collapse
-
#get_field_mapping(column_name) ⇒ Arel::Nodes::Node, ...
Build custom field from model mappings.
-
#initialize(opts) ⇒ Clearly::Query::Definition
constructor
Create a Definition.
Methods included from Validate
#like_syntax, #sanitize_like_value, #sanitize_similar_to_value, #validate_array, #validate_array_items, #validate_association, #validate_boolean, #validate_condition, #validate_definition, #validate_definition_instance, #validate_float, #validate_hash, #validate_integer, #validate_model, #validate_name, #validate_node_or_attribute, #validate_not_blank, #validate_query, #validate_spec_association, #validate_symbol, #validate_table, #validate_table_column
Methods included from Compose::Range
Constructor Details
#initialize(opts) ⇒ Clearly::Query::Definition
Create a Definition
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/clearly/query/definition.rb', line 46 def initialize(opts) opts = {model: nil, hash: nil, table: nil}.merge(opts) # two ways to go: model and hash, or table and joins result = nil result = create_from_model(opts[:model], opts[:hash]) unless opts[:model].nil? result = create_from_table(opts[:table]) if result.nil? && !opts[:table].nil? fail Clearly::Query::QueryArgumentError.new('could not build definition from options') if result.nil? result end |
Instance Attribute Details
#all_fields ⇒ Array<Symbol> (readonly)
Returns available model fields.
20 21 22 |
# File 'lib/clearly/query/definition.rb', line 20 def all_fields @all_fields end |
#associations ⇒ Array<Hash> (readonly)
Returns model associations hierarchy.
29 30 31 |
# File 'lib/clearly/query/definition.rb', line 29 def associations @associations end |
#associations_flat ⇒ Array<Hash> (readonly)
Returns model associations flat array.
32 33 34 |
# File 'lib/clearly/query/definition.rb', line 32 def associations_flat @associations_flat end |
#defaults ⇒ Hash (readonly)
Returns defaults.
38 39 40 |
# File 'lib/clearly/query/definition.rb', line 38 def defaults @defaults end |
#field_mappings ⇒ Array<Hash> (readonly)
Returns mapped model fields.
26 27 28 |
# File 'lib/clearly/query/definition.rb', line 26 def field_mappings @field_mappings end |
#joins ⇒ Array<Array<Hash>> (readonly)
Returns associations organised to calculate joins.
35 36 37 |
# File 'lib/clearly/query/definition.rb', line 35 def joins @joins end |
#model ⇒ ActiveRecord::Base (readonly)
Returns active record model for this definition.
14 15 16 |
# File 'lib/clearly/query/definition.rb', line 14 def model @model end |
#table ⇒ Arel::Table (readonly)
Returns arel table for this definition.
17 18 19 |
# File 'lib/clearly/query/definition.rb', line 17 def table @table end |
#text_fields ⇒ Array<Symbol> (readonly)
Returns available text model fields.
23 24 25 |
# File 'lib/clearly/query/definition.rb', line 23 def text_fields @text_fields end |
Instance Method Details
#get_field_mapping(column_name) ⇒ Arel::Nodes::Node, ...
Build custom field from model mappings
61 62 63 64 65 66 67 68 |
# File 'lib/clearly/query/definition.rb', line 61 def get_field_mapping(column_name) value = @field_mappings[column_name] if @field_mappings.keys.include?(column_name) && !value.blank? value else nil end end |