Module: Svelte::ModelFactory

Defined in:
lib/svelte/model_factory.rb,
lib/svelte/model_factory/parameter.rb

Overview

Note:

This module is supposed to be extended not used directly

Bridging the gap between an application and any external service that publishes its API as a Swagger JSON spec.

Defined Under Namespace

Classes: Parameter

Instance Method Summary collapse

Instance Method Details

#define_models(json) ⇒ Hash

Creates typed Ruby objects from JSON definitions. These definitions are found in the Swagger JSON spec as a top-level key, "definitions".



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/svelte/model_factory.rb', line 12

def define_models(json)
  return unless json
  models = {}
  model_definitions = json['definitions']
  model_definitions.each do |model_name, parameters|
    attributes = parameters['properties'].keys
    model = Class.new do
      attr_reader(*attributes.map(&:to_sym))

      parameters['properties'].each do |attribute, options|
        define_method("#{attribute}=", lambda do |value|
          if public_send(attribute).nil? || !public_send(attribute).present?
            permitted_values = options.fetch('enum', [])
            required = parameters.fetch('required', []).include?(attribute)
            instance_variable_set(
              "@#{attribute}",
              Parameter.new(options['type'],
                            permitted_values: permitted_values,
                            required: required)
            )
          end

          instance_variable_get("@#{attribute}").value = value
        end)
      end
    end

    define_initialize_on(model: model)
    define_attributes_on(model: model)
    define_required_attributes_on(model: model)
    define_json_for_model_on(model: model)
    define_nested_models_on(model: model)
    define_as_json_on(model: model)
    define_to_json_on(model: model)
    define_validate_on(model: model)
    define_valid_on(model: model)

    model.instance_variable_set('@json_for_model', parameters.freeze)

    constant_name_for_model = StringManipulator.constant_name_for(model_name)
    models[constant_name_for_model] = model
  end

  models.each do |model_name, model|
    const_set(model_name, model)
  end
end

#define_models_from_file(file) ⇒ Hash

Creates typed Ruby objects from JSON File definitions. These definitions are found in the Swagger JSON spec as a top-level key, "models".



72
73
74
# File 'lib/svelte/model_factory.rb', line 72

def define_models_from_file(file)
  define_models_from_json_string(File.read(file)) if file
end

#define_models_from_json_string(string) ⇒ Hash

Creates typed Ruby objects from JSON String definitions. These definitions are found in the Swagger JSON spec as a top-level key, "models".



64
65
66
# File 'lib/svelte/model_factory.rb', line 64

def define_models_from_json_string(string)
  define_models(JSON.parse(string)) if string
end