Class: RestEasy::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/rest_easy/attribute.rb

Overview

Stores the definition of a single attribute declared via the Resource DSL.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_name:, api_name:, type:, flags: [], parse_block: nil, serialise_block: nil, source_fields: [], target_fields: []) ⇒ Attribute

Returns a new instance of Attribute.



8
9
10
11
12
13
14
15
16
17
# File 'lib/rest_easy/attribute.rb', line 8

def initialize(model_name:, api_name:, type:, flags: [], parse_block: nil, serialise_block: nil, source_fields: [], target_fields: [])
  @model_name = model_name.to_sym
  @api_name = api_name.to_s
  @type = type
  @flags = Array(flags).map(&:to_sym)
  @parse_block = parse_block
  @serialise_block = serialise_block
  @source_fields = source_fields
  @target_fields = target_fields
end

Instance Attribute Details

#api_nameObject (readonly)

Returns the value of attribute api_name.



6
7
8
# File 'lib/rest_easy/attribute.rb', line 6

def api_name
  @api_name
end

#flagsObject (readonly)

Returns the value of attribute flags.



6
7
8
# File 'lib/rest_easy/attribute.rb', line 6

def flags
  @flags
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



6
7
8
# File 'lib/rest_easy/attribute.rb', line 6

def model_name
  @model_name
end

#parse_blockObject (readonly)

Returns the value of attribute parse_block.



6
7
8
# File 'lib/rest_easy/attribute.rb', line 6

def parse_block
  @parse_block
end

#serialise_blockObject (readonly)

Returns the value of attribute serialise_block.



6
7
8
# File 'lib/rest_easy/attribute.rb', line 6

def serialise_block
  @serialise_block
end

#source_fieldsObject (readonly)

Returns the value of attribute source_fields.



6
7
8
# File 'lib/rest_easy/attribute.rb', line 6

def source_fields
  @source_fields
end

#target_fieldsObject (readonly)

Returns the value of attribute target_fields.



6
7
8
# File 'lib/rest_easy/attribute.rb', line 6

def target_fields
  @target_fields
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/rest_easy/attribute.rb', line 6

def type
  @type
end

Instance Method Details

#coerce(value) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/rest_easy/attribute.rb', line 39

def coerce(value)
  @type[value]
rescue Dry::Types::ConstraintError => e
  raise RestEasy::ConstraintError.new(@model_name, value, e.message)
rescue Dry::Types::CoercionError => e
  raise RestEasy::ConstraintError.new(@model_name, value, e.message)
end

#key?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rest_easy/attribute.rb', line 31

def key?
  @flags.include?(:key)
end

#optional?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rest_easy/attribute.rb', line 23

def optional?
  @flags.include?(:optional)
end

#parse_value(*raw_values) ⇒ Object



47
48
49
50
# File 'lib/rest_easy/attribute.rb', line 47

def parse_value(*raw_values)
  value = @parse_block ? @parse_block.call(*raw_values) : raw_values.first
  coerce(value)
end

#read_only?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rest_easy/attribute.rb', line 27

def read_only?
  @flags.include?(:read_only)
end

#required?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rest_easy/attribute.rb', line 19

def required?
  @flags.include?(:required)
end

#serialise_value(*model_values) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/rest_easy/attribute.rb', line 52

def serialise_value(*model_values)
  if @serialise_block
    @serialise_block.call(*model_values)
  else
    to_json_value(model_values.first)
  end
end

#synthetic?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rest_easy/attribute.rb', line 35

def synthetic?
  @flags.include?(:synthetic)
end