Class: ApiMaker::SelectParser

Inherits:
ApplicationService show all
Defined in:
app/services/api_maker/select_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationService

#api_maker_json

Constructor Details

#initialize(select:) ⇒ SelectParser

Returns a new instance of SelectParser.



4
5
6
# File 'app/services/api_maker/select_parser.rb', line 4

def initialize(select:)
  @select = select
end

Instance Attribute Details

#selectObject (readonly)

Returns the value of attribute select.



2
3
4
# File 'app/services/api_maker/select_parser.rb', line 2

def select
  @select
end

Instance Method Details

#performObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/api_maker/select_parser.rb', line 8

def perform
  new_select = {}

  select.each do |model_collection_name, attributes|
    model_class = model_collection_name.underscore.singularize.camelize
    resource = "Resources::#{model_class}Resource".safe_constantize
    raise "Resource not found for: #{model_collection_name}" unless resource

    selects = {}
    new_select[resource.model_class] ||= selects
    resource_attributes = resource._attributes_with_string_keys

    attributes.each do |attribute|
      resource_attribute = resource_attributes[attribute]
      raise "Attribute not found on the #{resource.short_name} resource: #{attribute}" unless resource_attribute

      selects[attribute.to_sym] = resource_attribute
    end

    new_select[resource.model_class] = selects
  end

  succeed! new_select
end