Class: JSON2Ruby::Entity

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/json2ruby/entity.rb', line 6

def attributes
  @attributes
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/json2ruby/entity.rb', line 6

def name
  @name
end

#original_nameObject

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

.entitiesObject



73
74
75
# File 'lib/json2ruby/entity.rb', line 73

def self.entities
  @@objs
end

.get_next_unknownObject



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, options = {})
  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, options)
    elsif v.kind_of?(String)
      att = RUBYSTRING
    elsif v.kind_of?(Integer) && !options[:forcenumeric]
      att = RUBYINTEGER
    elsif v.kind_of?(Float) && !options[:forcenumeric]
      att = RUBYFLOAT
    elsif (v.kind_of?(Integer) || v.kind_of?(Float)) && options[:forcenumeric]
      att = RUBYNUMERIC
    elsif !!v==v
      att = RUBYBOOLEAN
    elsif v.kind_of?(Hash)
      att = self.parse_from(k, v, options)
    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_parseObject



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_nameObject



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_hashObject



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

#commentObject



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