Class: Buezli::Api::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/buezli/api/schema.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Schema

Returns a new instance of Schema.



10
11
12
13
# File 'lib/buezli/api/schema.rb', line 10

def initialize(config)
  @config = config.with_indifferent_access
  @version = @config.fetch(:version)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/buezli/api/schema.rb', line 4

def config
  @config
end

#versionObject (readonly)

Returns the value of attribute version.



4
5
6
# File 'lib/buezli/api/schema.rb', line 4

def version
  @version
end

Class Method Details

.load(path) ⇒ Object



6
7
8
# File 'lib/buezli/api/schema.rb', line 6

def self.load(path)
  new(JSON.parse(Pathname(path).read))
end

Instance Method Details

#association?(model_name, association) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/buezli/api/schema.rb', line 39

def association?(model_name, association)
  self.for(model_name)&.fetch(:associations, {})&.key?(association.to_s)
end

#association_class_name(model_name, association) ⇒ Object



47
48
49
50
# File 'lib/buezli/api/schema.rb', line 47

def association_class_name(model_name, association)
  (model_name, association)[:class_name] ||
    association.to_s.singularize.classify
end

#association_collection?(model_name, association) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/buezli/api/schema.rb', line 52

def association_collection?(model_name, association)
  (model_name, association)[:type].to_s.include?("many")
end

#association_metadata(model_name, association) ⇒ Object



43
44
45
# File 'lib/buezli/api/schema.rb', line 43

def (model_name, association)
  self.for(model_name)&.dig(:associations, association.to_s) || {}.with_indifferent_access
end

#attribute_type(model_name, attribute) ⇒ Object



35
36
37
# File 'lib/buezli/api/schema.rb', line 35

def attribute_type(model_name, attribute)
  self.for(model_name)&.dig(:attributes, attribute.to_s)
end

#declared_field?(model_name, field_name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/buezli/api/schema.rb', line 29

def declared_field?(model_name, field_name)
  model = self.for(model_name)
  model&.fetch(:attributes, {})&.key?(field_name.to_s) ||
    model&.fetch(:associations, {})&.key?(field_name.to_s)
end

#endpointsObject



15
16
17
# File 'lib/buezli/api/schema.rb', line 15

def endpoints
  config.fetch(:endpoints, {})
end

#for(model_name) ⇒ Object



19
20
21
# File 'lib/buezli/api/schema.rb', line 19

def for(model_name)
  config.dig(:models, model_name.to_s)
end

#model_name_for_resource(resource_name) ⇒ Object



23
24
25
26
27
# File 'lib/buezli/api/schema.rb', line 23

def model_name_for_resource(resource_name)
  config.fetch(:models).find do |_model_name, model|
    model[:resource_name] == resource_name.to_s
  end&.first
end