Class: JsonMessage::Field
- Inherits:
-
Object
- Object
- JsonMessage::Field
- Defined in:
- lib/json_message.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
-
#initialize(name, options = {}, &blk) ⇒ Field
constructor
A new instance of Field.
- #validate(value) ⇒ Object
Constructor Details
#initialize(name, options = {}, &blk) ⇒ Field
Returns a new instance of Field.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/json_message.rb', line 37 def initialize(name, = {}, &blk) blk ||= lambda { |*_| [:schema] || any } @name = name @schema = Membrane::SchemaParser.parse(&blk) @required = [:required] || false @default = [:default] if @required && @default raise DefinitionError, \ "Cannot define a default value for required field #{name}" end validate(@default) if @default end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
35 36 37 |
# File 'lib/json_message.rb', line 35 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
35 36 37 |
# File 'lib/json_message.rb', line 35 def name @name end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
35 36 37 |
# File 'lib/json_message.rb', line 35 def required @required end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
35 36 37 |
# File 'lib/json_message.rb', line 35 def schema @schema end |
Instance Method Details
#validate(value) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/json_message.rb', line 53 def validate(value) begin @schema.validate(value) rescue Membrane::SchemaValidationError => e raise ValidationError.new( { name => e. } ) end end |