Module: Chronicle::Models::Generation::ClassMethods

Defined in:
lib/chronicle/models/generation.rb

Overview

Methods for generating models from the schema

Instance Method Summary collapse

Instance Method Details

#enable_benchmarkingObject



69
70
71
# File 'lib/chronicle/models/generation.rb', line 69

def enable_benchmarking
  Chronicle::Models::Generation.benchmark_enabled = true
end

#extant_modelsObject



26
27
28
29
30
31
# File 'lib/chronicle/models/generation.rb', line 26

def extant_models
  constants.select do |constant_name|
    constant = const_get(constant_name)
    constant.is_a?(Class) && constant.superclass == Chronicle::Models::Base
  end
end

#generate_models(graph = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chronicle/models/generation.rb', line 33

def generate_models(graph = nil)
  graph ||= begin
    require 'json'
    schema_path = File.join(File.dirname(__FILE__), '..', '..', '..', 'schema', 'chronicle_schema_v1.json')
    Chronicle::Schema::SchemaGraph.build_from_json(JSON.parse(File.read(schema_path)))
  end

  start_time = Time.now
  graph.types.each do |klass|
    type_id = graph.id_to_identifier(klass.id)

    new_model_klass = Chronicle::Models::ModelFactory.new(
      type_id: type_id.to_sym,
      properties: klass.all_properties
    ).generate

    const_set(type_id, new_model_klass)
  end
  end_time = Time.now
  duration_ms = (end_time - start_time) * 1000
  handle_benchmark_data(graph.types.length, duration_ms) if Chronicle::Models::Generation.benchmark_enabled

  Chronicle::Models::Generation.models_generated = true
end

#models_generated?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/chronicle/models/generation.rb', line 22

def models_generated?
  Chronicle::Models::Generation.models_generated
end

#unload_modelsObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/chronicle/models/generation.rb', line 58

def unload_models
  constants.each do |constant_name|
    constant = const_get(constant_name)
    if constant.is_a?(Class) && constant.superclass == Chronicle::Models::Base
      send(:remove_const,
        constant_name)
    end
  end
  Chronicle::Models::Generation.models_generated = false
end