Class: Tweek::Entry

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/tweek/entry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(section, type, h) ⇒ Entry

Returns a new instance of Entry.



26
27
28
29
30
31
32
33
34
# File 'lib/tweek/entry.rb', line 26

def initialize section, type, h
  raise "Invalid Entry type #{type}" unless [:literal, :parameter, :section].include? type
  super h
  @section = section
  @type = type
  @mode = :query
  @condfail = nil
  @mismatch = nil
end

Instance Attribute Details

#modeObject

Not all entries are settable: record if this entry was used to set/reset a value or not



17
18
19
# File 'lib/tweek/entry.rb', line 17

def mode
  @mode
end

#typeObject (readonly)

The type of this entry: :literal With line attributes - single lines :section With section type and list attributes :parameter With parameter value and conditional attributes



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

def type
  @type
end

Class Method Details

.parse(section, line) ⇒ Object

Parse an input line



38
39
40
41
42
43
44
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
# File 'lib/tweek/entry.rb', line 38

def self.parse section, line

  line.chomp!
  comment = line.slice!(/#.*$/)
  comment.slice!(/^#\s*/) if comment
  entry = case
          when line.empty?
            Tweek::Entry.new( section, :literal, :line => line, :comment => comment )

          when /^\s*([A-Z0-9]+)\s*(.*?)\s*$/ =~ line # We've hit a new section
            Tweek::Entry.new( section, :section, :section_type => $1.to_sym, :list => $2,
                              :comment => comment )

          when /^\s*(.+?)\s*=\s*(.*?)\s*(\bif\b(.*))?$/ =~ line # A parameter line
            param = $1
            value = $2
            raise "Missing condition after 'if'" if $4 and $4.strip.empty?
            cond = $4.nil? ? nil : $4.strip
            was = /\s*\[was\s+(.*)\]/.match(comment)
            if was
              comment = was.pre_match+was.post_match
              was = was.captures.first
            end
            expected = /\s*\[expected\s+(.*)\]/.match(comment)
            if expected
              comment = expected.pre_match+was.post_match
              expected = expected.captures.first
            end
            comment = nil if comment and comment.empty?
            Tweek::Entry.new( section, :parameter, :param => param, :value => value,
                              :cond => cond, :comment => comment,
                              :was => was, :expected => expected )
          else
            raise "Unrecognized line"
          end
  return entry
end

Instance Method Details

#condfailObject

Check if any associated condition is true or false



78
79
80
# File 'lib/tweek/entry.rb', line 78

def condfail
  @condfail ||= @section.twf.condfail(cond)
end

#generateObject

Generate an output line for this entry



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/tweek/entry.rb', line 91

def generate

  outline = nil

  case self.type

  when :literal
    outline = line
    outline += "# #{comment}" if comment

  when :section
    if section_type.nil?
      outline = nil
    else
      name = list_element || list
      outline = "#{section_type} #{name} #{comment}"
    end

  when :parameter
    condout = cond.nil? ? "": " if #{cond}"
    if condfail
      body = "#{param} = #{value}#{condout.colorize(:red)}"
      note = nil
    else
      swap! if mode != :query
      if mismatch
        body = "#{param} = #{actual.colorize(:red)}#{condout.colorize(:green)}"
        note = " [#{mode != :query ? 'was' : 'expected'} #{value.colorize(:yellow)}]"
      else
        body = "#{param} = #{actual}#{condout.colorize(:green)}"
        note = nil
      end
    end
    if comment.nil?
      outline = (note ? (body + " #" + note) : body)
    else
      outline = (note ? (body + " # " + comment + note) : (body + " # " + comment))
    end
  end
  return outline

end

#mismatchObject

Check if the entry matches reality



84
85
86
87
# File 'lib/tweek/entry.rb', line 84

def mismatch
  raise "Value or Actual missing! Can't check for mismatch" if value.nil? or actual.nil?
  @mismatch ||= !(value === actual)
end

#swap!Object

Method to toggle the actual and expected values, used for :query, :set, :reset



135
136
137
# File 'lib/tweek/entry.rb', line 135

def swap!
    self.actual, self.value = self.value, self.actual
end