Class: Cucumber::Messages::Message
- Inherits:
-
Object
- Object
- Cucumber::Messages::Message
- Defined in:
- lib/cucumber/messages/message.rb
Direct Known Subclasses
Attachment, Background, Ci, Comment, DataTable, DocString, Duration, Envelope, Examples, Exception, Feature, FeatureChild, GherkinDocument, Git, Group, Hook, JavaMethod, JavaStackTraceElement, Location, Meta, ParameterType, ParseError, Pickle, PickleDocString, PickleStep, PickleStepArgument, PickleTable, PickleTableCell, PickleTableRow, PickleTag, Product, Rule, RuleChild, Scenario, Source, SourceReference, Step, StepDefinition, StepDefinitionPattern, StepMatchArgument, StepMatchArgumentsList, TableCell, TableRow, Tag, TestCase, TestCaseFinished, TestCaseStarted, TestRunFinished, TestRunStarted, TestStep, TestStepFinished, TestStepResult, TestStepStarted, Timestamp, UndefinedParameterType
Class Method Summary collapse
- .camelize(term) ⇒ Object
-
.from_json(json_string) ⇒ Object
Returns a new Message - or messages into an array - deserialized from the given json document.
Instance Method Summary collapse
-
#to_h(camelize: false, reject_nil_values: false) ⇒ Object
Returns a new Hash formed from the message attributes If
camelize:
keyword parameter is set to true, then keys will be camelized Ifreject_nil_values:
keyword parameter is set to true, resulting hash won’t include nil values. -
#to_json(*_args) ⇒ Object
Generates a JSON document from the message.
Class Method Details
.camelize(term) ⇒ Object
8 9 10 11 |
# File 'lib/cucumber/messages/message.rb', line 8 def self.camelize(term) camelized = term.to_s camelized.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" } end |
.from_json(json_string) ⇒ Object
Returns a new Message - or messages into an array - deserialized from the given json document. CamelCased keys are properly converted to snake_cased attributes in the process
Cucumber::Messages::Duration.from_json('{"seconds":1,"nanos":42}')
# => #<Cucumber::Messages::Duration:0x00007efda134c290 @seconds=1, @nanos=42>
Cucumber::Messages::PickleTag.from_json('{"name":"foo","astNodeId":"abc-def"}')
# => #<Cucumber::Messages::PickleTag:0x00007efda138cdb8 @name="foo", @ast_node_id="abc-def">
It is recursive so embedded messages are also processed.
json_string = { location: { line: 2 }, text: "comment" }.to_json
Cucumber::Messages::Comment.from_json(json_string)
# => #<Cucumber::Messages::Comment:0x00007efda6abf888 @location=#<Cucumber::Messages::Location:0x00007efda6abf978 @line=2, @column=nil>, @text="comment">
json_string = { uri: 'file:///...', comments: [{text: 'text comment'}, {text: 'another comment'}]}.to_json
Cucumber::Messages::GherkinDocument.from_json(json_string)
# => #<Cucumber::Messages::GherkinDocument:0x00007efda11e6a90 ... @comments=[#<Cucumber::Messages::Comment:0x00007efda11e6e50 ...]>
32 33 34 |
# File 'lib/cucumber/messages/message.rb', line 32 def self.from_json(json_string) from_h(JSON.parse(json_string, { symbolize_names: true })) end |
Instance Method Details
#to_h(camelize: false, reject_nil_values: false) ⇒ Object
Returns a new Hash formed from the message attributes If camelize:
keyword parameter is set to true, then keys will be camelized If reject_nil_values:
keyword parameter is set to true, resulting hash won’t include nil values
Cucumber::Messages::Duration.new(seconds: 1, nanos: 42).to_h
# => { seconds: 1, nanos: 42 }
Cucumber::Messages::PickleTag.new(name: 'foo', ast_node_id: 'abc-def').to_h(camelize: true)
# => { name: 'foo', astNodeId: 'abc-def' }
Cucumber::Messages::PickleTag.new(name: 'foo', ast_node_id: nil).to_h(reject_nil_values: true)
# => { name: 'foo' }
It is recursive so embedded messages are also processed
location = Cucumber::Messages::Location.new(line: 2)
Cucumber::Messages::Comment.new(location: location, text: 'comment').to_h
# => { location: { line: 2, :column: nil }, text: "comment" }
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cucumber/messages/message.rb', line 54 def to_h(camelize: false, reject_nil_values: false) resulting_hash = instance_variables.to_h do |variable_name| h_key = variable_name[1..] h_key = Cucumber::Messages::Message.camelize(h_key) if camelize h_value = prepare_value(instance_variable_get(variable_name), camelize: camelize, reject_nil_values: reject_nil_values) [h_key.to_sym, h_value] end resulting_hash.tap { |hash| hash.compact! if reject_nil_values } end |
#to_json(*_args) ⇒ Object
Generates a JSON document from the message. Keys are camelized during the process. Null values are not part of the json document.
Cucumber::Messages::Duration.new(seconds: 1, nanos: 42).to_json
# => '{"seconds":1,"nanos":42}'
Cucumber::Messages::PickleTag.new(name: 'foo', ast_node_id: 'abc-def').to_json
# => '{"name":"foo","astNodeId":"abc-def"}'
Cucumber::Messages::PickleTag.new(name: 'foo', ast_node_id: nil).to_json
# => '{"name":"foo"}'
As with #to_h, the method is recursive
location = Cucumber::Messages::Location.new(line: 2)
Cucumber::Messages::Comment.new(location: location, text: 'comment').to_json
# => '{"location":{"line":2,"column":null},"text":"comment"}'
82 83 84 |
# File 'lib/cucumber/messages/message.rb', line 82 def to_json(*_args) to_h(camelize: true, reject_nil_values: true).to_json end |