Class: SimpleJsonapi::Definition::Attribute

Inherits:
Base
  • Object
show all
Defined in:
lib/simple_jsonapi/definition/attribute.rb

Overview

Represents a single attribute in a resource’s attributes collection

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • type (Symbol) (defaults to: nil)

    data type

  • description (String) (defaults to: nil)
  • options (Hash)

    a customizable set of options

Options Hash (**options):

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

Yield Parameters:

  • resource (Object)

Yield Returns:

  • (Object)

Raises:

  • (ArgumentError)


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, **options, &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

#arrayBoolean (readonly)

Returns:

  • (Boolean)


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, **options, &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_typeSymbol (readonly)

Returns:

  • (Symbol)


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, **options, &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

#descriptionString (readonly)

Returns:

  • (String)


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, **options, &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

#nameSymbol (readonly)

Returns:

  • (Symbol)


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, **options, &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