Class: Clearly::Query::Definition

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#parse_interval, #parse_range

Constructor Details

#initialize(opts) ⇒ Clearly::Query::Definition

Create a Definition

Parameters:

  • opts (Hash)

    the options to create a message with.

Options Hash (opts):

  • :model (ActiveRecord::Base) — default: nil

    the ActiveRecord model

  • :hash (Hash) — default: nil

    the model definition hash

  • :table (Arel::Table) — default: nil

    the arel table


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_fieldsArray<Symbol> (readonly)

Returns available model fields

Returns:

  • (Array<Symbol>)

    available model fields


20
21
22
# File 'lib/clearly/query/definition.rb', line 20

def all_fields
  @all_fields
end

#associationsArray<Hash> (readonly)

Returns model associations hierarchy

Returns:

  • (Array<Hash>)

    model associations hierarchy


29
30
31
# File 'lib/clearly/query/definition.rb', line 29

def associations
  @associations
end

#associations_flatArray<Hash> (readonly)

Returns model associations flat array

Returns:

  • (Array<Hash>)

    model associations flat array


32
33
34
# File 'lib/clearly/query/definition.rb', line 32

def associations_flat
  @associations_flat
end

#defaultsHash (readonly)

Returns defaults

Returns:

  • (Hash)

    defaults


38
39
40
# File 'lib/clearly/query/definition.rb', line 38

def defaults
  @defaults
end

#field_mappingsArray<Hash> (readonly)

Returns mapped model fields

Returns:

  • (Array<Hash>)

    mapped model fields


26
27
28
# File 'lib/clearly/query/definition.rb', line 26

def field_mappings
  @field_mappings
end

#joinsArray<Array<Hash>> (readonly)

Returns associations organised to calculate joins

Returns:

  • (Array<Array<Hash>>)

    associations organised to calculate joins


35
36
37
# File 'lib/clearly/query/definition.rb', line 35

def joins
  @joins
end

#modelActiveRecord::Base (readonly)

Returns active record model for this definition

Returns:

  • (ActiveRecord::Base)

    active record model for this definition


14
15
16
# File 'lib/clearly/query/definition.rb', line 14

def model
  @model
end

#tableArel::Table (readonly)

Returns arel table for this definition

Returns:

  • (Arel::Table)

    arel table for this definition


17
18
19
# File 'lib/clearly/query/definition.rb', line 17

def table
  @table
end

#text_fieldsArray<Symbol> (readonly)

Returns available text model fields

Returns:

  • (Array<Symbol>)

    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

Parameters:

  • column_name (Symbol)

Returns:

  • (Arel::Nodes::Node, Arel::Attributes::Attribute, String)

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