Class: CouchbaseOrm::JsonSchema::Loader

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/couchbase-orm/json_schema/loader.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

JSON_SCHEMAS_PATH =
'db/cborm_schemas'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_schemas_path = JSON_SCHEMAS_PATH) ⇒ Loader

Returns a new instance of Loader.



14
15
16
17
18
19
20
# File 'lib/couchbase-orm/json_schema/loader.rb', line 14

def initialize(json_schemas_path = JSON_SCHEMAS_PATH)
  @schemas_directory = json_schemas_path
  @schemas = {}
  unless File.directory?(schemas_directory)
    CouchbaseOrm.logger.info { "Directory not found #{schemas_directory}" }
  end
end

Instance Attribute Details

#schemasObject (readonly)

Returns the value of attribute schemas.



12
13
14
# File 'lib/couchbase-orm/json_schema/loader.rb', line 12

def schemas
  @schemas
end

Instance Method Details

#extract_type(entity = {}) ⇒ Object



22
23
24
# File 'lib/couchbase-orm/json_schema/loader.rb', line 22

def extract_type(entity = {})
  entity[:type]
end

#get_json_schema!(entity, schema_path: nil) ⇒ Object

Raises:



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/couchbase-orm/json_schema/loader.rb', line 26

def get_json_schema!(entity, schema_path: nil)
  document_type = extract_type!(entity)

  return schemas[document_type] if schemas.key?(document_type)

  schema_path ||= File.join(schemas_directory, "#{document_type}.json")

  raise(Error, "Schema not found for #{document_type} in #{schema_path}") unless File.exist?(schema_path)

  schemas[document_type] = File.read schema_path
end