Module: JSONAPI::Materializer::Resource

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Model
Defined in:
lib/jsonapi/materializer/resource.rb,
lib/jsonapi/materializer/resource/relation.rb,
lib/jsonapi/materializer/resource/attribute.rb,
lib/jsonapi/materializer/resource/relationship.rb,
lib/jsonapi/materializer/resource/configuration.rb

Defined Under Namespace

Classes: Attribute, Configuration, Relation, Relationship

Constant Summary collapse

MIXIN_HOOK =
lambda do |*|
  @attributes = {}
  @relations = {}

  unless const_defined?(:Collection)
    self::Collection = Class.new do
      include(JSONAPI::Materializer::Collection)
    end
  end

  validates_presence_of(:object, allow_blank: true)

  origin(JSONAPI::Materializer.configuration.default_origin)
  identifier(JSONAPI::Materializer.configuration.default_identifier)

  has(JSONAPI::Materializer.configuration.default_identifier)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#includesObject



96
97
98
# File 'lib/jsonapi/materializer/resource.rb', line 96

def includes
  @includes || []
end

#objectObject

Returns the value of attribute object.



32
33
34
# File 'lib/jsonapi/materializer/resource.rb', line 32

def object
  @object
end

#rawObject (readonly)

Returns the value of attribute raw.



35
36
37
# File 'lib/jsonapi/materializer/resource.rb', line 35

def raw
  @raw
end

#selectsObject



92
93
94
# File 'lib/jsonapi/materializer/resource.rb', line 92

def selects
  (@selects || {}).transform_values { |list| list.map(&:to_sym) }
end

Instance Method Details

#as_dataObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jsonapi/materializer/resource.rb', line 45

def as_data
  {
    attributes: exposed(attributes.except(:id))
      .transform_values { |attribute| object.public_send(attribute.from) },
    relationships: relations
      .transform_values { |relation| relation.using(self).as_json }
  }.transform_values(&:presence).compact.merge(
    id:,
    type:,
    links: {
      self: links_self
      # TODO: Add more links
    }
  )
end

#as_jsonObject

rubocop:enable Metrics/AbcSize



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jsonapi/materializer/resource.rb', line 62

def as_json(*)
  {
    included:
  }.transform_values(&:presence).compact.merge(
    data: as_data,
    links: {
      self: links_self
      # TODO: Add more links
    }
  )
end

#attribute(name) ⇒ Object



78
79
80
# File 'lib/jsonapi/materializer/resource.rb', line 78

def attribute(name)
  self.class.attribute(name)
end

#initialize(**keyword_arguments) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/jsonapi/materializer/resource.rb', line 37

def initialize(**keyword_arguments)
  super(**keyword_arguments)

  @raw = keyword_arguments

  validate!
end


86
87
88
89
90
# File 'lib/jsonapi/materializer/resource.rb', line 86

def links_self
  Addressable::Template.new(
    "#{origin}/#{type}/#{object.public_send(identifier)}"
  ).pattern
end

#relation(name) ⇒ Object



82
83
84
# File 'lib/jsonapi/materializer/resource.rb', line 82

def relation(name)
  self.class.relation(name)
end

#typeObject



74
75
76
# File 'lib/jsonapi/materializer/resource.rb', line 74

def type
  self.class.configuration.type.to_s
end