Module: Kappamaki
- Defined in:
- lib/kappamaki.rb
Overview
Kappamaki provides helper methods to implement Cucumber steps that define data using natural language.
Class Method Summary collapse
-
.attributes_from_sentence(sentence) ⇒ Hash
Parses a given string containing key-value pairs into a Hash.
-
.from_sentence(sentence) ⇒ Array<String>
Reverse of ActiveSupport’s “to_sentence” method.
-
.symbolize_keys_deep!(hash) ⇒ Hash
Converts all keys in the given hash to symbols.
Class Method Details
.attributes_from_sentence(sentence) ⇒ Hash
Parses a given string containing key-value pairs into a Hash
Values must be delimited by double-quotes, like this: key: “value”
12 13 14 15 16 17 18 |
# File 'lib/kappamaki.rb', line 12 def self.attributes_from_sentence(sentence) raise ArgumentError, "sentence must be a String" unless sentence.is_a?(String) Kappamaki.from_sentence(sentence) .map { |piece| piece.split(": ") } .to_h { |key, value| [key.to_sym, value] } end |
.from_sentence(sentence) ⇒ Array<String>
Reverse of ActiveSupport’s “to_sentence” method
25 26 27 28 29 30 31 32 |
# File 'lib/kappamaki.rb', line 25 def self.from_sentence(sentence) raise ArgumentError, "sentence must be a String" unless sentence.is_a?(String) sentence.gsub(", and ", ", ") .gsub(" and ", ", ") .split(", ") .map { |s| s.delete('"') } end |
.symbolize_keys_deep!(hash) ⇒ Hash
Converts all keys in the given hash to symbols
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/kappamaki.rb', line 39 def self.symbolize_keys_deep!(hash) raise ArgumentError, "hash must be a Hash" unless hash.is_a?(Hash) hash.keys.each do |key| key_sym = key.to_sym value = hash.delete(key) hash[key_sym] = case value when Hash symbolize_keys_deep!(value) when Array value.map { |item| item.is_a?(Hash) ? symbolize_keys_deep!(item) : item } else value end end hash end |