Class: SimpleJsonapi::Definition::Resource

Inherits:
Base
  • Object
show all
Includes:
Concerns::HasLinksObject, Concerns::HasMetaObject
Defined in:
lib/simple_jsonapi/definition/resource.rb

Overview

Represents a single resource object.

Instance Attribute Summary collapse

Attributes included from Concerns::HasMetaObject

#meta_definitions

Attributes included from Concerns::HasLinksObject

#link_definitions

Instance Method Summary collapse

Methods included from Concerns::HasMetaObject

#meta

Methods included from Concerns::HasLinksObject

#link

Constructor Details

#initializeResource

Returns a new instance of Resource.



17
18
19
20
21
22
23
24
25
26
# File 'lib/simple_jsonapi/definition/resource.rb', line 17

def initialize
  super
  @id_definition = wrap_in_proc(&:id)
  @type_definition = wrap_in_proc do |resource|
    resource.class.name.demodulize.underscore.pluralize
  end

  @attribute_definitions = {}
  @relationship_definitions = {}
end

Instance Attribute Details

#attribute_definitionsHash{Symbol => Attribute} (readonly)

Returns:



11
12
13
14
15
16
17
18
19
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/simple_jsonapi/definition/resource.rb', line 11

class SimpleJsonapi::Definition::Resource < SimpleJsonapi::Definition::Base
  include SimpleJsonapi::Definition::Concerns::HasLinksObject
  include SimpleJsonapi::Definition::Concerns::HasMetaObject

  attr_reader :id_definition, :type_definition, :attribute_definitions, :relationship_definitions

  def initialize
    super
    @id_definition = wrap_in_proc(&:id)
    @type_definition = wrap_in_proc do |resource|
      resource.class.name.demodulize.underscore.pluralize
    end

    @attribute_definitions = {}
    @relationship_definitions = {}
  end

  private def initialize_dup(new_def)
    super
    new_def.instance_variable_set(:@id_definition, @id_definition.dup) unless @id_definition.nil?
    new_def.instance_variable_set(:@type_definition, @type_definition.dup) unless @type_definition.nil?

    unless @attribute_definitions.nil?
      new_def.instance_variable_set(:@attribute_definitions, @attribute_definitions.dup)
    end

    unless @relationship_definitions.nil?
      new_def.instance_variable_set(:@relationship_definitions, @relationship_definitions.dup)
    end
  end

  # @overload id(&block)
  # @overload id(value)
  # @yieldparam resource [Object]
  # @yieldreturn [String]
  # @return [void]
  def id(*args, &block)
    @id_definition = wrap_in_proc(*args, &block)
  end

  # @overload type(&block)
  # @overload type(value)
  # @yieldparam resource [Object]
  # @yieldreturn [String]
  # @return [void]
  def type(*args, &block)
    @type_definition = wrap_in_proc(*args, &block)
  end

  # @overload attribute(name, type: nil, description: nil, **options)
  # @overload attribute(name, type: nil, description: nil, **options, &block)
  # @option (see Definition::Base#initialize)
  # @option options [Symbol] type data type
  # @option options [String] description
  # @yieldparam resource [Object]
  # @yieldreturn [#to_json] the value
  # @return [void]
  def attribute(name, **options, &block)
    # Allow type attribute to be defined before throwing error to support non-compliant data_comleteness/v1
    attribute_definitions[name.to_sym] = SimpleJsonapi::Definition::Attribute.new(name, **options, &block)

    if %w[id type].include?(name.to_s)
      raise ArgumentError, "`#{name}` is not allowed as an attribute name"
    end
  end

  # @overload has_one(name, description: nil, **options, &block)
  # @param name [Symbol]
  # @option (see Definition::Relationship#initialize)
  # @option options [String] description
  # @yieldparam (see Definition::Relationship#initialize)
  # @yieldreturn (see Definition::Relationship#initialize)
  # @return [void]
  def has_one(name, **options, &block)
    relationship(name, cardinality: :singular, **options, &block)
  end

  # @overload has_many(name, description: nil, **options, &block)
  # @param name [Symbol]
  # @option (see Definition::Relationship#initialize)
  # @option options [String] description
  # @yieldparam (see Definition::Relationship#initialize)
  # @yieldreturn (see Definition::Relationship#initialize)
  # @return [void]
  def has_many(name, **options, &block)
    relationship(name, cardinality: :collection, **options, &block)
  end

  private

  def relationship(name, **options, &block)
    relationship_definitions[name.to_sym] = SimpleJsonapi::Definition::Relationship.new(name, options, &block)
  end
