Extended maintenance of Ruby versions 1.8.7 and 1.9.2 will end on July 31, 2014. Read more

In Files

  • json/lib/json/editor.rb

JSON::Editor

Constants

ALL_NODES

The Nodes necessary for the tree representation of a JSON document

ALL_TYPES

All JSON primitive types

CONTAINER_TYPES

JSON primitive types (Containers)

CONTENT_COL
DEFAULT_DIALOG_KEY_PRESS_HANDLER
TITLE

Beginning of the editor window title

Public Class Methods

data2model(data, model = nil, parent = nil) click to toggle source

Convert the Ruby data structure data into tree model data for Gtk and returns the whole model. If the parameter model wasn’t given a new Gtk::TreeStore is created as the model. The parent parameter specifies the parent node (iter, Gtk:TreeIter instance) to which the data is appended, alternativeley the result of the yielded block is used as iter.

 
               # File json/lib/json/editor.rb, line 121
def Editor.data2model(data, model = nil, parent = nil)
  model ||= TreeStore.new(Gdk::Pixbuf, String, String)
  iter = if block_given?
    yield model
  else
    model.append(parent)
  end
  case data
  when Hash
    iter.type = 'Hash'
    data.sort.each do |key, value|
      pair_iter = model.append(iter)
      pair_iter.type    = 'Key'
      pair_iter.content = key.to_s
      Editor.data2model(value, model, pair_iter)
    end
  when Array
    iter.type = 'Array'
    data.each do |value|
      Editor.data2model(value, model, iter)
    end
  when Numeric
    iter.type = 'Numeric'
    iter.content = data.to_s
  when String, true, false, nil
    iter.type    = data.class.name
    iter.content = data.nil? ? 'null' : data.to_s
  else
    iter.type    = 'String'
    iter.content = data.to_s
  end
  model
end
            
error_dialog(window, text) click to toggle source

Opens an error dialog on top of window showing the error message text.

 
               # File json/lib/json/editor.rb, line 50
def Editor.error_dialog(window, text)
  dialog = MessageDialog.new(window, Dialog::MODAL, 
    MessageDialog::ERROR, 
    MessageDialog::BUTTONS_CLOSE, text)
  dialog.show_all
  dialog.run
rescue TypeError
  dialog = MessageDialog.new(Editor.window, Dialog::MODAL, 
    MessageDialog::ERROR, 
    MessageDialog::BUTTONS_CLOSE, text)
  dialog.show_all
  dialog.run
ensure
  dialog.destroy if dialog
end
            
fetch_icon(name) click to toggle source

Returns the Gdk::Pixbuf of the icon named name from the icon cache.

 
               # File json/lib/json/editor.rb, line 39
def Editor.fetch_icon(name)
  @icon_cache ||= {}
  unless @icon_cache.key?(name)
    path = File.dirname(__FILE__)
    @icon_cache[name] = Gdk::Pixbuf.new(File.join(path, name + '.xpm'))
  end
 @icon_cache[name]
end
            
model2data(iter) click to toggle source

Convert the tree model starting from Gtk::TreeIter iter into a Ruby data structure and return it.

 
               # File json/lib/json/editor.rb, line 83
def Editor.model2data(iter)
  return nil if iter.nil?
  case iter.type
  when 'Hash'
    hash = {}
    iter.each { |c| hash[c.content] = Editor.model2data(c.first_child) }
    hash
  when 'Array'
    array = Array.new(iter.n_children)
    iter.each_with_index { |c, i| array[i] = Editor.model2data(c) }
    array
  when 'Key'
    iter.content
  when 'String'
    iter.content
  when 'Numeric'
    content = iter.content
    if /\./.match(content)
      content.to_f
    else
      content.to_i
    end
  when 'TrueClass'
    true
  when 'FalseClass'
    false
  when 'NilClass'
    nil
  else
    fail "Unknown type found in model: #{iter.type}"
  end
end
            
question_dialog(window, text) click to toggle source

Opens a yes/no question dialog on top of window showing the error message text. If yes was answered true is returned, otherwise false.

 
               # File json/lib/json/editor.rb, line 69
def Editor.question_dialog(window, text)
  dialog = MessageDialog.new(window, Dialog::MODAL, 
    MessageDialog::QUESTION, 
    MessageDialog::BUTTONS_YES_NO, text)
  dialog.show_all
  dialog.run do |response|
    return Gtk::Dialog::RESPONSE_YES === response
  end
ensure
  dialog.destroy if dialog
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus