Class: JSON2Ruby::Entity
- Inherits:
-
Object
- Object
- JSON2Ruby::Entity
- Defined in:
- lib/json2ruby/entity.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#name ⇒ Object
Returns the value of attribute name.
-
#original_name ⇒ Object
Returns the value of attribute original_name.
Class Method Summary collapse
- .entities ⇒ Object
- .get_next_unknown ⇒ Object
- .parse_from(name, obj_hash, options = {}) ⇒ Object
- .reset_parse ⇒ Object
- .short_name ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #attr_hash ⇒ Object
- #comment ⇒ Object
-
#initialize(name, attributes = {}) ⇒ Entity
constructor
A new instance of Entity.
Constructor Details
#initialize(name, attributes = {}) ⇒ Entity
Returns a new instance of Entity.
12 13 14 15 |
# File 'lib/json2ruby/entity.rb', line 12 def initialize(name, attributes = {}) @name = name @attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/json2ruby/entity.rb', line 6 def attributes @attributes end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/json2ruby/entity.rb', line 6 def name @name end |
#original_name ⇒ Object
Returns the value of attribute original_name.
6 7 8 |
# File 'lib/json2ruby/entity.rb', line 6 def original_name @original_name end |
Class Method Details
.entities ⇒ Object
73 74 75 |
# File 'lib/json2ruby/entity.rb', line 73 def self.entities @@objs end |
.get_next_unknown ⇒ Object
77 78 79 80 81 |
# File 'lib/json2ruby/entity.rb', line 77 def self.get_next_unknown @@unknowncount ||= 0 @@unknowncount += 1 "Unknown#{@@unknowncount}" end |
.parse_from(name, obj_hash, options = {}) ⇒ Object
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 |
# File 'lib/json2ruby/entity.rb', line 41 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
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/json2ruby/entity.rb', line 30 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
8 9 10 |
# File 'lib/json2ruby/entity.rb', line 8 def self.short_name "Entity" end |
Instance Method Details
#==(other) ⇒ Object
25 26 27 28 |
# File 'lib/json2ruby/entity.rb', line 25 def ==(other) return false if other.class != self.class attr_hash == other.attr_hash end |
#attr_hash ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/json2ruby/entity.rb', line 17 def attr_hash md5 = Digest::MD5.new @attributes.each do |k,v| md5.update "#{k}:#{v.attr_hash}" end md5.hexdigest end |
#comment ⇒ Object
83 84 85 86 87 |
# File 'lib/json2ruby/entity.rb', line 83 def comment x = @name x += " (#{@original_name})" unless @original_name.nil? x end |