end

#id_definitionProc (readonly)

Returns:

  • (Proc)


11
12
13
14
15
16
17
18
19
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/simple_jsonapi/definition/resource.rb', line 11

class SimpleJsonapi::Definition::Resource < SimpleJsonapi::Definition::Base
  include SimpleJsonapi::Definition::Concerns::HasLinksObject
  include SimpleJsonapi::Definition::Concerns::HasMetaObject

  attr_reader :id_definition, :type_definition, :attribute_definitions, :relationship_definitions

  def initialize
    super
    @id_definition = wrap_in_proc(&:id)
    @type_definition = wrap_in_proc do |resource|
      resource.class.name.demodulize.underscore.pluralize
    end

    @attribute_definitions = {}
    @relationship_definitions = {}
  end

  private def initialize_dup(new_def)
    super
    new_def.instance_variable_set(:@id_definition, @id_definition.dup) unless @id_definition.nil?
    new_def.instance_variable_set(:@type_definition, @type_definition.dup) unless @type_definition.nil?

    unless @attribute_definitions.nil?
      new_def.instance_variable_set(:@attribute_definitions, @attribute_definitions.dup)
    end

    unless @relationship_definitions.nil?
      new_def.instance_variable_set(:@relationship_definitions, @relationship_definitions.dup)
    end
  end

  # @overload id(&block)
  # @overload id(value)
  # @yieldparam resource [Object]
  # @yieldreturn [String]
  # @return [void]
  def id(*args, &block)
    @id_definition = wrap_in_proc(*args, &block)
  end

  # @overload type(&block)
  # @overload type(value)
  # @yieldparam resource [Object]
  # @yieldreturn [String]
  # @return [void]
  def type(*args, &block)
    @type_definition = wrap_in_proc(*args, &block)
  end

  # @overload attribute(name, type: nil, description: nil, **options)
  # @overload attribute(name, type: nil, description: nil, **options, &block)
  # @option (see Definition::Base#initialize)
  # @option options [Symbol] type data type
  # @option options [String] description
  # @yieldparam resource [Object]
  # @yieldreturn [#to_json] the value
  # @return [void]
  def attribute(name, **options, &block)
    # Allow type attribute to be defined before throwing error to support non-compliant data_comleteness/v1
    attribute_definitions[name.to_sym] = SimpleJsonapi::Definition::Attribute.new(name, **options, &block)

    if %w[id type].include?(name.to_s)
      raise ArgumentError, "`#{name}` is not allowed as an attribute name"
    end
  end

  # @overload has_one(name, description: nil, **options, &block)
  # @param name [Symbol]
  # @option (see Definition::Relationship#initialize)
  # @option options [String] description
  # @yieldparam (see Definition::Relationship#initialize)
  # @yieldreturn (see Definition::Relationship#initialize)
  # @return [void]
  def has_one(name, **options, &block)
    relationship(name, cardinality: :singular, **options, &block)
  end

  # @overload has_many(name, description: nil, **options, &block)
  # @param name [Symbol]
  # @option (see Definition::Relationship#initialize)
  # @option options [String] description
  # @yieldparam (see Definition::Relationship#initialize)
  # @yieldreturn (see Definition::Relationship#initialize)
  # @return [void]
  def has_many(name, **options, &block)
    relationship(name, cardinality: :collection, **options, &block)
  end

  private

  def relationship(name, **options, &block)
    relationship_definitions[name.to_sym] = SimpleJsonapi::Definition::Relationship.new(name, options, &block)
  end
end

#relationship_definitionsHash{Symbol => Relationship} (readonly)

Returns:



11
12
13
14
15
16
17
18
19
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/simple_jsonapi/definition/resource.rb', line 11

