Class: Apstrings::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/apstrings/line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ Line

Returns a new instance of Line.



6
7
8
9
10
# File 'lib/apstrings/line.rb', line 6

def initialize(line)
  @content = line
  @in_comment = false
  raise "ERROR : Line does not end with `;`, Line => {#{line}}" unless valid?
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



3
4
5
# File 'lib/apstrings/line.rb', line 3

def content
  @content
end

#in_commentObject

Returns the value of attribute in_comment.



4
5
6
# File 'lib/apstrings/line.rb', line 4

def in_comment
  @in_comment
end

Instance Method Details

#cleaned_contentObject



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

def cleaned_content
  content.gsub(/;\s*$/, "")
end

#close_commentObject



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

def close_comment
  /^(.+)\*\/\s*$/.match(content).to_s
end

#close_comment?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/apstrings/line.rb', line 40

def close_comment?
  !(close_comment.empty?)
end

#empty_line?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/apstrings/line.rb', line 12

def empty_line?
  /^\s*$/.match(content) || false
end

#is_comment?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/apstrings/line.rb', line 76

def is_comment?
 whole_comment? || open_comment? || close_comment? || in_comment || slash_comment?
end

#keyObject



61
62
63
64
65
# File 'lib/apstrings/line.rb', line 61

def key
  if key_value_pair?
    cleaned_content.partition(/"\s*=\s*"/)[0].gsub!(/(^")/, "")
  end
end

#key_value_pair?Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/apstrings/line.rb', line 44

def key_value_pair?
  #Bugfix:  支持 "Key text \" with \" " = "Value text \" with \" ";  格式
  !!(/^\s*"([^"]+)[\S\s]*=/.match(content))
end

#open_commentObject



28
29
30
# File 'lib/apstrings/line.rb', line 28

def open_comment
  /^\/\*(.+)$/.match(content).to_s
end

#open_comment?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/apstrings/line.rb', line 32

def open_comment?
  !(open_comment.empty?)
end

#slash_comment?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/apstrings/line.rb', line 16

def slash_comment?
    content.strip.start_with?("//")
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  if key_value_pair?
    !!(/;[\s]*$/.match(content))
  else
    true
  end
end

#valueObject



67
68
69
70
71
72
73
74
# File 'lib/apstrings/line.rb', line 67

def value
  if key_value_pair?
    # puts cleaned_content.partition(/"\s*=\s*"/)[2]
    # Bugfix : 去掉引号后、冒号前的多余空格
    # "key" = "value"  ; 
    cleaned_content.partition(/"\s*=\s*"/)[2].rstrip.gsub!(/(^"|"$)/, "")
  end
end

#whole_commentObject



20
21
22
# File 'lib/apstrings/line.rb', line 20

def whole_comment
  /((^\/\*(.+)\*\/)|(^\/\/(.+)))/.match(content).to_s
end

#whole_comment?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/apstrings/line.rb', line 24

def whole_comment?
  !(whole_comment.empty?)
end