Class: JSON2Ruby::Collection
- Inherits:
-
Object
- Object
- JSON2Ruby::Collection
- Defined in:
- lib/json2ruby/collection.rb
Overview
Collection represents a JSON Array
Instance Attribute Summary collapse
-
#name ⇒ Object
The String name of the Array, i.e.
-
#original_name ⇒ Object
The original String name of the Array in the JSON ([^A-Za-z0-9_] are replaced with ‘_’).
-
#ruby_types ⇒ Object
The array of types (Entity, Collection or Primitive instances) encountered in the JSON Array.
Class Method Summary collapse
-
.parse_from(name, obj_array, options = {}) ⇒ Object
Create a new, or return an existing, Collection named name that supports all types in obj_array.
-
.short_name ⇒ Object
The short name is ‘Collection’.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compare this Collection with another Collection.
-
#attr_hash ⇒ Object
Return a 128-bit hash as a hex string, representative of the set of possible types in the Collection Internally, this is calculated as the MD5 hash of the attr_hash values of all types.
-
#comment ⇒ Object
Generate a String comment of the form ‘<x>[] (<y)’ where <x> and <y> are the name and original name of the Collection respectively.
-
#initialize(name, ruby_types = {}) ⇒ Collection
constructor
Create a new Collection with the specified name and optional Array of ypes.
Constructor Details
#initialize(name, ruby_types = {}) ⇒ Collection
Create a new Collection with the specified name and optional Array of ypes.
19 20 21 22 |
# File 'lib/json2ruby/collection.rb', line 19 def initialize(name, ruby_types = {}) @name = name @ruby_types = ruby_types end |
Instance Attribute Details
#name ⇒ Object
The String name of the Array, i.e. the field name in which it was first encountered.
7 8 9 |
# File 'lib/json2ruby/collection.rb', line 7 def name @name end |
#original_name ⇒ Object
The original String name of the Array in the JSON ([^A-Za-z0-9_] are replaced with ‘_’)
9 10 11 |
# File 'lib/json2ruby/collection.rb', line 9 def original_name @original_name end |
#ruby_types ⇒ Object
The array of types (Entity, Collection or Primitive instances) encountered in the JSON Array
11 12 13 |
# File 'lib/json2ruby/collection.rb', line 11 def ruby_types @ruby_types end |
Class Method Details
.parse_from(name, obj_array, options = {}) ⇒ Object
Create a new, or return an existing, Collection named name that supports all types in obj_array. Optionally, options can be supplied:
-
:forcenumeric => true - Use RUBYNUMERIC instead of RUBYINTEGER / RUBYFLOAT.
Note: Contained JSON Objects and Arrays will be recursively parsed into Entity and Collection instances.
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 |
# File 'lib/json2ruby/collection.rb', line 29 def self.parse_from(name, obj_array, = {}) ob = self.new(name) obj_array.each do |v| if v.kind_of?(Array) arr = Collection.parse_from(Entity.get_next_unknown, v, ) ob.ruby_types[arr.attr_hash] = arr elsif v.kind_of?(String) ob.ruby_types[RUBYSTRING.attr_hash] = RUBYSTRING elsif v.kind_of?(Integer) && ![:forcenumeric] ob.ruby_types[RUBYINTEGER.attr_hash] = RUBYINTEGER elsif v.kind_of?(Float) && ![:forcenumeric] ob.ruby_types[RUBYFLOAT.attr_hash] = RUBYFLOAT elsif (v.kind_of?(Float) || v.kind_of?(Integer)) && [: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, ) 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_name ⇒ Object
The short name is ‘Collection’
14 15 16 |
# File 'lib/json2ruby/collection.rb', line 14 def self.short_name "Collection" end |
Instance Method Details
#==(other) ⇒ Object
Compare this Collection with another Collection. Two collections are equal if and only if they can contain exactly the same types.
68 69 70 71 |
# File 'lib/json2ruby/collection.rb', line 68 def ==(other) return false if other.class != self.class attr_hash == other.attr_hash end |
#attr_hash ⇒ Object
Return a 128-bit hash as a hex string, representative of the set of possible types in the Collection Internally, this is calculated as the MD5 hash of the attr_hash values of all types.
59 60 61 62 63 64 65 |
# File 'lib/json2ruby/collection.rb', line 59 def attr_hash md5 = Digest::MD5.new @ruby_types.each do |k,typ| md5.update typ.attr_hash end md5.hexdigest end |
#comment ⇒ Object
Generate a String comment of the form ‘<x>[] (<y)’ where <x> and <y> are the name and original name of the Collection respectively.
74 75 76 77 78 |
# File 'lib/json2ruby/collection.rb', line 74 def comment x = "#{@name}[]" x += " (#{@original_name})" unless @original_name.nil? x end |