Class: GSONClassGenerator::JavaClass

Inherits:
Object
  • Object
show all
Defined in:
lib/gson-class-generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name, json, depth = 0) ⇒ JavaClass

Returns a new instance of JavaClass.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gson-class-generator.rb', line 86

def initialize(class_name, json, depth = 0)
	@class_name = class_name
	@nested_classes = Array.new
	@fields = Array.new
	@depth = depth
	
	if (json.is_a?(Array))
		
		# Pull out first object to examine what it looks like
		first_json = json.first
		fields = self.class.new(nil, first_json).fields
		
		@fields += fields
	
	else
	
		json.each do |k, v|
			field = JavaField.new(k, v)
			
			if (field.custom_class?)
				nested_name = self.class.get_nested_class_name(field)
				nested_class = self.class.new(nested_name, v, depth + 1)
				@nested_classes << nested_class
			end
			
			@fields << field
		end
		
	end
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



84
85
86
# File 'lib/gson-class-generator.rb', line 84

def class_name
  @class_name
end

#fieldsObject (readonly)

Returns the value of attribute fields.



84
85
86
# File 'lib/gson-class-generator.rb', line 84

def fields
  @fields
end

Instance Method Details

#write(stream, opts) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gson-class-generator.rb', line 117

def write(stream, opts)
	tabs = self.class.indentation(@depth)
	field_tabs = self.class.indentation(@depth + 1)
	
	stream << "#{tabs}#{class_definition}\n"
	stream << "#{tabs}{\n"
	write_field_declarations(stream, field_tabs, opts)
	write_constructor(stream, field_tabs, opts)
	write_field_accessors(stream, field_tabs, opts)
	write_nested_classes(stream, opts)
	stream << "#{tabs}}\n"
end