Class: JSONAPI::Serializable::Resource

Inherits:
Object
  • Object
show all
Extended by:
DSL
Defined in:
lib/jsonapi/serializable/resource.rb,
lib/jsonapi/serializable/resource/dsl.rb,
lib/jsonapi/serializable/resource/key_format.rb,
lib/jsonapi/serializable/resource/conditional_fields.rb

Defined Under Namespace

Modules: ConditionalFields, DSL, InstanceMethods, KeyFormat

Instance Method Summary collapse

Methods included from DSL

attribute, attributes, extended, id, inherited, link, meta, relationship, type

Constructor Details

#initialize(exposures = {}) ⇒ Resource

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jsonapi/serializable/resource.rb', line 18

def initialize(exposures = {})
  @_exposures = exposures
  @_exposures.each { |k, v| instance_variable_set("@#{k}", v) }

  @_id = instance_eval(&self.class.id_block).to_s
  @_type = if (b = self.class.type_block)
             instance_eval(&b).to_sym
           else
             self.class.type_val || :unknown
           end
  @_relationships = self.class.relationship_blocks
                        .each_with_object({}) do |(k, v), h|
    opts = self.class.relationship_options[k] || {}
    h[k] = Relationship.new(@_exposures, opts, &v)
  end
  @_meta = if (b = self.class.meta_block)
             instance_eval(&b)
           else
             self.class.meta_val
           end

  freeze
end

Instance Method Details

#as_jsonapi(fields: nil, include: []) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jsonapi/serializable/resource.rb', line 44

def as_jsonapi(fields: nil, include: [])
  attrs = requested_attributes(fields).each_with_object({}) do |(k, v), h|
    h[k] = instance_eval(&v)
  end
  rels = requested_relationships(fields)
         .each_with_object({}) do |(k, v), h|
    h[k] = v.as_jsonapi(include.include?(k))
  end
  links = link_blocks.each_with_object({}) do |(k, v), h|
    h[k] = Link.as_jsonapi(@_exposures, &v)
  end
  {}.tap do |hash|
    hash[:id]   = @_id
    hash[:type] = @_type
    hash[:attributes]    = attrs if attrs.any?
    hash[:relationships] = rels  if rels.any?
    hash[:links] = links if links.any?
    hash[:meta]  = @_meta  unless @_meta.nil?
  end
end

#jsonapi_cache_key(options) ⇒ Object



80
81
82
83
84
# File 'lib/jsonapi/serializable/resource.rb', line 80

def jsonapi_cache_key(options)
  "#{jsonapi_type} - #{jsonapi_id}" \
  "- #{options[:include].to_a.sort}" \
  "- #{(options[:fields] || Set.new).to_a.sort}"
end

#jsonapi_idObject



70
71
72
# File 'lib/jsonapi/serializable/resource.rb', line 70

def jsonapi_id
  @_id
end


74
75
76
77
78
# File 'lib/jsonapi/serializable/resource.rb', line 74

def jsonapi_related(include)
  @_relationships
    .select { |k, _| include.include?(k) }
    .each_with_object({}) { |(k, v), h| h[k] = v.related_resources }
end

#jsonapi_typeObject

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



66
67
68
# File 'lib/jsonapi/serializable/resource.rb', line 66

def jsonapi_type
  @_type
end