Class: Esquema::Builder
- Inherits:
-
Object
- Object
- Esquema::Builder
- Defined in:
- lib/esquema/builder.rb
Overview
The Builder class is responsible for building a schema for an ActiveRecord model.
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#required_properties ⇒ Object
readonly
Returns the value of attribute required_properties.
Instance Method Summary collapse
-
#build_properties ⇒ Hash
Builds the properties for the schema.
-
#build_schema ⇒ Hash
Builds the schema for the ActiveRecord model.
-
#build_type ⇒ String
Builds the type for the schema.
-
#initialize(model) ⇒ Builder
constructor
A new instance of Builder.
-
#schema ⇒ Hash
The schema for the ActiveRecord model.
Constructor Details
#initialize(model) ⇒ Builder
Returns a new instance of Builder.
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
#model ⇒ Object (readonly)
Returns the value of attribute model.
9 10 11 |
# File 'lib/esquema/builder.rb', line 9 def model @model end |
#required_properties ⇒ Object (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_properties ⇒ Hash
Builds the properties for the schema.
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_schema ⇒ Hash
Builds the schema for the ActiveRecord model.
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_type ⇒ String
Builds the type for the schema.
50 51 52 |
# File 'lib/esquema/builder.rb', line 50 def build_type model.respond_to?(:type) ? model.type : "object" end |
#schema ⇒ Hash
Returns The schema for the ActiveRecord model.
33 34 35 |
# File 'lib/esquema/builder.rb', line 33 def schema build_schema end |