Class: Jason::ApiModel

Inherits:
Object
  • Object
show all
Defined in:
lib/jason/api_model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ApiModel

Returns a new instance of ApiModel.



9
10
11
12
# File 'lib/jason/api_model.rb', line 9

def initialize(name)
  @name = name
  @model = OpenStruct.new(Jason.schema[name.to_sym])
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



3
4
5
# File 'lib/jason/api_model.rb', line 3

def model
  @model
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/jason/api_model.rb', line 3

def name
  @name
end

Class Method Details

.configure(models) ⇒ Object



5
6
7
# File 'lib/jason/api_model.rb', line 5

def self.configure(models)
  @@models = models
end

Instance Method Details

#allowed_object_paramsObject



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

def allowed_object_params
  model.allowed_object_params || []
end

#allowed_paramsObject



14
15
16
# File 'lib/jason/api_model.rb', line 14

def allowed_params
  model.allowed_params || []
end

#as_json_configObject



46
47
48
# File 'lib/jason/api_model.rb', line 46

def as_json_config
  { only: subscribed_fields, methods: include_methods }
end

#include_methodsObject



22
23
24
# File 'lib/jason/api_model.rb', line 22

def include_methods
  model.include_methods || []
end

#permit(params) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/jason/api_model.rb', line 38

def permit(params)
  params = params.require(:payload).permit(allowed_params).tap do |allowed|
    allowed_object_params.each do |key|
      allowed[key] = params[:payload][key].to_unsafe_h if params[:payload][key]
    end
  end
end

#priority_scopeObject



26
27
28
# File 'lib/jason/api_model.rb', line 26

def priority_scope
  model.priority_scope || []
end

#scopeObject



34
35
36
# File 'lib/jason/api_model.rb', line 34

def scope
  model.scope
end

#subscribed_fieldsObject



30
31
32
# File 'lib/jason/api_model.rb', line 30

def subscribed_fields
  model.subscribed_fields || []
end