Skip to content

Instantly share code, notes, and snippets.

@ono-max
Last active October 2, 2022 05:55
Show Gist options
  • Save ono-max/05c6070225e49ec6ade339268a6593c0 to your computer and use it in GitHub Desktop.
Save ono-max/05c6070225e49ec6ade339268a6593c0 to your computer and use it in GitHub Desktop.
require 'json'
module REXMLInspector
def to_rdbg_mimebundle kw
json = JSON.generate {
type: :tree,
data: get_tree(self)
}
[
{
"application/json" => json
}
]
end
def get_tree elems
ary = []
elems.each_element{|elem|
hash = {}
key = elem.name
childs = []
if elem.attributes.size > 0
attrs = {}
elem.attributes.each_attribute{|attribute|
attrs[attribute.name] = attribute.value
}
childs << attrs
end
if elem.has_elements?
childs.push *get_tree(elem)
end
if childs.size == 0
hash[key] = elem.text
else
if elem.text && elem.text.strip != ""
key = "#{key} #{elem.text}"
end
hash[key] = childs
end
ary << hash
}
ary
end
end
begin
require 'rexml'
REXML::Document.prepend REXMLInspector
rescue LoadError
end
module ActiveRecordInspector
def to_obj_inspector kw
ary = self.to_a
table_data = ary[kw['offset'], kw['pageSize']].map{|elem| elem.attributes}
x_keys = []
y_keys = []
table_data.first.each{|key, val|
if key == 'id' || !val.is_a?(Numeric)
x_keys << key
else
y_keys << key
end
}
table = JSON.generate({
type: :table,
data: table_data,
paginate: {
totalLen: ary.size
}
})
bar = JSON.generate({
type: :barChart,
data: table_data,
xAxisKeys: x_keys,
yAxisKeys: y_keys
})
line = JSON.generate({
type: :lineChart,
data: table_data,
xAxisKeys: x_keys,
yAxisKeys: y_keys
})
[
"application/json" => table,
"application/json" => bar,
"application/json" => line
]
end
end
begin
require 'active_support'
ActiveSupport.on_load(:active_record) do
ActiveRecord::Relation.include ActiveRecordInspector
end
rescue LoadError
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment