Module: ElmHistoryTools::HistoryFormatter
- Defined in:
- lib/elm_history_tools/history_formatter.rb
Class Method Summary collapse
-
.simplify_history_entry(entry) ⇒ Object
Turn an Elm history entry into a simple Ruby hash, as described above.
-
.to_simple_hash(history_data) ⇒ Object
Given a raw Elm history file parsed to JSON, return a simplified hash of the history.
Class Method Details
.simplify_history_entry(entry) ⇒ Object
Turn an Elm history entry into a simple Ruby hash, as described above.
Constructors that take no arguments are represented as taking an empty list (see above); an alternative approach would be to use nil. While that would clearly distinguish between those cases, it would make working with the results more complicated.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/elm_history_tools/history_formatter.rb', line 22 def self.simplify_history_entry(entry) ElmHistoryTools::Utils.transform_object(entry) do |object_hash| if object_hash["ctor"] == "::" # Elm lists are represented as nested entries with the contructor ::. (See the readme for # more detail.) # We collapse those into a proper Ruby array via flatten. # The last entry of the list will have no nested entry, so we use compact to remove the nil. [simplify_history_entry(object_hash["_0"]), simplify_history_entry(object_hash["_1"])].compact.flatten elsif object_hash["ctor"] # we have an Elm object type (we know this because non-objects aren't passed to the block) { object_hash["ctor"] => object_hash.reject {|k, _v| k == "ctor"}.values.map {|val| simplify_history_entry(val) } } end end end |
.to_simple_hash(history_data) ⇒ Object
Given a raw Elm history file parsed to JSON, return a simplified hash of the history.
For instance, given an array like:
- “MessageType”, “_0”: “Arg1”, “_02”: {“ctor”: “AnotherType”}
-
you’ll get
- => [“Arg1”, {“AnotherType” => []]}
11 12 13 14 15 |
# File 'lib/elm_history_tools/history_formatter.rb', line 11 def self.to_simple_hash(history_data) history_data["history"].map do |entry| simplify_history_entry(entry) end end |