Class: JSON2Ruby::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/json2ruby/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ruby_types = {}) ⇒ Collection

Returns a new instance of Collection.



11
12
13
14
# File 'lib/json2ruby/collection.rb', line 11

def initialize(name, ruby_types = {})
  @name = name
  @ruby_types = ruby_types
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/json2ruby/collection.rb', line 5

def name
  @name
end

#original_nameObject

Returns the value of attribute original_name.



5
6
7
# File 'lib/json2ruby/collection.rb', line 5

def original_name
  @original_name
end

#ruby_typesObject

Returns the value of attribute ruby_types.



5
6
7
# File 'lib/json2ruby/collection.rb', line 5

def ruby_types
  @ruby_types
end

Class Method Details

.parse_from(name, obj_array, options = {}) ⇒ Object



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
# File 'lib/json2ruby/collection.rb', line 16

def self.parse_from(name, obj_array, options = {})
  ob = self.new(name)
  obj_array.each do |v|
    if v.kind_of?(Array)
      arr = Collection.parse_from(Entity.get_next_unknown, v, options)
      ob.ruby_types[arr.attr_hash] = arr
    elsif v.kind_of?(String)
      ob.ruby_types[RUBYSTRING.attr_hash] = RUBYSTRING
    elsif v.kind_of?(Integer) && !options[:forcenumeric]
      ob.ruby_types[RUBYINTEGER.attr_hash] = RUBYINTEGER
    elsif v.kind_of?(Float) && !options[:forcenumeric]
      ob.ruby_types[RUBYFLOAT.attr_hash] = RUBYFLOAT
    elsif (v.kind_of?(Float) || v.kind_of?(Integer)) && options[:forcenumeric]
      ob.ruby_types[RUBYNUMERIC.attr_hash] = RUBYNUMERIC
    elsif !!v==v
      ob.ruby_types[RUBYBOOLEAN.attr_hash] = RUBYBOOLEAN
    elsif v.kind_of?(Hash)
      ent = Entity.parse_from(Entity.get_next_unknown, v, options)
      ob.ruby_types[ent.attr_hash] = ent
    end
  end

  x = ob.attr_hash
  return Entity.entities[x] if Entity.entities.has_key?(x)
  Entity.entities[x] = ob
  ob
end

.short_nameObject



7
8
9
# File 'lib/json2ruby/collection.rb', line 7

def self.short_name
  "Collection"
end

Instance Method Details

#==(other) ⇒ Object



52
53
54
55
# File 'lib/json2ruby/collection.rb', line 52

def ==(other)
  return false if other.class != self.class
  attr_hash == other.attr_hash
end

#attr_hashObject



44
45
46
47
48
49
50
# File 'lib/json2ruby/collection.rb', line 44

def attr_hash
  md5 = Digest::MD5.new
  @ruby_types.each do |k,typ|
    md5.update typ.attr_hash
  end
  md5.hexdigest
end

#commentObject



57
58
59
60
61
# File 'lib/json2ruby/collection.rb', line 57

def comment
  x = "#{@name}[]"
  x += " (#{@original_name})" unless @original_name.nil?
  x
end