Module: ModelApi::Model::ClassMethods

Defined in:
lib/model-api/model.rb

Instance Method Summary collapse

Instance Method Details

#api_attributes(*args) ⇒ Object



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
# File 'lib/model-api/model.rb', line 20

def api_attributes(*args)
   = ModelApi::HashMetadata.(:attributes, self, args)
   = ModelApi::HashMetadata.(:attributes, superclass, )
  if args.present?
    id_attrs = []
    .each { |attr, | id_attrs << attr if [:id] }
    if id_attrs.present?
      id_attr_sets = id_attrs.map { |v| (v.is_a?(Array) ? v.flatten : [v]).map(&:to_sym) }
      existing_id_attr_sets = (api_model[:id_attributes] || [])
          .map { |v| (v.is_a?(Array) ? v.flatten : [v]).map(&:to_sym) }
      if (id_attr_sets - existing_id_attr_sets).present?
        api_model id_attributes: (id_attr_sets - existing_id_attr_sets).uniq
      end
    end
  end
  if self < ActiveRecord::Base && (args.present? ||
      !self.instance_variable_get(:@api_attrs_characterized))
    .each do |attr, |
      if (assoc = self.reflect_on_association(attr)).present?
        [:type] = :association
        [:association] = assoc
      else
        [:type] = :attribute
      end
    end
    self.instance_variable_set(:@api_attrs_characterized, true)
  end
  
end


50
51
52
53
# File 'lib/model-api/model.rb', line 50

def api_links(*args)
   = ModelApi::HashMetadata.(:links, self, args)
  ModelApi::HashMetadata.(:links, superclass, )
end

#api_model(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/model-api/model.rb', line 4

def api_model(*args)
   = ModelApi::SimpleMetadata.(:model, self, args,
      post_process: (lambda do ||
        if args.present? && .include?(:id_attributes)
          [:id_attributes] = [:id_attributes]
              .map { |v| (v.is_a?(Array) ? v.flatten : [v]).map(&:to_sym) }
        end
      end))
  ModelApi::SimpleMetadata.(:model, superclass, ,
      exclude_keys: [:alias])
end

#api_model_post_process_metadata(klass, metadata) ⇒ Object



16
17
18
# File 'lib/model-api/model.rb', line 16

def (klass, )

end