Class: Kronk::DataString
- Inherits:
-
String
- Object
- String
- Kronk::DataString
- Defined in:
- lib/kronk/data_string.rb
Overview
Creates ordered data string renders for diffing with character-precise path information.
dstr = DataString.new({'a' => 'foo', 'b' => 'bar', 'c' => ["one", "two"]})
# {
# "a": "foo",
# "b": "bar",
# "c": [
# "one",
# "two"
# ]
# }
dstr.[dstr.index("\"a\"")]
# []
dstr.[dstr.index("\"foo\"")]
# ['a']
dstr.[dstr.index("\"two\"")]
# ['c', 1]
Constant Summary collapse
- TO_RUBY =
proc do |type, obj| case type when :key_assign then " => " when :key then obj.inspect when :value then obj.inspect when :struct (obj == true || obj == false) ? "Boolean" : obj.class end end
- TO_JSON =
proc do |type, obj| case type when :key_assign then ": " when :key (Symbol === obj ? obj.inspect : obj.to_s).to_json when :value (Symbol === obj ? obj.inspect : obj).to_json when :struct ((obj == true || obj == false) ? "Boolean" : obj.class).to_json end end
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#data ⇒ Object
Returns the value of attribute data.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#struct_only ⇒ Object
Returns the value of attribute struct_only.
Class Method Summary collapse
-
.json(data, opts = {}) ⇒ Object
Returns a json data string that is diff-able, meaning sorted by Hash keys when available.
-
.ruby(data, opts = {}) ⇒ Object
Returns a ruby data string that is diff-able, meaning sorted by Hash keys when available.
Instance Method Summary collapse
-
#<<(str) ⇒ Object
Similar to String#<< but adds metadata.
-
#[](arg) ⇒ Object
Similar to String#[] but keeps metadata.
-
#append(str, metadata = nil) ⇒ Object
Add a string with metadata to the data string.
- #append_arrow ⇒ Object
-
#colorize(string, data) ⇒ Object
Assign ANSI colors based on data type.
-
#initialize(data = nil, opts = {}, &block) ⇒ DataString
constructor
Create a new DataString that is diff-able, meaning sorted by Hash keys when available.
-
#render(data, path = [], &block) ⇒ Object
Turns a data set into an ordered string output for diff-ing.
-
#split(pattern = $;, *more) ⇒ Object
Similar to String#split but keeps metadata.
Constructor Details
#initialize(data = nil, opts = {}, &block) ⇒ DataString
Create a new DataString that is diff-able, meaning sorted by Hash keys when available.
Options supported are:
- :indentation
-
Integer - how many spaces to indent by; default 1
- :render_lang
-
String - output to ‘ruby’ or ‘json’; default ‘json’
- :struct
-
Boolean - class names used instead of values; default nil
- :color
-
Boolean - render values with ANSI colors; default false
If block is given, will yield the type of object to render and an optional object to render. Types given are :key_assign, :key, :value, or :struct. By default DataString uses the TO_JSON proc.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/kronk/data_string.rb', line 87 def initialize data=nil, opts={}, &block @struct_only = opts[:struct] @color = opts[:color] || Kronk.config[:color_data] @indentation = opts[:indentation] || Kronk.config[:indentation] || 1 @meta = [] if String === data super data @data = nil else super "" data = Path.pathed(data) if Kronk.config[:render_paths] @data = data block ||= Kronk.config[:render_lang].to_s == 'ruby' ? TO_RUBY : TO_JSON end render data, &block if data && block end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
70 71 72 |
# File 'lib/kronk/data_string.rb', line 70 def color @color end |
#data ⇒ Object
Returns the value of attribute data.
70 71 72 |
# File 'lib/kronk/data_string.rb', line 70 def data @data end |
#meta ⇒ Object
Returns the value of attribute meta.
70 71 72 |
# File 'lib/kronk/data_string.rb', line 70 def @meta end |
#struct_only ⇒ Object
Returns the value of attribute struct_only.
70 71 72 |
# File 'lib/kronk/data_string.rb', line 70 def struct_only @struct_only end |
Class Method Details
.json(data, opts = {}) ⇒ Object
Returns a json data string that is diff-able, meaning sorted by Hash keys when available.
65 66 67 |
# File 'lib/kronk/data_string.rb', line 65 def self.json data, opts={} new(data, opts, &TO_JSON) end |
.ruby(data, opts = {}) ⇒ Object
Returns a ruby data string that is diff-able, meaning sorted by Hash keys when available.
56 57 58 |
# File 'lib/kronk/data_string.rb', line 56 def self.ruby data, opts={} new(data, opts, &TO_RUBY) end |
Instance Method Details
#<<(str) ⇒ Object
Similar to String#<< but adds metadata.
191 192 193 194 195 196 197 198 |
# File 'lib/kronk/data_string.rb', line 191 def << str if str.class == self.class @meta.concat str. else @meta.concat([@meta.last] * str.length) end super str end |
#[](arg) ⇒ Object
Similar to String#[] but keeps metadata.
204 205 206 207 208 |
# File 'lib/kronk/data_string.rb', line 204 def [] arg dstr = self.class.new super dstr. = @meta[arg] dstr end |
#append(str, metadata = nil) ⇒ Object
Add a string with metadata to the data string.
183 184 185 186 |
# File 'lib/kronk/data_string.rb', line 183 def append str, =nil @meta.concat([] * str.length) self.append_arrow str end |
#append_arrow ⇒ Object
178 |
# File 'lib/kronk/data_string.rb', line 178 alias append_arrow << |
#colorize(string, data) ⇒ Object
Assign ANSI colors based on data type.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/kronk/data_string.rb', line 111 def colorize string, data case data when String "\033[0;36m#{string}\033[0m" when Numeric "\033[0;33m#{string}\033[0m" when TrueClass, FalseClass "\033[1;35m#{string}\033[0m" when NilClass "\033[1;31m#{string}\033[0m" else string end end |
#render(data, path = [], &block) ⇒ Object
Turns a data set into an ordered string output for diff-ing.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/kronk/data_string.rb', line 130 def render data, path=[], &block indent = (path.length + 1) * @indentation pad = " " * indent case data when Hash append("{}", path) and return if data.empty? append "{\n", path sort_any(data.keys).each_with_index do |key, i| append "#{pad}#{ yield(:key, key) }#{ yield(:key_assign) }", path value = data[key] new_path = path.dup << key render value, new_path, &block append(",", path) unless i == data.length - 1 append("\n", path) end append(("#{" " * (indent - @indentation)}}"), path) when Array append("[]", path) and return if data.empty? append "[\n", path (0...data.length).each do |key| append pad, path value = data[key] new_path = path.dup << key render value, new_path, &block append(",", path) unless key == data.length - 1 append("\n", path) end append(("#{" " * (indent - @indentation)}]"), path) else output = @struct_only ? yield(:struct, data) : yield(:value, data) output = colorize output, data if @color append output.to_s, path end end |
#split(pattern = $;, *more) ⇒ Object
Similar to String#split but keeps metadata.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/kronk/data_string.rb', line 214 def split pattern=$;, *more arr = super i = 0 interval = 0 interval = (self.length - arr.join.length) / (arr.length - 1) if arr.length > 1 arr.map do |str| ds = self.class.new str ds. = @meta[i,str.length] i += str.length + interval ds end end |