Class: SchemaSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_serializer.rb,
lib/schema_serializer/errors.rb,
lib/schema_serializer/schema.rb,
lib/schema_serializer/railtie.rb,
lib/schema_serializer/version.rb,
lib/schema_serializer/definition.rb,
lib/schema_serializer/constantizer.rb,
lib/schema_serializer/serializable.rb,
lib/schema_serializer/configuration.rb

Defined Under Namespace

Modules: Generators, Serializable Classes: BaseError, Configuration, Constantizer, Definition, NullValue, Railtie, ReloadError, RequiredNotDefined, Schema, SchemaNotFound

Constant Summary collapse

VERSION =
"0.2.7".freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ SchemaSerializer

Returns a new instance of SchemaSerializer.



45
46
47
48
# File 'lib/schema_serializer.rb', line 45

def initialize(object, options = {})
  @object = object
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



66
67
68
69
70
# File 'lib/schema_serializer.rb', line 66

def method_missing(name, *args, &block)
  super unless object.respond_to?(name)

  object.public_send(name, *args, &block)
end

Class Attribute Details

.definitionObject

Returns the value of attribute definition.



18
19
20
# File 'lib/schema_serializer.rb', line 18

def definition
  @definition
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



15
16
17
# File 'lib/schema_serializer.rb', line 15

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/schema_serializer.rb', line 15

def options
  @options
end

Class Method Details

.configObject



40
41
42
# File 'lib/schema_serializer.rb', line 40

def config
  @config ||= Configuration.new
end

.load_definition(path) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/schema_serializer.rb', line 20

def load_definition(path)
  doc = YamlExt.load(path)
  if doc.has_key?("openapi") && Gem::Version.new(doc.fetch("openapi")) >= Gem::Version.new("3.0.0")
    doc = doc.fetch("components").fetch("schemas")
  end

  @document_path = path
  self.definition = doc
end

.reload!Object

Raises:



30
31
32
33
34
# File 'lib/schema_serializer.rb', line 30

def reload!
  raise ReloadError, "Load config using .load_definition" if @document_path.nil?

  load_definition(@document_path)
end

Instance Method Details

#as_json(_options = nil) ⇒ Object



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

def as_json(_options = nil)
  schema.serialize(self)
end

#schemaObject



54
55
56
# File 'lib/schema_serializer.rb', line 54

def schema
  ::SchemaSerializer.definition.schema(schema_name)
end

#schema_nameObject



58
59
60
61
62
# File 'lib/schema_serializer.rb', line 58

def schema_name
  return self.class.name.sub("Serializer", "") if self.class < SchemaSerializer

  object.class.name
end