Class: Hessian2::ClassWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/hessian2/class_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, object) ⇒ ClassWrapper

Returns a new instance of ClassWrapper.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hessian2/class_wrapper.rb', line 5

def initialize(klass, object)
	raise 'klass should not be nil' unless klass
  
  if klass.include?('[')
  	is_multi = true
   klass.delete!('[]')

  	sample = object.select{|x| x}.first
    unless sample # all nil
   	values = [ nil ] * object.size
   else
   	fields = if sample.is_a?(Hash)
    	sample.keys.map{|k| k.to_sym }
      elsif sample.instance_variable_get(:@values).is_a?(Hash)
        sample.values.keys.map{|k| k }
    elsif sample.instance_variable_get(:@attributes).is_a?(Hash)
    	sample.attributes.keys.map{|k| k.to_sym }
      elsif sample.is_a?(ClassWrapper)
        sample.fields
      elsif sample.is_a?(TypeWrapper)
        sample.object.keys.map{|k| k.to_sym }
    else
    	sample.instance_variables.map{|k| k[1..-1].to_sym }
    end

    raise "fields should not be empty: #{object.inspect}" if fields.empty?

    values = object.map do |obj|
    	if obj.nil?
    		nil
    	elsif obj.is_a?(Hash)
       fields.map{|f| obj[f] || obj[f.to_s] }
        elsif obj.instance_variable_get(:@values).is_a?(Hash)
          fields.map{|f| obj.values[f] }
     elsif obj.instance_variable_get(:@attributes).is_a?(Hash)
       fields.map{|f| obj.attributes[f.to_s] }
        elsif obj.is_a?(ClassWrapper)
          obj.values
        elsif obj.is_a?(TypeWrapper)
          fields.map{|f| obj.object[f] || obj.object[f.to_s] }
     else
       fields.map{|f| obj.instance_variable_get(f.to_s.prepend('@')) }
     end
    end
   end
 else
 	is_multi = false

 	if object
 		fields, values = [], []
  	if object.is_a?(Hash)
      object.each do |k, v|
      	fields << k.to_sym
      	values << v
      end
      elsif object.instance_variable_get(:@values).is_a?(Hash)
        object.values.each do |k, v|
          fields << k
          values << v
        end
    elsif object.instance_variable_get(:@attributes).is_a?(Hash)
      object.attributes.each do |k, v|
      	fields << k.to_sym
      	values << v
      end
      elsif object.is_a?(ClassWrapper)
        fields, values = object.fields, object.values
      elsif object.is_a?(TypeWrapper)
        object.object.each do |k, v|
          fields << k.to_sym
          values << v
        end
    else
      object.instance_variables.each do |var|
      	k = var[1..-1]
      	fields << k.to_sym
      	values << object.instance_variable_get(k.prepend('@'))
      end
    end

    raise "fields should not be empty: #{object.inspect}" if fields.empty?
  end
 end

  @klass, @fields, @values, @is_multi = klass, fields, values, is_multi 
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



3
4
5
# File 'lib/hessian2/class_wrapper.rb', line 3

def fields
  @fields
end

#is_multiObject (readonly)

Returns the value of attribute is_multi.



3
4
5
# File 'lib/hessian2/class_wrapper.rb', line 3

def is_multi
  @is_multi
end

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/hessian2/class_wrapper.rb', line 3

def klass
  @klass
end

#valuesObject (readonly)

Returns the value of attribute values.



3
4
5
# File 'lib/hessian2/class_wrapper.rb', line 3

def values
  @values
end

Instance Method Details

#is_multi?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/hessian2/class_wrapper.rb', line 93

def is_multi?
	@is_multi
end