Class: Apstrings::Line
- Inherits:
-
Object
- Object
- Apstrings::Line
- Defined in:
- lib/apstrings/line.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#in_comment ⇒ Object
Returns the value of attribute in_comment.
Instance Method Summary collapse
- #cleaned_content ⇒ Object
- #close_comment ⇒ Object
- #close_comment? ⇒ Boolean
- #empty_line? ⇒ Boolean
-
#initialize(line) ⇒ Line
constructor
A new instance of Line.
- #is_comment? ⇒ Boolean
- #key ⇒ Object
- #key_value_pair? ⇒ Boolean
- #open_comment ⇒ Object
- #open_comment? ⇒ Boolean
- #slash_comment? ⇒ Boolean
- #valid? ⇒ Boolean
- #value ⇒ Object
- #whole_comment ⇒ Object
- #whole_comment? ⇒ Boolean
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
#content ⇒ Object (readonly)
Returns the value of attribute content.
3 4 5 |
# File 'lib/apstrings/line.rb', line 3 def content @content end |
#in_comment ⇒ Object
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_content ⇒ Object
49 50 51 |
# File 'lib/apstrings/line.rb', line 49 def cleaned_content content.gsub(/;\s*$/, "") end |
#close_comment ⇒ Object
36 37 38 |
# File 'lib/apstrings/line.rb', line 36 def close_comment /^(.+)\*\/\s*$/.match(content).to_s end |
#close_comment? ⇒ Boolean
40 41 42 |
# File 'lib/apstrings/line.rb', line 40 def close_comment? !(close_comment.empty?) end |
#empty_line? ⇒ Boolean
12 13 14 |
# File 'lib/apstrings/line.rb', line 12 def empty_line? /^\s*$/.match(content) || false end |
#is_comment? ⇒ 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 |
#key ⇒ Object
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
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_comment ⇒ Object
28 29 30 |
# File 'lib/apstrings/line.rb', line 28 def open_comment /^\/\*(.+)$/.match(content).to_s end |
#open_comment? ⇒ Boolean
32 33 34 |
# File 'lib/apstrings/line.rb', line 32 def open_comment? !(open_comment.empty?) end |
#slash_comment? ⇒ Boolean
16 17 18 |
# File 'lib/apstrings/line.rb', line 16 def slash_comment? content.strip.start_with?("//") end |
#valid? ⇒ 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 |
#value ⇒ Object
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_comment ⇒ Object
20 21 22 |
# File 'lib/apstrings/line.rb', line 20 def whole_comment /((^\/\*(.+)\*\/)|(^\/\/(.+)))/.match(content).to_s end |
#whole_comment? ⇒ Boolean
24 25 26 |
# File 'lib/apstrings/line.rb', line 24 def whole_comment? !(whole_comment.empty?) end |