Class: JSON2Ruby::Attribute

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

Overview

The Attribute class represents a field on an Entity - i.e. A field name and associated type.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ruby_type = nil) ⇒ Attribute

Create a new attribute with the supplied String name and optional String type name. If the type is not supplied or nil, if will be assigned ‘_unknown’



20
21
22
23
# File 'lib/json2ruby/attribute.rb', line 20

def initialize(name, ruby_type = nil)
  @name = name
  @ruby_type = ruby_type || "_unknown"
end

Instance Attribute Details

#nameObject

The String name of the Attribute



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

def name
  @name
end

#original_nameObject

The original String name of the Array in the JSON ([^A-Za-z0-9_] are replaced with ‘_’)



9
10
11
# File 'lib/json2ruby/attribute.rb', line 9

def original_name
  @original_name
end

#ruby_typeObject

The type of the attribute, i.e. a Entity, Collection or Primitive instance



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

def ruby_type
  @ruby_type
end

Class Method Details

.short_nameObject

The short name is ‘Attribute’



14
15
16
# File 'lib/json2ruby/attribute.rb', line 14

def self.short_name
  "Attribute"
end

Instance Method Details

#==(other) ⇒ Object

An Attribute is equal to another if and only if its attr_hash value is the same.



31
32
33
34
# File 'lib/json2ruby/attribute.rb', line 31

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

#attr_hashObject

Return the MD5 hash of the name and the type.



26
27
28
# File 'lib/json2ruby/attribute.rb', line 26

def attr_hash
  Digest::MD5.hexdigest("#{@name}:#{@ruby_type}")
end