Class: Esquema::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/esquema/builder.rb

Overview

The Builder class is responsible for building a schema for an ActiveRecord model.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Builder

Returns a new instance of Builder.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
# File 'lib/esquema/builder.rb', line 11

def initialize(model)
  raise ArgumentError, "Class is not an ActiveRecord model" unless model.ancestors.include? ActiveRecord::Base

  @model = model
  @properties = {}
  @required_properties = []
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



9
10
11
# File 'lib/esquema/builder.rb', line 9

def model
  @model
end

#required_propertiesObject (readonly)

Returns the value of attribute required_properties.



9
10
11
# File 'lib/esquema/builder.rb', line 9

def required_properties
  @required_properties
end

Instance Method Details

#build_propertiesHash

Builds the properties for the schema.

Returns:

  • (Hash)

    The built properties.



40
41
42
43
44
45
# File 'lib/esquema/builder.rb', line 40

def build_properties
  add_properties_from_columns
  add_properties_from_associations
  add_virtual_properties
  @properties
end

#build_schemaHash

Builds the schema for the ActiveRecord model.

Returns:

  • (Hash)

    The built schema.



22
23
24
25
26
27
28
29
30
# File 'lib/esquema/builder.rb', line 22

def build_schema
  @build_schema ||= {
    title: build_title,
    description: build_description,
    type: build_type,
    properties: build_properties,
    required: required_properties
  }.compact
end

#build_typeString

Builds the type for the schema.

Returns:

  • (String)

    The built type.



50
51
52
# File 'lib/esquema/builder.rb', line 50

def build_type
  model.respond_to?(:type) ? model.type : "object"
end

#schemaHash

Returns The schema for the ActiveRecord model.

Returns:

  • (Hash)

    The schema for the ActiveRecord model.



33
34
35
# File 'lib/esquema/builder.rb', line 33

def schema
  build_schema
end