Class: RestEasy::Attribute
- Inherits:
-
Object
- Object
- RestEasy::Attribute
- Defined in:
- lib/rest_easy/attribute.rb
Overview
Stores the definition of a single attribute declared via the Resource DSL.
Instance Attribute Summary collapse
-
#api_name ⇒ Object
readonly
Returns the value of attribute api_name.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#parse_block ⇒ Object
readonly
Returns the value of attribute parse_block.
-
#serialise_block ⇒ Object
readonly
Returns the value of attribute serialise_block.
-
#source_fields ⇒ Object
readonly
Returns the value of attribute source_fields.
-
#target_fields ⇒ Object
readonly
Returns the value of attribute target_fields.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #coerce(value) ⇒ Object
-
#initialize(model_name:, api_name:, type:, flags: [], parse_block: nil, serialise_block: nil, source_fields: [], target_fields: []) ⇒ Attribute
constructor
A new instance of Attribute.
- #key? ⇒ Boolean
- #optional? ⇒ Boolean
- #parse_value(*raw_values) ⇒ Object
- #read_only? ⇒ Boolean
- #required? ⇒ Boolean
- #serialise_value(*model_values) ⇒ Object
- #synthetic? ⇒ Boolean
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_name ⇒ Object (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 |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
6 7 8 |
# File 'lib/rest_easy/attribute.rb', line 6 def flags @flags end |
#model_name ⇒ Object (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_block ⇒ Object (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_block ⇒ Object (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_fields ⇒ Object (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_fields ⇒ Object (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 |
#type ⇒ Object (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.) rescue Dry::Types::CoercionError => e raise RestEasy::ConstraintError.new(@model_name, value, e.) end |
#key? ⇒ Boolean
31 32 33 |
# File 'lib/rest_easy/attribute.rb', line 31 def key? @flags.include?(:key) end |
#optional? ⇒ 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
27 28 29 |
# File 'lib/rest_easy/attribute.rb', line 27 def read_only? @flags.include?(:read_only) end |
#required? ⇒ 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
35 36 37 |
# File 'lib/rest_easy/attribute.rb', line 35 def synthetic? @flags.include?(:synthetic) end |