Class: Xml2Go::Struct

Inherits:
Object
  • Object
show all
Defined in:
lib/xml2go/struct.rb

Overview

class representing a Go struct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Struct

Returns a new instance of Struct.



38
39
40
41
# File 'lib/xml2go/struct.rb', line 38

def initialize(type)
  @type = type
  @fields = {}
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



36
37
38
# File 'lib/xml2go/struct.rb', line 36

def fields
  @fields
end

#typeObject

Returns the value of attribute type.



36
37
38
# File 'lib/xml2go/struct.rb', line 36

def type
  @type
end

Instance Method Details

#add_field(var_name, type, xml_tag, value = nil) ⇒ Object

adds member variable to struct



44
45
46
# File 'lib/xml2go/struct.rb', line 44

def add_field(var_name, type, xml_tag, value=nil)
  @fields[var_name] = Field.new(var_name, type, xml_tag, value)
end

#delete_field(var_name) ⇒ Object

adds member variable to struct



49
50
51
# File 'lib/xml2go/struct.rb', line 49

def delete_field(var_name)
  @fields.delete(var_name)
end

#get_constsObject



68
69
70
71
# File 'lib/xml2go/struct.rb', line 68

def get_consts
  consts_string = @fields.values.map{ |v| v.to_const}
  "// #{@type} info \n" << consts_string.join("\n")
end

#to_declarationObject



61
62
63
64
65
66
# File 'lib/xml2go/struct.rb', line 61

def to_declaration
  fields_string = @fields.values.map { |v| v.to_declaration}
  "#{Xml2Go::low(@type)} := #{@type} {
  #{fields_string.join("\n")}
  }"
end

#to_sObject



53
54
55
56
57
58
59
# File 'lib/xml2go/struct.rb', line 53

def to_s
  fields_string = @fields.values.join("\n")
  "type #{@type} struct {\n
  #{fields_string}
  }
  "
end