Module: JSONAPI::Serializable::Resource::DSL
- Included in:
- JSONAPI::Serializable::Resource
- Defined in:
- lib/jsonapi/serializable/resource/dsl.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#attribute(name, _options = {}, &block) ⇒ Object
Declare an attribute for this resource.
-
#attributes(*args) ⇒ Object
Declare a list of attributes for this resource.
-
#id(&block) ⇒ Object
Declare the JSON API id of this resource.
-
#inherited(klass) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#link(name, &block) ⇒ Object
Declare a link for this resource.
- #meta(value = nil, &block) ⇒ Object
-
#relationship(name, options = {}, &block) ⇒ Object
(also: #has_many, #has_one, #belongs_to)
Declare a relationship for this resource.
- #type(value = nil, &block) ⇒ Object
Class Method Details
.extended(klass) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 5 def self.extended(klass) class << klass attr_accessor :id_block, :type_val, :type_block, :attribute_blocks, :relationship_blocks, :relationship_options, :link_blocks, :meta_val, :meta_block end klass.attribute_blocks = {} klass.relationship_blocks = {} klass. = {} klass.link_blocks = {} end |
Instance Method Details
#attribute(name, _options = {}, &block) ⇒ Object
Declare an attribute for this resource.
67 68 69 70 |
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 67 def attribute(name, = {}, &block) block ||= proc { @object.public_send(name) } attribute_blocks[name.to_sym] = block end |
#attributes(*args) ⇒ Object
Declare a list of attributes for this resource.
78 79 80 81 82 |
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 78 def attributes(*args) args.each do |attr| attribute(attr) end end |
#id(&block) ⇒ Object
Declare the JSON API id of this resource.
38 39 40 |
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 38 def id(&block) self.id_block = block end |
#inherited(klass) ⇒ Object
rubocop:disable Metrics/AbcSize
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 19 def inherited(klass) klass.id_block = id_block klass.type_val = type_val klass.type_block = type_block klass.attribute_blocks = attribute_blocks.dup klass.relationship_blocks = relationship_blocks.dup klass. = .dup klass.link_blocks = link_blocks.dup klass. = klass. = end |
#link(name, &block) ⇒ Object
Declare a link for this resource. The properties of the link are set
by providing a block in which the DSL methods of
+JSONAPI::Serializable::Link+ are called, or the value of the link
is returned directly.
104 105 106 |
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 104 def link(name, &block) link_blocks[name] = block end |
#meta(value) ⇒ Object #meta(&block) ⇒ Object
122 123 124 125 |
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 122 def (value = nil, &block) self. = value self. = block end |
#relationship(name, options = {}, &block) ⇒ Object Also known as: has_many, has_one, belongs_to
Declare a relationship for this resource. The properties of the
relationship are set by providing a block in which the DSL methods
of +JSONAPI::Serializable::Relationship+ are called.
153 154 155 156 157 158 159 160 |
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 153 def relationship(name, = {}, &block) rel_block = proc do data { @object.public_send(name) } instance_eval(&block) unless block.nil? end relationship_blocks[name.to_sym] = rel_block [name.to_sym] = end |
#type(value) ⇒ Object #type(&block) ⇒ Object
55 56 57 58 |
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 55 def type(value = nil, &block) self.type_val = value.to_sym if value self.type_block = block end |