Class: JSON2Ruby::Entity
- Inherits:
-
Object
- Object
- JSON2Ruby::Entity
- Defined in:
- lib/json2ruby/entity.rb
Overview
Entity represents a JSON Object.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
A Hash of String names to Attribute instances for this Entity, representing its attributes.
-
#name ⇒ Object
The String name of the Object - i.e.
-
#original_name ⇒ Object
The original String name of the object in the JSON ([^A-Za-z0-9_] are replaced with ‘_’).
Class Method Summary collapse
-
.entities ⇒ Object
Return the type cache of all Entity objects.
-
.get_next_unknown ⇒ Object
Return a string of the form ‘Unknown<x>’ where <x> is a globally unique sequence.
-
.parse_from(name, obj_hash, options = {}) ⇒ Object
Create a new, or return an existing, Entity named name that supports all fields in obj_hash.
-
.reset_parse ⇒ Object
Reset the internal type cache for all Entities everywhere, and reset the global Unknown number.
-
.short_name ⇒ Object
The short name is ‘Entity’.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compare this Entity with another.
-
#attr_hash ⇒ Object
Return a 128-bit hash as a hex string, representative of the unique set of fields and their types, including all subobjects.
-
#comment ⇒ Object
Return a string of the form ‘ (<y>)’ where <y> is the original_name of the Entity.
-
#initialize(name, attributes = {}) ⇒ Entity
constructor
Create a new Entity with the specified name and optional Hash of attributes (String name to Entity, Collection or Primitive instances).
Constructor Details
#initialize(name, attributes = {}) ⇒ Entity
Create a new Entity with the specified name and optional Hash of attributes (String name to Entity, Collection or Primitive instances)
19 20 21 22 |
# File 'lib/json2ruby/entity.rb', line 19 def initialize(name, attributes = {}) @name = name @attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Object
A Hash of String names to Attribute instances for this Entity, representing its attributes.
11 12 13 |
# File 'lib/json2ruby/entity.rb', line 11 def attributes @attributes end |
#name ⇒ Object
The String name of the Object - i.e. the field name in which it was first encountered.
7 8 9 |
# File 'lib/json2ruby/entity.rb', line 7 def name @name end |
#original_name ⇒ Object
The original String name of the object in the JSON ([^A-Za-z0-9_] are replaced with ‘_’)
9 10 11 |
# File 'lib/json2ruby/entity.rb', line 9 def original_name @original_name end |
Class Method Details
.entities ⇒ Object
Return the type cache of all Entity objects. This is a Hash of hash_attr values to Entity instances.
95 96 97 |
# File 'lib/json2ruby/entity.rb', line 95 def self.entities @@objs end |
.get_next_unknown ⇒ Object
Return a string of the form ‘Unknown<x>’ where <x> is a globally unique sequence.
100 101 102 103 104 |
# File 'lib/json2ruby/entity.rb', line 100 def self.get_next_unknown @@unknowncount ||= 0 @@unknowncount += 1 "Unknown#{@@unknowncount}" end |
.parse_from(name, obj_hash, options = {}) ⇒ Object
Create a new, or return an existing, Entity named name that supports all fields in obj_hash. 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.
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 91 |
# File 'lib/json2ruby/entity.rb', line 61 def self.parse_from(name, obj_hash, = {}) ob = self.new(name) obj_hash.each do |k,v| orig = k k = k.gsub(/[^A-Za-z0-9_]/, "_") if v.kind_of?(Array) att = Collection.parse_from(k, v, ) elsif v.kind_of?(String) att = RUBYSTRING elsif v.kind_of?(Integer) && ![:forcenumeric] att = RUBYINTEGER elsif v.kind_of?(Float) && ![:forcenumeric] att = RUBYFLOAT elsif (v.kind_of?(Integer) || v.kind_of?(Float)) && [:forcenumeric] att = RUBYNUMERIC elsif !!v==v att = RUBYBOOLEAN elsif v.kind_of?(Hash) att = self.parse_from(k, v, ) end att.original_name = orig if orig != k ob.attributes[k] = att end x = ob.attr_hash return @@objs[x] if @@objs.has_key?(x) @@objs[x] = ob ob end |
.reset_parse ⇒ Object
Reset the internal type cache for all Entities everywhere, and reset the global Unknown number.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/json2ruby/entity.rb', line 45 def self.reset_parse @@objs = { RUBYSTRING.attr_hash => RUBYSTRING, RUBYINTEGER.attr_hash => RUBYINTEGER, RUBYFLOAT.attr_hash => RUBYFLOAT, RUBYBOOLEAN.attr_hash => RUBYBOOLEAN, RUBYNUMERIC.attr_hash => RUBYNUMERIC, } @@unknowncount = 0 end |
.short_name ⇒ Object
The short name is ‘Entity’
14 15 16 |
# File 'lib/json2ruby/entity.rb', line 14 def self.short_name "Entity" end |
Instance Method Details
#==(other) ⇒ Object
Compare this Entity with another. An entity is equal to another entity if and only if it has:
-
The same number of fields
-
The fields have the same case-sensitive name
-
The fields have the same types, as tested with ‘attr_hash`
i.e. in short, an entity is equal to another entity if and only if both attr_hash calls return the same value.
39 40 41 42 |
# File 'lib/json2ruby/entity.rb', line 39 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 unique set of fields and their types, including all subobjects. Internally, this is calculated as the MD5 of all field names and their type attr_hash calls.
26 27 28 29 30 31 32 |
# File 'lib/json2ruby/entity.rb', line 26 def attr_hash md5 = Digest::MD5.new @attributes.each do |k,v| md5.update "#{k}:#{v.attr_hash}" end md5.hexdigest end |
#comment ⇒ Object
Return a string of the form ‘ (<y>)’ where <y> is the original_name of the Entity
107 108 109 110 111 |
# File 'lib/json2ruby/entity.rb', line 107 def comment x = @name x += " (#{@original_name})" unless @original_name.nil? x end |