Class: Languages::Ruby::AttributeRuby

Inherits:
Attribute
  • Object
show all
Defined in:
lib/kuniri/language/ruby/attribute_ruby.rb

Overview

Ruby Handling Ruby attributes

Instance Method Summary collapse

Constructor Details

#initializeAttributeRuby

Returns a new instance of AttributeRuby.



15
16
17
18
# File 'lib/kuniri/language/ruby/attribute_ruby.rb', line 15

def initialize
  @log = @settings = Kuniri::Setting.create.log
  @attributeList = []
end

Instance Method Details

#detect_attribute(pLine) ⇒ Object (protected)

Override



52
53
54
55
56
# File 'lib/kuniri/language/ruby/attribute_ruby.rb', line 52

def detect_attribute(pLine)
  regexExp = /^\s*(?:@|attr_(?:accessor|read|write))(.*)$/
  return nil unless pLine =~ regexExp
  return pLine.scan(regexExp)[0].join("")
end

#get_attribute(pLine) ⇒ Object

Get ruby attribute.

Parameters:

  • pLine

    Verify if line has a ruby attribute.

Returns:

  • Return AttributeData or nil.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kuniri/language/ruby/attribute_ruby.rb', line 23

def get_attribute(pLine)
  result = detect_attribute(pLine)
  return nil unless result

  @log.write_log("Info: Prepare to get attribute.")

  listOfAttributes = []

  # Has comma? Split string by comma
  result = remove_unnecessary_information(result)

  # Separated by comma, equal or the common case
  if result.scan(/,/).count >= 1
    listOfAttributes = handle_multiple_declaration_with_comma(result)
    @log.write_log("Debug: Declared with comma: #{listOfAttributes}")
  elsif result.scan(/=/).count > 1
    listOfAttributes = handle_multiple_declaration_with_equal(result)
    @log.write_log("Debug: separed by comma: #{listOfAttributes}")
  else
    listOfAttributes = handle_line_declaration(result)
    @log.write_log("Debug: One line declaration #{listOfAttributes}")
  end

  return listOfAttributes
end

#handle_line_declaration(pString) ⇒ Object (protected)

Override



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kuniri/language/ruby/attribute_ruby.rb', line 107

def handle_line_declaration(pString)
  listOfAttributes = []
  if pString =~ /=/
    pString = pString.scan(/.*=/).join("")
    return nil if pString =~ /\./
  end

  return nil if pString =~ /\./

  pString = prepare_final_string(pString)
  attribute = Languages::AttributeData.new(pString)
  listOfAttributes.push(attribute)

  return listOfAttributes
end

#handle_multiple_declaration_with_comma(pString) ⇒ Object (protected)

Override



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kuniri/language/ruby/attribute_ruby.rb', line 73

def handle_multiple_declaration_with_comma(pString)
  listOfAttributes = []
  pString = pString.split(",")
  pString.each do |variable|
    return nil if variable.scan(/=/).count > 1

    variable = variable.scan(/.*=/).join("") if variable =~ /.*=/

    return nil if variable =~ /\./

    variable = prepare_final_string(variable)
    attribute = Languages::AttributeData.new(variable)
    listOfAttributes.push(attribute)
  end

  return listOfAttributes
end

#handle_multiple_declaration_with_equal(pString) ⇒ Object (protected)

Override



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/kuniri/language/ruby/attribute_ruby.rb', line 92

def handle_multiple_declaration_with_equal(pString)
  listOfAttributes = []
  pString = pString.split("=")
  pString.each do |variable|
    return nil if variable =~ /\./

    variable = prepare_final_string(variable)
    attribute = Languages::AttributeData.new(variable)
    listOfAttributes.push(attribute)
  end

  return listOfAttributes
end

#prepare_final_string(pString) ⇒ Object (protected)

Override



65
66
67
68
69
70
# File 'lib/kuniri/language/ruby/attribute_ruby.rb', line 65

def prepare_final_string(pString)
  if pString =~ /\s+|:|@|=/
    return pString.gsub!(/\s+|:|@|=/,"")
  end
  return pString
end

#remove_unnecessary_information(pString) ⇒ Object (protected)

Override



59
60
61
62
# File 'lib/kuniri/language/ruby/attribute_ruby.rb', line 59

def remove_unnecessary_information(pString)
  return pString.gsub!(/\(.*\)/,"") if pString =~ /\(.*\)/
  return pString
end