class SimpleJsonapi::Definition::Resource < SimpleJsonapi::Definition::Base
  include SimpleJsonapi::Definition::Concerns::HasLinksObject
  include SimpleJsonapi::Definition::Concerns::HasMetaObject

  attr_reader :id_definition, :type_definition, :attribute_definitions, :relationship_definitions

  def initialize
    super
    @id_definition = wrap_in_proc(&:id)
    @type_definition = wrap_in_proc do |resource|
      resource.class.name.demodulize.underscore.pluralize
    end

    @attribute_definitions = {}
    @relationship_definitions = {}
  end

  private def initialize_dup(new_def)
    super
    new_def.instance_variable_set(:@id_definition, @id_definition.dup) unless @id_definition.nil?
    new_def.instance_variable_set(:@type_definition, @type_definition.dup) unless @type_definition.nil?

    unless @attribute_definitions.nil?
      new_def.instance_variable_set(:@attribute_definitions, @attribute_definitions.dup)
    end

    unless @relationship_definitions.nil?
      new_def.instance_variable_set(:@relationship_definitions, @relationship_definitions.dup)
    end
  end

  # @overload id(&block)
  # @overload id(value)
  # @yieldparam resource [Object]
  # @yieldreturn [String]
  # @return [void]
  def id(*args, &block)
    @id_definition = wrap_in_proc(*args, &block)
  end

  # @overload type(&block)
  # @overload type(value)
  # @yieldparam resource [Object]
  # @yieldreturn [String]
  # @return [void]
  def type(*args, &block)
    @type_definition = wrap_in_proc(*args, &block)
  end

  # @overload attribute(name, type: nil, description: nil, **options)
  # @overload attribute(name, type: nil, description: nil, **options, &block)
  # @option (see Definition::Base#initialize)
  # @option options [Symbol] type data type
  # @option options [String] description
  # @yieldparam resource [Object]
  # @yieldreturn [#to_json] the value
  # @return [void]
  def attribute(name, **options, &block)
    # Allow type attribute to be defined before throwing error to support non-compliant data_comleteness/v1
    attribute_definitions[name.to_sym] = SimpleJsonapi::Definition::Attribute.new(name, **options, &block)

    if %w[id type].include?(name.to_s)
      raise ArgumentError, "`#{name}` is not allowed as an attribute name"
    end
  end

  # @overload has_one(name, description: nil, **options, &block)
  # @param name [Symbol]
  # @option (see Definition::Relationship#initialize)
  # @option options [String] description
  # @yieldparam (see Definition::Relationship#initialize)
  # @yieldreturn (see Definition::Relationship#initialize)
  # @return [void]
  def has_one(name, **options, &block)
    relationship(name, cardinality: :singular, **options, &block)
  end

  # @overload has_many(name, description: nil, **options, &block)
  # @param name [Symbol]
  # @option (see Definition::Relationship#initialize)
  # @option options [String] description
  # @yieldparam (see Definition::Relationship#initialize)
  # @yieldreturn (see Definition::Relationship#initialize)
  # @return [void]
  def has_many(name, **options, &block)
    relationship(name, cardinality: :collection, **options, &block)
  end

  private

  def relationship(name, **options, &block)
    relationship_definitions[name.to_sym] = SimpleJsonapi::Definition::Relationship.new(name, options, &block)
  end
end

#type_definitionProc (readonly)

Returns:

  • (Proc)


11
12
13
14
15
16
17
18
19
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/simple_jsonapi/definition/resource.rb', line 11

