Last active
September 26, 2022 23:25
-
-
Save ono-max/cc4ef9433b83a4037a63232fd0e4863b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module REXMLInspector | |
def to_obj_inspector kw | |
[ | |
{ | |
type: :tree, | |
data: get_tree(self) | |
} | |
] | |
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 | |
} | |
[ | |
{ | |
type: :table, | |
data: table_data, | |
paginate: { | |
totalLen: ary.size | |
} | |
}, | |
{ | |
type: :barChart, | |
data: table_data, | |
xAxisKeys: x_keys, | |
yAxisKeys: y_keys | |
}, | |
{ | |
type: :lineChart, | |
data: table_data, | |
xAxisKeys: x_keys, | |
yAxisKeys: y_keys | |
} | |
] | |
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