Class: SKUI::JSON

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
src/SKUI/json.rb

Overview

Sortable Hash that preserves the insertion order. When converted to strings it output a JavaScript JSON string that can be used in WebDialogs for instance.

Based of Bill Kelly’s InsertOrderPreservingHash

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ JSON

Returns a new instance of JSON.

Since:

  • 1.0.0



16
17
18
19
20
21
22
23
24
# File 'src/SKUI/json.rb', line 16

def initialize( *args, &block )
  if args.size == 1 && args[0].is_a?( Hash )
    @hash = args[0].dup
    @ordered_keys = @hash.keys
  else
    @hash = Hash.new( *args, &block )
    @ordered_keys = []
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object

Since:

  • 1.0.0



117
118
119
# File 'src/SKUI/json.rb', line 117

def method_missing( *args )
  @hash.send( *args )
end

Class Method Details

.object_to_js(object, format = false) ⇒ String

Converts given Ruby object to a JavaScript string.

Parameters:

  • object (Object)
  • format (Boolean) (defaults to: false)

    Set to true for pretty print.

Returns:

  • (String)

Since:

  • 1.0.0



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
176
177
178
179
180
181
182
183
# File 'src/SKUI/json.rb', line 151

def self.object_to_js( object, format = false )
  if object.is_a?( self )
    object.to_s( format )
  elsif object.is_a?( Hash )
    self.new( object ).to_s( format )
  elsif object.is_a?( Symbol )
    object.inspect.inspect
  elsif object.is_a?( Regexp )
    o = object.options
    i = o & Regexp::EXTENDED == Regexp::EXTENDED ? 'i' : '' # Invalid in JS
    i = o & Regexp::IGNORECASE == Regexp::IGNORECASE ? 'i' : ''
    m = o & Regexp::MULTILINE == Regexp::MULTILINE ? 'm' : ''
    "/#{object.source}/#{i}#{m}"
  elsif object.nil?
    'null'
  elsif object.is_a?( Array )
    data = object.map { |obj| object_to_js( obj, format ) }
    "[#{data.join(',')}]"
  elsif object.is_a?( Geom::Point3d )
    "new Point3d( #{object.to_a.join(', ')} )"
  elsif object.is_a?( Geom::Vector3d )
    "new Vector3d( #{object.to_a.join(', ')} )"
  elsif object.is_a?( Sketchup::Color )
    "new Color( #{object.to_a.join(', ')} )"
  elsif object.respond_to?( :to_js )
    object.to_js
  else
    # String, Integer, Float, TrueClass, FalseClass.
    # (!) Filter out accepted objects.
    # (!) Convert unknown into strings - then inspect.
    object.inspect
  end
end

Instance Method Details

#[]=(key, value) ⇒ Object

Since:

  • 1.0.0



34
35
36
37
# File 'src/SKUI/json.rb', line 34

def []=( key, value )
  @ordered_keys << key unless @hash.has_key?( key )
  @hash[key] = value
end

#clearObject

Since:

  • 1.0.0



40
41
42
43
# File 'src/SKUI/json.rb', line 40

def clear
  @hash.clear
  @ordered_keys.clear
end

#delete(key, &block) ⇒ Object

Since:

  • 1.0.0



86
87
88
89
# File 'src/SKUI/json.rb', line 86

def delete( key, &block )
  @ordered_keys.delete( key )
  @hash.delete( key, &block )
end

#delete_if(&block) ⇒ Object

Since:

  • 1.0.0



100
101
102
103
# File 'src/SKUI/json.rb', line 100

def delete_if( &block )
  reject!( &block )
  self
end

#eachObject Also known as: each_pair

Since:

  • 1.0.0



46
47
48
# File 'src/SKUI/json.rb', line 46

def each
  @ordered_keys.each { |key| yield( key, @hash[key] ) }
end

#each_keyObject

Since:

  • 1.0.0



57
58
59
# File 'src/SKUI/json.rb', line 57

def each_key
  @ordered_keys.each { |key| yield key }
end

#each_valueObject

Since:

  • 1.0.0



52
53
54
# File 'src/SKUI/json.rb', line 52

def each_value
  @ordered_keys.each { |key| yield( @hash[key] ) }
end

#initialize_copy(source) ⇒ Object

Since:

  • 1.0.0



27
28
29
30
31
# File 'src/SKUI/json.rb', line 27

def initialize_copy( source )  
  super  
  @hash = @hash.dup
  @ordered_keys = @ordered_keys.dup  
end

#key?(key) ⇒ Boolean Also known as: has_key?, include?, member?

Returns:

  • (Boolean)

Since:

  • 1.0.0



62
63
64
# File 'src/SKUI/json.rb', line 62

def key?( key )
  @hash.key?( key )
end

#keysObject

Since:

  • 1.0.0



70
71
72
# File 'src/SKUI/json.rb', line 70

def keys
  @ordered_keys
end

#merge!(hash) ⇒ Object

Since:

  • 1.0.0



106
107
108
109
110
111
112
113
114
# File 'src/SKUI/json.rb', line 106

def merge!( hash )
  hash.each { |key, value|
    if @hash.key?( key )
      @hash[key] = value
    else
      self[key] = value
    end
  }
end

#reject!Object

Since:

  • 1.0.0



92
93
94
95
96
97
# File 'src/SKUI/json.rb', line 92

def reject!
  del = []
  each_pair { |key, value| del << key if yield( key, value ) }
  del.each { |key| delete( key ) }
  del.empty? ? nil : self
end

#to_hashHash

Returns:

  • (Hash)

Since:

  • 1.0.0



123
124
125
# File 'src/SKUI/json.rb', line 123

def to_hash
  @hash.dup
end

#to_s(format = false) ⇒ String Also known as: inspect

Compile JSON Hash into a string.

Parameters:

  • format (Boolean) (defaults to: false)

    Set to true for pretty print.

Returns:

  • (String)

Since:

  • 1.0.0



133
134
135
136
137
138
139
140
141
# File 'src/SKUI/json.rb', line 133

def to_s( format = false )
  data = self.map { |key, value|
    json_key = ( key.is_a?(Symbol) ) ? key.to_s.inspect : key.inspect
    json_value = self.class.object_to_js( value, format )
    "#{json_key}: #{json_value}"
  }
  json_string = (format) ? data.join(",\n\t") : data.join(", ")
  return (format) ? "{\n\t#{json_string}\n}\n" : "{#{json_string}}"
end

#valuesObject

Since:

  • 1.0.0



75
76
77
# File 'src/SKUI/json.rb', line 75

def values
  @ordered_keys.map { |key| @hash[key] }
end