Class: Dbee::SchemaCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/dbee/schema_creator.rb

Overview

This creates a Dbee::Schema from a variety of different inputs:

  1. The hash representation of a schema.

  2. A Dbee::Base subclass (code based models).

For backward compatibility, tree based models are also supported in the following formats:

  1. The hash representation of a tree based model.

  2. Dbee::Model instances in tree based form (using the deprecated constraints and models attributes).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_or_model, query) ⇒ SchemaCreator

An ArgumentError is raised if the query “from” attribute differs from the name of the root model of a tree based model or if the “from” attribute is blank.

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dbee/schema_creator.rb', line 27

def initialize(schema_or_model, query)
  @orig_query = Query.make(query) || raise(ArgumentError, 'query is required')
  raise ArgumentError, 'a schema or model is required' unless schema_or_model

  @schema = make_schema(schema_or_model)

  # Note that for backward compatibility reasons, this validation does not
  # exist in the DBee::Query class. This allows continued support for
  # old callers who depend on the "from" field being inferred from the root
  # tree model name.
  raise ArgumentError, 'query requires a from model name' if expected_from_model.empty?

  validate_query_from_model!

  freeze
end

Instance Attribute Details

#schemaObject (readonly)

:nodoc:



23
24
25
# File 'lib/dbee/schema_creator.rb', line 23

def schema
  @schema
end

Instance Method Details

#queryObject

Returns a Dbee::Query instance with a “from” attribute which is sometimes derived for tree based models.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dbee/schema_creator.rb', line 46

def query
  return orig_query if expected_from_model == orig_query.from

  Query.new(
    from: expected_from_model,
    fields: orig_query.fields,
    filters: orig_query.filters,
    sorters: orig_query.sorters,
    limit: orig_query.limit
  )
end