Class: CaseyJones::ActiveRecord::StringReader

Inherits:
Object
  • Object
show all
Defined in:
lib/casey_jones/string_reader.rb

Constant Summary collapse

KEY_SYMPTOM =
"*"
POSS_SYMPTOM =
"-"
START_COMMENT =
"{"
ONE_LINE_COMMENT =
"/"
END_COMMENT =
"}"
NEWLINE =
"\n"
DELIM =
[START_COMMENT, NEWLINE, ONE_LINE_COMMENT, END_COMMENT]
MULTI_LINE =
:multi_line
ONE_LINE =
:one_line
NAME =
:name
COMMENT =
[MULTI_LINE, ONE_LINE]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.decorate_symptom(obj) ⇒ Object



107
108
109
110
111
112
# File 'lib/casey_jones/string_reader.rb', line 107

def self.decorate_symptom(obj)
  decorator = ""
  decorator = KEY_SYMPTOM if obj.key_symptom
  decorator = POSS_SYMPTOM if obj.maybe
  "#{decorator}#{obj.symptom_name}"
end

.parse_symptom(obj, symptom) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/casey_jones/string_reader.rb', line 95

def self.parse_symptom(obj, symptom)
  if symptom.index(KEY_SYMPTOM) == 0
    obj.key_symptom = true
    obj.symptom_name = symptom[KEY_SYMPTOM.length..symptom.length]
  elsif symptom.index(POSS_SYMPTOM) == 0
    obj.maybe = true
    obj.symptom_name = symptom[POSS_SYMPTOM.length..symptom.length]
  else
    obj.symptom_name = symptom
  end
end

Instance Method Details

#read_items(str, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/casey_jones/string_reader.rb', line 45

def read_items(str, &block)
  @items = []
  buffer = ""
  @item = ""
  @comment = nil
  state = NAME

  str.chars.each do |char|
    if (char == "/") && (state != MULTI_LINE)
      @item = buffer.strip
      buffer = ""
      state = ONE_LINE
    elsif char == "{"
      @item = buffer.strip
      buffer = ""
      state = MULTI_LINE
    elsif char == "}"
      @comment = buffer.strip unless buffer.strip.empty?
      buffer = ""
      save(block)
      state = NAME
    elsif char == "\n"
      if state == NAME
        @item = buffer.strip
        buffer = ""
        save(block)
      elsif state == ONE_LINE
        @comment = buffer.strip
        state = NAME
        buffer = ""
        save(block)
      else
        buffer << char
      end
    else
      buffer << char
    end
  end

  if state == ONE_LINE
    @comment = buffer
  elsif state == MULTI_LINE
    @comment = buffer
  else
    @item = buffer
  end
  save(block)
  @items
end

#save(block) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/casey_jones/string_reader.rb', line 22

def save(block)
  unless @item.strip.empty?
    @items << block.call(@item.strip, @comment.nil? ? nil : @comment.strip)
  end
  @item = ""
  @comment = nil
end

#write_items(coll, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/casey_jones/string_reader.rb', line 30

def write_items(coll, &block)
  lines = []
  coll.each do |obj|
    item, comment = block.call(obj)
    if comment.nil? || comment.empty?
      lines << item
    elsif comment.index("/") || comment.index("\n")
      lines << "#{item} {#{comment}}"
    else
      lines << "#{item} / #{comment}"
    end
  end
  lines.join("\n")
end