Class: SimpleJsonapi::Definition::Resource
- Includes:
- Concerns::HasLinksObject, Concerns::HasMetaObject
- Defined in:
- lib/simple_jsonapi/definition/resource.rb
Overview
Represents a single resource object.
Instance Attribute Summary collapse
- #attribute_definitions ⇒ Hash{Symbol => Attribute} readonly
- #id_definition ⇒ Proc readonly
- #relationship_definitions ⇒ Hash{Symbol => Relationship} readonly
- #type_definition ⇒ Proc readonly
Attributes included from Concerns::HasMetaObject
Attributes included from Concerns::HasLinksObject
Instance Method Summary collapse
- #attribute(name, **options) {|resource| ... } ⇒ void
- #has_many(name, description: nil, **options, &block) ⇒ void
- #has_one(name, description: nil, **options, &block) ⇒ void
- #id(*args) {|resource| ... } ⇒ void
-
#initialize ⇒ Resource
constructor
A new instance of Resource.
- #type(*args) {|resource| ... } ⇒ void
Methods included from Concerns::HasMetaObject
Methods included from Concerns::HasLinksObject
Constructor Details
#initialize ⇒ Resource
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_definitions ⇒ Hash{Symbol => Attribute} (readonly)
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, **, &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, **, &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, **, &block) relationship(name, cardinality: :singular, **, &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, **, &block) relationship(name, cardinality: :collection, **, &block) end private def relationship(name, **, &block) relationship_definitions[name.to_sym] = SimpleJsonapi::Definition::Relationship.new(name, , &block) end end |
#id_definition ⇒ Proc (readonly)
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, **, &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, **, &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, **, &block) relationship(name, cardinality: :singular, **, &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, **, &block) relationship(name, cardinality: :collection, **, &block) end private def relationship(name, **, &block) relationship_definitions[name.to_sym] = SimpleJsonapi::Definition::Relationship.new(name, , &block) end end |
#relationship_definitions ⇒ Hash{Symbol => Relationship} (readonly)
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, **, &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, **, &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, **, &block) relationship(name, cardinality: :singular, **, &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, **, &block) relationship(name, cardinality: :collection, **, &block) end private def relationship(name, **, &block) relationship_definitions[name.to_sym] = SimpleJsonapi::Definition::Relationship.new(name, , &block) end end |
#type_definition ⇒ Proc (readonly)
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, **, &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, **, &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, **, &block) relationship(name, cardinality: :singular, **, &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, **, &block) relationship(name, cardinality: :collection, **, &block) end private def relationship(name, **, &block) relationship_definitions[name.to_sym] = SimpleJsonapi::Definition::Relationship.new(name, , &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.
68 69 70 71 72 73 74 75 |
# File 'lib/simple_jsonapi/definition/resource.rb', line 68 def attribute(name, **, &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, **, &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.
95 96 97 |
# File 'lib/simple_jsonapi/definition/resource.rb', line 95 def has_many(name, **, &block) relationship(name, cardinality: :collection, **, &block) end |
#has_one(name, description: nil, **options, &block) ⇒ void
This method returns an undefined value.
84 85 86 |
# File 'lib/simple_jsonapi/definition/resource.rb', line 84 def has_one(name, **, &block) relationship(name, cardinality: :singular, **, &block) end |
#id(&block) ⇒ void #id(value) ⇒ void
This method returns an undefined value.
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.
56 57 58 |
# File 'lib/simple_jsonapi/definition/resource.rb', line 56 def type(*args, &block) @type_definition = wrap_in_proc(*args, &block) end |