Class: Api::ResourceAttributeSet

Inherits:
Struct
  • Object
show all
Defined in:
lib/api/resource_attribute_set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes

Returns:

  • (Object)

    the current value of attributes



2
3
4
# File 'lib/api/resource_attribute_set.rb', line 2

def attributes
  @attributes
end

#includesObject

Returns the value of attribute includes

Returns:

  • (Object)

    the current value of includes



2
3
4
# File 'lib/api/resource_attribute_set.rb', line 2

def includes
  @includes
end

#methods_listObject

Returns the value of attribute methods_list

Returns:

  • (Object)

    the current value of methods_list



2
3
4
# File 'lib/api/resource_attribute_set.rb', line 2

def methods_list
  @methods_list
end

Class Method Details

.for(model_class, jattrs: nil) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/api/resource_attribute_set.rb', line 3

def self.for(model_class, jattrs: nil)
  jattrs ||= model_class.respond_to?(:json_attrs) ? (model_class.json_attrs || {}) : {}
  only = Array(jattrs[:only]).map(&:to_sym)
  if only.empty?
    except = Array(jattrs[:except]).map(&:to_sym)
    only = model_class.column_names.map(&:to_sym) - except
  end
  new(only.reject { |a| a == :id }, Array(jattrs[:methods]).map(&:to_sym), jattrs[:include])
end

Instance Method Details

#parsed_includesObject

Parse json_attrs into { assoc_name => spec_or_nil }. Handles both symbol items (:roles) and hash items (users: { only: [:id] }).



15
16
17
18
19
20
21
22
23
# File 'lib/api/resource_attribute_set.rb', line 15

def parsed_includes
  return {} unless includes
  Array(includes).each_with_object({}) do |item, hash|
    case item
    when Hash then item.each { |k, v| hash[k] = v }
    when Symbol then hash[item] = nil
    end
  end
end