Class: Avromatic::Model::Attributes::AttributeDefinition
- Inherits:
-
Object
- Object
- Avromatic::Model::Attributes::AttributeDefinition
- Defined in:
- lib/avromatic/model/attributes.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#name_string ⇒ Object
readonly
Returns the value of attribute name_string.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#setter_name ⇒ Object
readonly
Returns the value of attribute setter_name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #coerce(input) ⇒ Object
-
#initialize(owner:, field:, type:) ⇒ AttributeDefinition
constructor
A new instance of AttributeDefinition.
- #required? ⇒ Boolean
Constructor Details
#initialize(owner:, field:, type:) ⇒ AttributeDefinition
Returns a new instance of AttributeDefinition.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/avromatic/model/attributes.rb', line 27 def initialize(owner:, field:, type:) @owner = owner @field = field @type = type @name = field.name.to_sym @name_string = field.name.to_s.dup.freeze @setter_name = "#{field.name}=".to_sym @default = if field.default == :no_default nil elsif field.default.duplicable? field.default.dup.deep_freeze else field.default end end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
24 25 26 |
# File 'lib/avromatic/model/attributes.rb', line 24 def default @default end |
#field ⇒ Object (readonly)
Returns the value of attribute field.
24 25 26 |
# File 'lib/avromatic/model/attributes.rb', line 24 def field @field end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
24 25 26 |
# File 'lib/avromatic/model/attributes.rb', line 24 def name @name end |
#name_string ⇒ Object (readonly)
Returns the value of attribute name_string.
24 25 26 |
# File 'lib/avromatic/model/attributes.rb', line 24 def name_string @name_string end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
24 25 26 |
# File 'lib/avromatic/model/attributes.rb', line 24 def owner @owner end |
#setter_name ⇒ Object (readonly)
Returns the value of attribute setter_name.
24 25 26 |
# File 'lib/avromatic/model/attributes.rb', line 24 def setter_name @setter_name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
24 25 26 |
# File 'lib/avromatic/model/attributes.rb', line 24 def type @type end |
Instance Method Details
#coerce(input) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/avromatic/model/attributes.rb', line 47 def coerce(input) type.coerce(input) rescue Avromatic::Model::UnknownAttributeError => e raise Avromatic::Model::CoercionError.new("Value for #{owner.name}##{name} could not be coerced to a #{type.name} " \ "because the following unexpected attributes were provided: #{e.unknown_attributes.join(', ')}. " \ "Only the following attributes are allowed: #{e.allowed_attributes.join(', ')}. " \ "Provided argument: #{input.inspect}") rescue StandardError if type.input_classes && type.input_classes.none? { |input_class| input.is_a?(input_class) } raise Avromatic::Model::CoercionError.new("Value for #{owner.name}##{name} could not be coerced to a #{type.name} " \ "because a #{input.class.name} was provided but expected a #{type.input_classes.map(&:name).to_sentence(two_words_connector: ' or ', last_word_connector: ', or ')}. " \ "Provided argument: #{input.inspect}") elsif input.is_a?(Hash) && type.is_a?(Avromatic::Model::Types::UnionType) raise Avromatic::Model::CoercionError.new("Value for #{owner.name}##{name} could not be coerced to a #{type.name} " \ "because no union member type matches the provided attributes: #{input.inspect}") else raise Avromatic::Model::CoercionError.new("Value for #{owner.name}##{name} could not be coerced to a #{type.name}. " \ "Provided argument: #{input.inspect}") end end |
#required? ⇒ Boolean
43 44 45 |
# File 'lib/avromatic/model/attributes.rb', line 43 def required? FieldHelper.required?(field) end |