Class: SimpleJsonapi::Definition::Attribute
- Defined in:
- lib/simple_jsonapi/definition/attribute.rb
Overview
Represents a single attribute in a resource’s attributes
collection
Instance Attribute Summary collapse
- #array ⇒ Boolean readonly
- #data_type ⇒ Symbol readonly
- #description ⇒ String readonly
- #name ⇒ Symbol readonly
Instance Method Summary collapse
-
#initialize(name, *args, type: nil, array: false, description: nil, **options) {|resource| ... } ⇒ Attribute
constructor
A new instance of Attribute.
Constructor Details
#initialize(name, type: nil, description: nil, **options, &block) ⇒ Attribute #initialize(name, value, type: nil, description: nil, **options) ⇒ Attribute
Returns a new instance of Attribute.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/simple_jsonapi/definition/attribute.rb', line 22 def initialize(name, *args, type: nil, array: false, description: nil, **, &block) raise ArgumentError, "A name is required" if name.blank? super @name = name.to_sym @data_type = type&.to_sym @array = array @description = description.to_s.presence if args.none? && !block_given? @value_proc = wrap_in_proc { |resource| resource.public_send(name) } else @value_proc = wrap_in_proc(*args, &block) end end |
Instance Attribute Details
#array ⇒ Boolean (readonly)
10 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 |
# File 'lib/simple_jsonapi/definition/attribute.rb', line 10 class SimpleJsonapi::Definition::Attribute < SimpleJsonapi::Definition::Base attr_reader :name, :data_type, :array, :description # @visibility private attr_reader :value_proc # @overload initialize(name, type: nil, description: nil, **options, &block) # @overload initialize(name, value, type: nil, description: nil, **options) # @param type [Symbol] data type # @param description [String] # @yieldparam resource [Object] # @yieldreturn [Object] # @option (see Definition::Base#initialize) def initialize(name, *args, type: nil, array: false, description: nil, **, &block) raise ArgumentError, "A name is required" if name.blank? super @name = name.to_sym @data_type = type&.to_sym @array = array @description = description.to_s.presence if args.none? && !block_given? @value_proc = wrap_in_proc { |resource| resource.public_send(name) } else @value_proc = wrap_in_proc(*args, &block) end end private def initialize_dup(new_def) super # name is a symbol, can't be duped new_def.instance_variable_set(:@value_proc, @value_proc.dup) end end |
#data_type ⇒ Symbol (readonly)
10 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 |
# File 'lib/simple_jsonapi/definition/attribute.rb', line 10 class SimpleJsonapi::Definition::Attribute < SimpleJsonapi::Definition::Base attr_reader :name, :data_type, :array, :description # @visibility private attr_reader :value_proc # @overload initialize(name, type: nil, description: nil, **options, &block) # @overload initialize(name, value, type: nil, description: nil, **options) # @param type [Symbol] data type # @param description [String] # @yieldparam resource [Object] # @yieldreturn [Object] # @option (see Definition::Base#initialize) def initialize(name, *args, type: nil, array: false, description: nil, **, &block) raise ArgumentError, "A name is required" if name.blank? super @name = name.to_sym @data_type = type&.to_sym @array = array @description = description.to_s.presence if args.none? && !block_given? @value_proc = wrap_in_proc { |resource| resource.public_send(name) } else @value_proc = wrap_in_proc(*args, &block) end end private def initialize_dup(new_def) super # name is a symbol, can't be duped new_def.instance_variable_set(:@value_proc, @value_proc.dup) end end |
#description ⇒ String (readonly)
10 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 |
# File 'lib/simple_jsonapi/definition/attribute.rb', line 10 class SimpleJsonapi::Definition::Attribute < SimpleJsonapi::Definition::Base attr_reader :name, :data_type, :array, :description # @visibility private attr_reader :value_proc # @overload initialize(name, type: nil, description: nil, **options, &block) # @overload initialize(name, value, type: nil, description: nil, **options) # @param type [Symbol] data type # @param description [String] # @yieldparam resource [Object] # @yieldreturn [Object] # @option (see Definition::Base#initialize) def initialize(name, *args, type: nil, array: false, description: nil, **, &block) raise ArgumentError, "A name is required" if name.blank? super @name = name.to_sym @data_type = type&.to_sym @array = array @description = description.to_s.presence if args.none? && !block_given? @value_proc = wrap_in_proc { |resource| resource.public_send(name) } else @value_proc = wrap_in_proc(*args, &block) end end private def initialize_dup(new_def) super # name is a symbol, can't be duped new_def.instance_variable_set(:@value_proc, @value_proc.dup) end end |
#name ⇒ Symbol (readonly)
10 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 |
# File 'lib/simple_jsonapi/definition/attribute.rb', line 10 class SimpleJsonapi::Definition::Attribute < SimpleJsonapi::Definition::Base attr_reader :name, :data_type, :array, :description # @visibility private attr_reader :value_proc # @overload initialize(name, type: nil, description: nil, **options, &block) # @overload initialize(name, value, type: nil, description: nil, **options) # @param type [Symbol] data type # @param description [String] # @yieldparam resource [Object] # @yieldreturn [Object] # @option (see Definition::Base#initialize) def initialize(name, *args, type: nil, array: false, description: nil, **, &block) raise ArgumentError, "A name is required" if name.blank? super @name = name.to_sym @data_type = type&.to_sym @array = array @description = description.to_s.presence if args.none? && !block_given? @value_proc = wrap_in_proc { |resource| resource.public_send(name) } else @value_proc = wrap_in_proc(*args, &block) end end private def initialize_dup(new_def) super # name is a symbol, can't be duped new_def.instance_variable_set(:@value_proc, @value_proc.dup) end end |