Class: WebTools::Support::ObjectInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/web_tools/support/code_browser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ ObjectInfo

Returns a new instance of ObjectInfo.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/web_tools/support/code_browser.rb', line 68

def initialize(obj)
  @info = { }
  @info[:object_id]          = obj.object_id
  @info[:class]              = obj.class.name
  @info[:inspect]            = obj.inspect
  @info[:instance_variables] = []
  @info[:enumerated]         = []
  @info[:enumerated_size]    = obj.respond_to?(:size) ? obj.size : nil

  obj.instance_variables.each do |iv|
    val = obj.instance_variable_get(iv)
    @info[:instance_variables] << [iv, val.inspect, val.object_id]
  end

  limit = 10
  case obj
  when Enumerable, Array
    obj.each_with_index do |o,i|
      if i > limit
        @info[:enumerated] << '...'
        break
      else
        @info[:enumerated] << o.inspect
      end
    end

  when Hash
    obj.each_with_index do |pair, idx|
      if idx > limit
        @info[:enumerated] << ['', '...']
        break
      end
      @info[:enumerated] << [pair[0].to_s, pair[1].inspect]
    end
  end

end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



66
67
68
# File 'lib/web_tools/support/code_browser.rb', line 66

def info
  @info
end

Class Method Details

.for(object) ⇒ Object

Return a new ObjectInfo instance for the given object.



62
63
64
# File 'lib/web_tools/support/code_browser.rb', line 62

def self.for(object)
  new(object)
end

.for_id(object_id) ⇒ Object

Return a new ObjectInfo instance for the object with the given object id.



57
58
59
# File 'lib/web_tools/support/code_browser.rb', line 57

def self.for_id(object_id)
  new(ObjectSpace._id2ref(object_id))
end

Instance Method Details

#to_json(*args) ⇒ Object



106
107
108
# File 'lib/web_tools/support/code_browser.rb', line 106

def to_json(*args)
  @info.to_json
end