class SimpleJsonapi::Definition::Resource < SimpleJsonapi::Definition::Base
  include SimpleJsonapi::Definition::Concerns::HasLinksObject
  include SimpleJsonapi::Definition::Concerns::HasMetaObject

  attr_reader :id_definition, :type_definition, :attribute_definitions, :relationship_definitions

  def initialize
    super
    @id_definition = wrap_in_proc(&:id)
    @type_definition = wrap_in_proc do |resource|
      resource.class.name.demodulize.underscore.pluralize
    end

    @attribute_definitions = {}
    @relationship_definitions = {}
  end

  private def initialize_dup(new_def)
    super
    new_def.instance_variable_set(:@id_definition, @id_definition.dup) unless @id_definition.nil?
    new_def.instance_variable_set(:@type_definition, @type_definition.dup) unless @type_definition.nil?

    unless @attribute_definitions.nil?
      new_def.instance_variable_set(:@attribute_definitions, @attribute_definitions.dup)
    end

    unless @relationship_definitions.nil?
      new_def.instance_variable_set(:@relationship_definitions, @relationship_definitions.dup)
    end
  end

  # @overload id(&block)
  # @overload id(value)
  # @yieldparam resource [Object]
  # @yieldreturn [String]
  # @return [void]
  def id(*args, &block)
    @id_definition = wrap_in_proc(*args, &block)
  end

  # @overload type(&block)
  # @overload type(value)
  # @yieldparam resource [Object]
  # @yieldreturn [String]
  # @return [void]
  def type(*args, &block)
    @type_definition = wrap_in_proc(*args, &block)
  end

  # @overload attribute(name, type: nil, description: nil, **options)
  # @overload attribute(name, type: nil, description: nil, **options, &block)
  # @option (see Definition::Base#initialize)
  # @option options [Symbol] type data type
  # @option options [String] description
  # @yieldparam resource [Object]
  # @yieldreturn [#to_json] the value
  # @return [void]
  def attribute(name, **options, &block)
    # Allow type attribute to be defined before throwing error to support non-compliant data_comleteness/v1
    attribute_definitions[name.to_sym] = SimpleJsonapi::Definition::Attribute.new(name, **options, &block)

    if %w[id type].include?(name.to_s)
      raise ArgumentError, "`#{name}` is not allowed as an attribute name"
    end
  end

  # @overload has_one(name, description: nil, **options, &block)
  # @param name [Symbol]
  # @option (see Definition::Relationship#initialize)
  # @option options [String] description
  # @yieldparam (see Definition::Relationship#initialize)
  # @yieldreturn (see Definition::Relationship#initialize)
  # @return [void]
  def has_one(name, **options, &block)
    relationship(name, cardinality: :singular, **options, &block)
  end

  # @overload has_many(name, description: nil, **options, &block)
  # @param name [Symbol]
  # @option (see Definition::Relationship#initialize)
  # @option options [String] description
  # @yieldparam (see Definition::Relationship#initialize)
  # @yieldreturn (see Definition::Relationship#initialize)
  # @return [void]
  def has_many(name, **options, &block)
    relationship(name, cardinality: :collection, **options, &block)
  end

  private

  def relationship(name, **options, &block)
    relationship_definitions[name.to_sym] = SimpleJsonapi::Definition::Relationship.new(name, options, &block)
  end
end

Instance Method Details

#attribute(name, type: nil, description: nil, **options) ⇒ void #attribute(name, type: nil, description: nil, **options, &block) ⇒ void

This method returns an undefined value.

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • type (Symbol)

    data type

  • description (String)
  • if (Proc<Boolean>)
  • unless (Proc<Boolean>)

Yield Parameters:

  • resource (Object)

Yield Returns:

  • (#to_json)

    the value



68
69
70
71
72
73
74
75
# File 'lib/simple_jsonapi/definition/resource.rb', line 68

def attribute(name, **options, &block)
  # Allow type attribute to be defined before throwing error to support non-compliant data_comleteness/v1
  attribute_definitions[name.to_sym] = SimpleJsonapi::Definition::Attribute.new(name, **options, &block)

  if %w[id type].include?(name.to_s)
    raise ArgumentError, "`#{name}` is not allowed as an attribute name"
  end
end

#has_many(name, description: nil, **options, &block) ⇒ void

This method returns an undefined value.

Parameters:

  • name (Symbol)
  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • description (String)
  • if (Proc<Boolean>)
  • unless (Proc<Boolean>)

Yield Parameters:

  • resource (Object)

Yield Returns:

  • (Object, Array<Object>)

    The related resource or resources



95
96
97
# File 'lib/simple_jsonapi/definition/resource.rb', line 95

def has_many(name, **options, &block)
  relationship(name, cardinality: :collection, **options, &block)
end

#has_one(name, description: nil, **options, &block) ⇒ void

This method returns an undefined value.

Parameters:

  • name (Symbol)
  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • description (String)
  • if (Proc<Boolean>)
  • unless (Proc<Boolean>)

Yield Parameters:

  • resource (Object)

Yield Returns:

  • (Object, Array<Object>)

    The related resource or resources



84
85
86
# File 'lib/simple_jsonapi/definition/resource.rb', line 84

def has_one(name, **options, &block)
  relationship(name, cardinality: :singular, **options, &block)
end

#id(&block) ⇒ void #id(value) ⇒ void

This method returns an undefined value.

Yield Parameters:

  • resource (Object)

Yield Returns:

  • (String)


47
48
49
# File 'lib/simple_jsonapi/definition/resource.rb', line 47

def id(*args, &block)
  @id_definition = wrap_in_proc(*args, &block)
end

#type(&block) ⇒ void #type(value) ⇒ void

This method returns an undefined value.

Yield Parameters:

  • resource (Object)

Yield Returns:

  • (String)


56
57
58
# File 'lib/simple_jsonapi/definition/resource.rb', line 56

def type(*args, &block)
  @type_definition = wrap_in_proc(*args, &block)
end