tweek 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b8c6ef673ab0665cb66d13f99dc0023475b897e7
4
- data.tar.gz: 00de0099ebd776a88efd3179dc7ead5d5306d386
3
+ metadata.gz: a1e291275563c2896f9c59ee25c11486e0a891f7
4
+ data.tar.gz: e97cac8c95c5beda3dc9ec48a3c4ea418c5d6bbc
5
5
  SHA512:
6
- metadata.gz: 276719adb0d4db1ed7c326a06b477676ae9a078b561ebe5a45df8b76cb4247a31f252cb0a40234008c76f83b6ba008f9d1b91644824729f64ddbd6dddbeac6b5
7
- data.tar.gz: 73b5560399d4374b0b13b21c39eeeffc94fc9023851cbb7e6dc344214a2ecf8f2c6c53defd6dadc6f4c07528ea1f25c14a0e54f7ca7580475b9857de2942bb1b
6
+ metadata.gz: 49c42da3fcfa156d2f12cd6fdf561fb2a010982f7e272ec45ec00d49f7f829021606cd8f26bd78679791ad8c58125ff360118ce2a9a5a69890b7c9432ea6e59e
7
+ data.tar.gz: b2c84d89a856596c074c941e757d047b902877a4a4ad513ca0b22e42bc88fb4f196b0140e9fd0cb4aececd9200f3a746648899520c3fa8c345a36b5080f1fc8e
@@ -0,0 +1,139 @@
1
+ # A tweek file consists of lines represented by Tweek::Entry
2
+ #
3
+ # All of the entries in the file have a line number and optional comment
4
+ # other attributes depend on the specific type.
5
+ #
6
+ # Lifecycle: An entry is created either directly or parsed from an input line,
7
+ # then the 'condfail' method is called to see if it's relevant, if so
8
+ # the actual values are filled in according to the section. Then the 'mismatch'
9
+ # method can be used to see if it matched. Finally the 'generate' method
10
+ # produces a human-readable output
11
+ #
12
+ require 'ostruct'
13
+
14
+ class Tweek::Entry < OpenStruct
15
+
16
+ # Not all entries are settable: record if this entry was used to set/reset a value or not
17
+ attr_accessor :mode
18
+
19
+ # The type of this entry:
20
+ # :literal With line attributes - single lines
21
+ # :section With section type and list attributes
22
+ # :parameter With parameter value and conditional attributes
23
+ #
24
+ attr_reader :type
25
+
26
+ def initialize section, type, h
27
+ raise "Invalid Entry type #{type}" unless [:literal, :parameter, :section].include? type
28
+ super h
29
+ @section = section
30
+ @type = type
31
+ @mode = :query
32
+ @condfail = nil
33
+ @mismatch = nil
34
+ end
35
+
36
+ # Parse an input line
37
+ #
38
+ def self.parse section, line
39
+
40
+ line.chomp!
41
+ comment = line.slice!(/#.*$/)
42
+ comment.slice!(/^#\s*/) if comment
43
+ entry = case
44
+ when line.empty?
45
+ Tweek::Entry.new( section, :literal, :line => line, :comment => comment )
46
+
47
+ when /^\s*([A-Z0-9]+)\s*(.*?)\s*$/ =~ line # We've hit a new section
48
+ Tweek::Entry.new( section, :section, :section_type => $1.to_sym, :list => $2,
49
+ :comment => comment )
50
+
51
+ when /^\s*(.+?)\s*=\s*(.*?)\s*(\bif\b(.*))?$/ =~ line # A parameter line
52
+ param = $1
53
+ value = $2
54
+ raise "Missing condition after 'if'" if $4 and $4.strip.empty?
55
+ cond = $4.nil? ? nil : $4.strip
56
+ was = /\s*\[was\s+(.*)\]/.match(comment)
57
+ if was
58
+ comment = was.pre_match+was.post_match
59
+ was = was.captures.first
60
+ end
61
+ expected = /\s*\[expected\s+(.*)\]/.match(comment)
62
+ if expected
63
+ comment = expected.pre_match+was.post_match
64
+ expected = expected.captures.first
65
+ end
66
+ comment = nil if comment and comment.empty?
67
+ Tweek::Entry.new( section, :parameter, :param => param, :value => value,
68
+ :cond => cond, :comment => comment,
69
+ :was => was, :expected => expected )
70
+ else
71
+ raise "Unrecognized line"
72
+ end
73
+ return entry
74
+ end
75
+
76
+ # Check if any associated condition is true or false
77
+ #
78
+ def condfail
79
+ @condfail ||= @section.twf.condfail(cond)
80
+ end
81
+
82
+ # Check if the entry matches reality
83
+ #
84
+ def mismatch
85
+ raise "Value or Actual missing! Can't check for mismatch" if value.nil? or actual.nil?
86
+ @mismatch ||= !(value === actual)
87
+ end
88
+
89
+ # Generate an output line for this entry
90
+ #
91
+ def generate
92
+
93
+ outline = nil
94
+
95
+ case self.type
96
+
97
+ when :literal
98
+ outline = line
99
+ outline += "# #{comment}" if comment
100
+
101
+ when :section
102
+ if section_type.nil?
103
+ outline = nil
104
+ else
105
+ name = list_element || list
106
+ outline = "#{section_type} #{name} #{comment}"
107
+ end
108
+
109
+ when :parameter
110
+ condout = cond.nil? ? "": " if #{cond}"
111
+ if condfail
112
+ body = "#{param} = #{value}#{condout.colorize(:red)}"
113
+ note = nil
114
+ else
115
+ swap! if mode != :query
116
+ if mismatch
117
+ body = "#{param} = #{actual.colorize(:red)}#{condout.colorize(:green)}"
118
+ note = " [#{mode != :query ? 'was' : 'expected'} #{value.colorize(:yellow)}]"
119
+ else
120
+ body = "#{param} = #{actual}#{condout.colorize(:green)}"
121
+ note = nil
122
+ end
123
+ end
124
+ if comment.nil?
125
+ outline = (note ? (body + " #" + note) : body)
126
+ else
127
+ outline = (note ? (body + " # " + comment + note) : (body + " # " + comment))
128
+ end
129
+ end
130
+ return outline
131
+
132
+ end
133
+
134
+ # Method to toggle the actual and expected values, used for :query, :set, :reset
135
+ def swap!
136
+ self.actual, self.value = self.value, self.actual
137
+ end
138
+
139
+ end
data/lib/tweek/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tweek
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tweek
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Townsend
@@ -79,6 +79,7 @@ files:
79
79
  - README.md
80
80
  - bin/tweek
81
81
  - lib/tweek.rb
82
+ - lib/tweek/entry.rb
82
83
  - lib/tweek/file.rb
83
84
  - lib/tweek/section.rb
84
85
  - lib/tweek/version.rb