gloo 6.5.1 → 6.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/docs/application.md +2 -0
- data/docs/verbs.md +6 -2
- data/lib/VERSION +1 -1
- data/lib/VERSION_NOTES +12 -0
- data/lib/gloo/app/engine.rb +17 -1
- data/lib/gloo/app/settings.rb +40 -0
- data/lib/gloo/core/tokens.rb +8 -2
- data/lib/gloo/docs/help_shell.rb +46 -1
- data/lib/gloo/objs/basic/string_msgs.rb +20 -0
- data/lib/gloo/objs/basic/word_wrap.rb +58 -0
- data/lib/gloo/persist/file_loader.rb +16 -12
- data/lib/gloo/persist/file_storage.rb +20 -3
- data/lib/gloo/persist/persist_man.rb +31 -1
- data/lib/gloo/persist/source/source_doc.rb +36 -0
- data/lib/gloo/persist/subtree_extractor.rb +184 -0
- data/lib/gloo/persist/trivia_buffer.rb +120 -0
- data/lib/gloo/verbs/if.rb +18 -8
- data/lib/gloo/verbs/list.rb +36 -4
- data/lib/gloo/verbs/save.rb +24 -4
- data/lib/gloo/verbs/unless.rb +13 -4
- data/test.gloo/objs/string.test.gloo +17 -0
- data/test.gloo/objs/text.test.gloo +9 -0
- data/test.gloo/verbs/if.test.gloo +21 -0
- data/test.gloo/verbs/save.test.gloo +86 -0
- data/test.gloo/verbs/unless.test.gloo +21 -0
- metadata +9 -4
- data/lib/gloo/persist/comment_buffer.rb +0 -58
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# Author:: Eric Crane (mailto:eric.crane@mac.com)
|
|
2
|
+
# Copyright:: Copyright (c) 2026 Eric Crane. All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# Resolves 'save {obj} to {path}' into the Source::ObjNode that should
|
|
5
|
+
# be moved into the target file: pulled out of whichever file already
|
|
6
|
+
# owns it, or built fresh when obj has no file of its own yet. Does
|
|
7
|
+
# not touch disk -- PersistMan wires the result into a new
|
|
8
|
+
# FileStorage and drives the actual save (including re-saving the
|
|
9
|
+
# file the node was pulled out of).
|
|
10
|
+
#
|
|
11
|
+
|
|
12
|
+
module Gloo
|
|
13
|
+
module Persist
|
|
14
|
+
class SubtreeExtractor
|
|
15
|
+
|
|
16
|
+
SPANS_MULTIPLE_FILES_ERR =
|
|
17
|
+
"Can't extract: this object's subtree has declarations in more than one file. " \
|
|
18
|
+
'Save each contributing file on its own first.'.freeze
|
|
19
|
+
NO_OWN_DECLARATION_ERR =
|
|
20
|
+
"Can't extract: this object has no declaration of its own to move " \
|
|
21
|
+
'(it may be an auto-created intermediate container from nested-container ' \
|
|
22
|
+
'shorthand) -- extract a child that does, or a shallower ancestor that does.'.freeze
|
|
23
|
+
|
|
24
|
+
#
|
|
25
|
+
# Set up an extractor for the given engine.
|
|
26
|
+
#
|
|
27
|
+
def initialize( engine )
|
|
28
|
+
@engine = engine
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
#
|
|
32
|
+
# Resolve obj to the Source::ObjNode that should be written to
|
|
33
|
+
# the new file, with its name set to obj's full dotted path (so
|
|
34
|
+
# the target file recreates any prefix container chain via
|
|
35
|
+
# nested-container shorthand) and its indentation recomputed for
|
|
36
|
+
# life as a fresh top-level declaration.
|
|
37
|
+
#
|
|
38
|
+
# Returns [ node, source_fs ]: source_fs is the FileStorage obj
|
|
39
|
+
# was pulled out of (already updated in memory -- its own save is
|
|
40
|
+
# still the caller's job), or nil when obj had no file of its own
|
|
41
|
+
# to remove from. Returns [ nil, nil ] (having already called
|
|
42
|
+
# engine.err) if extraction isn't possible.
|
|
43
|
+
#
|
|
44
|
+
def extract( obj, maps )
|
|
45
|
+
owners = owning_file_storages( obj, maps )
|
|
46
|
+
return err( SPANS_MULTIPLE_FILES_ERR ) if owners.count > 1
|
|
47
|
+
|
|
48
|
+
source_fs = owners.first
|
|
49
|
+
node = source_fs ? remove_node( obj, source_fs ) : fresh_node( obj )
|
|
50
|
+
return err( NO_OWN_DECLARATION_ERR ) unless node
|
|
51
|
+
|
|
52
|
+
node.name = dotted_name( obj )
|
|
53
|
+
reindent( node, 0 )
|
|
54
|
+
return [ node, source_fs ]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
#
|
|
60
|
+
# Report the error and return [ nil, nil ], for a one-line call site.
|
|
61
|
+
#
|
|
62
|
+
def err( message )
|
|
63
|
+
@engine.err( message )
|
|
64
|
+
return [ nil, nil ]
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
#
|
|
68
|
+
# Every FileStorage whose SourceDoc has a node for obj itself, or
|
|
69
|
+
# for any of its descendants (a namespace pattern applied inside
|
|
70
|
+
# obj's own subtree, not just above it).
|
|
71
|
+
#
|
|
72
|
+
def owning_file_storages( obj, maps )
|
|
73
|
+
subtree = subtree_objects( obj )
|
|
74
|
+
return maps.select { |fs| fs.source_doc && any_node_for?( fs.source_doc.children, subtree ) }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
#
|
|
78
|
+
# obj and every object beneath it in the live heap, as a
|
|
79
|
+
# (compare-by-identity) set membership check.
|
|
80
|
+
#
|
|
81
|
+
def subtree_objects( obj )
|
|
82
|
+
set = {}.compare_by_identity
|
|
83
|
+
set[ obj ] = true
|
|
84
|
+
collect_descendants( obj, set )
|
|
85
|
+
return set
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
#
|
|
89
|
+
# Recursively add obj's live children to the set.
|
|
90
|
+
#
|
|
91
|
+
def collect_descendants( obj, set )
|
|
92
|
+
obj.children.each do |child|
|
|
93
|
+
set[ child ] = true
|
|
94
|
+
collect_descendants( child, set )
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
#
|
|
99
|
+
# Does any node in this list (or their descendants) belong to an
|
|
100
|
+
# object in the given set?
|
|
101
|
+
#
|
|
102
|
+
def any_node_for?( nodes, set )
|
|
103
|
+
return nodes.any? do |node|
|
|
104
|
+
next false unless node.is_a?( Source::ObjNode )
|
|
105
|
+
next true if node.obj && set.key?( node.obj )
|
|
106
|
+
|
|
107
|
+
any_node_for?( node.children, set )
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
#
|
|
112
|
+
# obj has no file of its own (never loaded, or created at
|
|
113
|
+
# runtime since) -- build a plain node with no raw text of its
|
|
114
|
+
# own. FileSaver renders it fresh from the live object, the same
|
|
115
|
+
# way it already does for any brand-new child.
|
|
116
|
+
#
|
|
117
|
+
def fresh_node( obj )
|
|
118
|
+
node = Source::ObjNode.new( :name => obj.name, :raw_type => obj.type_display )
|
|
119
|
+
node.obj = obj
|
|
120
|
+
return node
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
#
|
|
124
|
+
# Pull obj's node out of the file that owns it. Returns nil if
|
|
125
|
+
# that file turns out to have no node of obj's own to remove (eg.
|
|
126
|
+
# obj is an intermediate container ShorthandExpander created,
|
|
127
|
+
# which never gets a node of its own).
|
|
128
|
+
#
|
|
129
|
+
def remove_node( obj, fs )
|
|
130
|
+
node = fs.source_doc.extract( obj )
|
|
131
|
+
return nil unless node
|
|
132
|
+
|
|
133
|
+
fs.drop_root( obj )
|
|
134
|
+
return node
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
#
|
|
138
|
+
# obj's full dotted path from its file-owning root down to
|
|
139
|
+
# itself, eg. "app.core.settings" -- how the target file
|
|
140
|
+
# recreates the prefix chain via nested-container shorthand. A
|
|
141
|
+
# plain top-level object's path is just its own name.
|
|
142
|
+
#
|
|
143
|
+
def dotted_name( obj )
|
|
144
|
+
names = [ obj.name ]
|
|
145
|
+
o = obj.parent
|
|
146
|
+
until o.nil? || o.root?
|
|
147
|
+
names.unshift( o.name )
|
|
148
|
+
o = o.parent
|
|
149
|
+
end
|
|
150
|
+
return names.join( '.' )
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
#
|
|
154
|
+
# Recompute raw_indent (and, for a BEGIN/END node, raw_end_indent
|
|
155
|
+
# to match) from scratch for the moved node and every descendant
|
|
156
|
+
# ObjNode. Their old indentation depth described nesting inside
|
|
157
|
+
# the file they came from; here obj itself is depth 0, in a
|
|
158
|
+
# brand-new file. Floating trivia (a blank line or comment not
|
|
159
|
+
# attached as some declaration's leading_doc) travels along
|
|
160
|
+
# unindented-adjusted -- purely cosmetic, since indentation never
|
|
161
|
+
# affects how a comment/blank line is recognized on reload.
|
|
162
|
+
#
|
|
163
|
+
def reindent( node, depth )
|
|
164
|
+
new_indent = "\t" * depth
|
|
165
|
+
node.raw_indent = new_indent
|
|
166
|
+
node.raw_end_indent = new_indent if node.block_style == :begin_end
|
|
167
|
+
node.leading_doc = reindent_doc( node.leading_doc, new_indent ) if node.leading_doc
|
|
168
|
+
node.children.each do |child|
|
|
169
|
+
reindent( child, depth + 1 ) if child.is_a?( Source::ObjNode )
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
#
|
|
174
|
+
# Re-indent a raw leading_doc's lines to new_indent, replacing
|
|
175
|
+
# whatever leading whitespace each '#'-prefixed line had in the
|
|
176
|
+
# file it came from.
|
|
177
|
+
#
|
|
178
|
+
def reindent_doc( raw, new_indent )
|
|
179
|
+
return raw.split( "\n" ).map { |line| "#{new_indent}#{line.lstrip}" }.join( "\n" )
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
end
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# Author:: Eric Crane (mailto:eric.crane@mac.com)
|
|
2
|
+
# Copyright:: Copyright (c) 2026 Eric Crane. All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# Buffers a run of comment and blank lines while the loader decides
|
|
5
|
+
# where they actually belong -- both what becomes a following
|
|
6
|
+
# declaration's leading_doc, and which source node's children array
|
|
7
|
+
# floating trivia lands in.
|
|
8
|
+
#
|
|
9
|
+
# Both kinds are held here (not placed immediately) because a
|
|
10
|
+
# declaration only becomes a parent -- and its Source::ObjNode only
|
|
11
|
+
# gets pushed as the current node -- once IndentStack#place sees a
|
|
12
|
+
# genuinely deeper line follow it (see IndentStack). A blank or
|
|
13
|
+
# comment line between a container's declaration and its first child
|
|
14
|
+
# arrives *before* that push happens; placing it immediately would
|
|
15
|
+
# attach it one level too shallow. Buffering defers the decision until
|
|
16
|
+
# take_leading_doc/flush_into is called against the *correct*,
|
|
17
|
+
# already-placed node.
|
|
18
|
+
#
|
|
19
|
+
|
|
20
|
+
module Gloo
|
|
21
|
+
module Persist
|
|
22
|
+
class TriviaBuffer
|
|
23
|
+
|
|
24
|
+
#
|
|
25
|
+
# Set up an empty buffer.
|
|
26
|
+
#
|
|
27
|
+
def initialize
|
|
28
|
+
@pending = []
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
#
|
|
32
|
+
# Buffer one comment line. raw is the full source line (minus
|
|
33
|
+
# its trailing newline); tabs is its indentation level.
|
|
34
|
+
#
|
|
35
|
+
def push_comment( raw, tabs )
|
|
36
|
+
@pending << { :kind => :comment, :raw => raw, :tabs => tabs }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
#
|
|
40
|
+
# Buffer one blank line. raw is its exact source text (may be
|
|
41
|
+
# non-empty if it had trailing whitespace) -- a blank line always
|
|
42
|
+
# breaks a comment run's association with whatever declaration
|
|
43
|
+
# follows, the same as it always has.
|
|
44
|
+
#
|
|
45
|
+
def push_blank( raw )
|
|
46
|
+
@pending << { :kind => :blank, :raw => raw }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
#
|
|
50
|
+
# Detach the whole buffered run -- comments and blanks, in
|
|
51
|
+
# original order -- as floating nodes appended to the given
|
|
52
|
+
# children array.
|
|
53
|
+
#
|
|
54
|
+
def flush_into( children )
|
|
55
|
+
return if @pending.empty?
|
|
56
|
+
|
|
57
|
+
flush_list( @pending, children )
|
|
58
|
+
@pending = []
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
#
|
|
62
|
+
# If the run since the last blank (or the whole run, if it has no
|
|
63
|
+
# blank in it) is one or more comment lines all at the same
|
|
64
|
+
# indent as line_tabs, claim it as a leading_doc (raw,
|
|
65
|
+
# newline-joined). Anything before that -- an earlier blank, and
|
|
66
|
+
# whatever preceded it -- floats into children first, in order,
|
|
67
|
+
# since a blank always breaks the association. If the trailing
|
|
68
|
+
# run doesn't qualify (wrong indent, or empty), it floats into
|
|
69
|
+
# children too and this returns nil. Either way the buffer is
|
|
70
|
+
# empty afterward.
|
|
71
|
+
#
|
|
72
|
+
def take_leading_doc( line_tabs, children )
|
|
73
|
+
before, after = split_at_last_blank
|
|
74
|
+
flush_list( before, children )
|
|
75
|
+
|
|
76
|
+
if after.any? && after.last[ :tabs ] == line_tabs
|
|
77
|
+
doc = after.map { |t| t[ :raw ] }.join( "\n" )
|
|
78
|
+
@pending = []
|
|
79
|
+
return doc
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
flush_list( after, children )
|
|
83
|
+
@pending = []
|
|
84
|
+
return nil
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private
|
|
88
|
+
|
|
89
|
+
#
|
|
90
|
+
# Split the pending run at its last blank line: everything up to
|
|
91
|
+
# and including it, and everything after (always comments only,
|
|
92
|
+
# by construction -- there's no later blank to have split on).
|
|
93
|
+
# With no blank at all, everything is "after".
|
|
94
|
+
#
|
|
95
|
+
def split_at_last_blank
|
|
96
|
+
idx = @pending.rindex { |t| t[ :kind ] == :blank }
|
|
97
|
+
return [ [], @pending ] unless idx
|
|
98
|
+
|
|
99
|
+
return [ @pending[ 0..idx ], @pending[ idx + 1.. ] ]
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
#
|
|
103
|
+
# Append each pending entry's node, in order, to children.
|
|
104
|
+
#
|
|
105
|
+
def flush_list( list, children )
|
|
106
|
+
list.each { |t| children << node_for( t ) }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
#
|
|
110
|
+
# The source node one pending entry becomes when it floats.
|
|
111
|
+
#
|
|
112
|
+
def node_for( t )
|
|
113
|
+
return Source::BlankNode.new( t[ :raw ] ) if t[ :kind ] == :blank
|
|
114
|
+
|
|
115
|
+
return Source::CommentNode.new( t[ :raw ] )
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
data/lib/gloo/verbs/if.rb
CHANGED
|
@@ -11,7 +11,11 @@ module Gloo
|
|
|
11
11
|
KEYWORD = 'if'.freeze
|
|
12
12
|
KEYWORD_SHORT = 'if'.freeze
|
|
13
13
|
THEN = 'then'.freeze
|
|
14
|
+
DO = 'do'.freeze
|
|
14
15
|
ELSE = 'else'.freeze
|
|
16
|
+
# 'then' and 'do' are interchangeable -- whichever appears first
|
|
17
|
+
# in the tokens is the actual separator (see Tokens#index_of).
|
|
18
|
+
SEPARATORS = [ THEN, DO ].freeze
|
|
15
19
|
MISSING_EXPR_ERR = 'Missing Expression!'.freeze
|
|
16
20
|
|
|
17
21
|
#
|
|
@@ -21,7 +25,7 @@ module Gloo
|
|
|
21
25
|
value = value_tokens
|
|
22
26
|
return if value.nil?
|
|
23
27
|
|
|
24
|
-
@then = @tokens.expr_after(
|
|
28
|
+
@then = @tokens.expr_after( SEPARATORS, ELSE )
|
|
25
29
|
@else = @tokens.expr_after( ELSE )
|
|
26
30
|
|
|
27
31
|
if evals_true( value )
|
|
@@ -56,7 +60,7 @@ module Gloo
|
|
|
56
60
|
# of the if command.
|
|
57
61
|
#
|
|
58
62
|
def value_tokens
|
|
59
|
-
value = @tokens.before_token(
|
|
63
|
+
value = @tokens.before_token( SEPARATORS )
|
|
60
64
|
if value && value.count > 1
|
|
61
65
|
# The first token is the verb, so we drop it.
|
|
62
66
|
value = value[ 1..-1 ]
|
|
@@ -113,19 +117,22 @@ module Gloo
|
|
|
113
117
|
{
|
|
114
118
|
:name => KEYWORD,
|
|
115
119
|
:shortcut => KEYWORD_SHORT,
|
|
116
|
-
:description => 'If an expression is true then do something.'
|
|
120
|
+
:description => 'If an expression is true then do something. ' \
|
|
121
|
+
"'then' and 'do' are interchangeable separators -- pick " \
|
|
122
|
+
'whichever reads better.',
|
|
117
123
|
:syntax => [
|
|
118
|
-
'if {true} then {
|
|
119
|
-
'if {true}
|
|
124
|
+
'if {true} then {action}',
|
|
125
|
+
'if {true} do {action}',
|
|
126
|
+
'if {true} then {action} else {else action}'
|
|
120
127
|
],
|
|
121
128
|
:parameters => [
|
|
122
129
|
'{true} — Does the expression evaluate to true?',
|
|
123
|
-
'{
|
|
124
|
-
'{
|
|
130
|
+
'{action} — Execute command if the expression is true.',
|
|
131
|
+
'{else action} — The else command is optional. Execute command if the expression is false.'
|
|
125
132
|
],
|
|
126
133
|
:result => 'Unchanged if the expression is not true. If true, ' \
|
|
127
134
|
'then the result will be based on the command specified ' \
|
|
128
|
-
|
|
135
|
+
"after the 'then'/'do' keyword.",
|
|
129
136
|
:errors => [
|
|
130
137
|
"#{MISSING_EXPR_ERR} — No expression is provided as parameter to the verb.",
|
|
131
138
|
'Other errors depend on the command that is run.'
|
|
@@ -150,6 +157,9 @@ module Gloo
|
|
|
150
157
|
if ^.x \\
|
|
151
158
|
then show 'T' \\
|
|
152
159
|
else show 'F'
|
|
160
|
+
|
|
161
|
+
# 'do' works the same as 'then'
|
|
162
|
+
if if.x do show "still true: " + if.true_msg
|
|
153
163
|
EXAMPLES
|
|
154
164
|
}
|
|
155
165
|
end
|
data/lib/gloo/verbs/list.rb
CHANGED
|
@@ -92,21 +92,53 @@ module Gloo
|
|
|
92
92
|
|
|
93
93
|
#
|
|
94
94
|
# Show the object's doc (its leading comment, cleaned up) above
|
|
95
|
-
# its listing line, one '#'-prefixed line per line of doc
|
|
96
|
-
# Silent when the object has none.
|
|
95
|
+
# its listing line, one '#'-prefixed line per line of doc, word-
|
|
96
|
+
# wrapped to fit the terminal. Silent when the object has none.
|
|
97
97
|
#
|
|
98
98
|
def show_doc( obj, indent )
|
|
99
99
|
return if obj.doc.to_s.strip.empty?
|
|
100
100
|
|
|
101
|
-
|
|
101
|
+
prefix = "#{indent}# "
|
|
102
|
+
cont_indent = "#{indent} " # lines up under the text after '# '
|
|
103
|
+
width = doc_wrap_width( prefix.length )
|
|
104
|
+
|
|
102
105
|
# split( -1 ), not each_line -- a doc ending in a blank '#' line
|
|
103
106
|
# ends with "\n", and each_line silently drops that trailing
|
|
104
107
|
# empty line rather than yielding it.
|
|
105
108
|
obj.doc.split( "\n", -1 ).each do |line|
|
|
106
|
-
|
|
109
|
+
show_doc_line( line, prefix, cont_indent, width )
|
|
107
110
|
end
|
|
108
111
|
end
|
|
109
112
|
|
|
113
|
+
#
|
|
114
|
+
# Word-wrap a single doc line to width, then show each wrapped
|
|
115
|
+
# piece: the '#' prefix on the first, the aligned continuation
|
|
116
|
+
# indent on the rest.
|
|
117
|
+
#
|
|
118
|
+
def show_doc_line( line, prefix, cont_indent, width )
|
|
119
|
+
theme = @engine.theme
|
|
120
|
+
wrapped = Gloo::Objs::WordWrap.wrap( line, width )
|
|
121
|
+
# "".split( "\n", -1 ) is [], not [ '' ] -- a blank line still
|
|
122
|
+
# needs one piece so it renders as a bare '#', not nothing.
|
|
123
|
+
pieces = wrapped.empty? ? [ '' ] : wrapped.split( "\n", -1 )
|
|
124
|
+
pieces.each_with_index do |piece, i|
|
|
125
|
+
p = i.zero? ? prefix : cont_indent
|
|
126
|
+
@engine.log.show theme.muted( "#{p}#{piece}".rstrip )
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
#
|
|
131
|
+
# How many columns of text fit after the doc-line prefix. Falls
|
|
132
|
+
# back to a small minimum rather than a zero/negative width when
|
|
133
|
+
# the terminal is very narrow or the indent is very deep.
|
|
134
|
+
#
|
|
135
|
+
def doc_wrap_width( prefix_length )
|
|
136
|
+
width = Gloo::App::Settings.cols( @engine ) - prefix_length
|
|
137
|
+
return width if width.positive?
|
|
138
|
+
|
|
139
|
+
return 20
|
|
140
|
+
end
|
|
141
|
+
|
|
110
142
|
#
|
|
111
143
|
# Determine how many levels to show.
|
|
112
144
|
#
|
data/lib/gloo/verbs/save.rb
CHANGED
|
@@ -73,7 +73,12 @@ module Gloo
|
|
|
73
73
|
'a namespace shared across several files); with no object ' \
|
|
74
74
|
'at all, saves every open file. An object not yet mapped ' \
|
|
75
75
|
'to a file is saved fresh, either to a given path or to a ' \
|
|
76
|
-
"default path built from the object's own name."
|
|
76
|
+
"default path built from the object's own name. With a " \
|
|
77
|
+
'path, this extracts: the object (and its descendants) ' \
|
|
78
|
+
"move out of whichever file currently owns them -- they're " \
|
|
79
|
+
'no longer declared there too -- into the new file, under ' \
|
|
80
|
+
'their full dotted path (so any parent containers are ' \
|
|
81
|
+
'recreated there via nested-container shorthand).',
|
|
77
82
|
:syntax => [
|
|
78
83
|
'save',
|
|
79
84
|
'save {path.to.object}',
|
|
@@ -88,21 +93,36 @@ module Gloo
|
|
|
88
93
|
:result => 'The file(s) are updated with the latest object ' \
|
|
89
94
|
'state. This is a rewrite, not a regeneration: comments, ' \
|
|
90
95
|
'blank lines, and the original formatting are preserved, ' \
|
|
91
|
-
'and only values that actually changed are re-written.'
|
|
96
|
+
'and only values that actually changed are re-written. ' \
|
|
97
|
+
'With a path, the object also stops being declared in its ' \
|
|
98
|
+
'old file (if it had one) -- both files are rewritten ' \
|
|
99
|
+
'together.',
|
|
92
100
|
:errors => [
|
|
93
101
|
"#{MISSING_PATH_ERR} — 'to' was given with nothing after it.",
|
|
94
102
|
'Could not resolve object to save — the object was not found.',
|
|
95
103
|
'Will not overwrite a file not already saved there — the ' \
|
|
96
|
-
'target path exists but is not mapped to this object.'
|
|
104
|
+
'target path exists but is not mapped to this object.',
|
|
105
|
+
"Can't extract: this object's subtree has declarations in " \
|
|
106
|
+
'more than one file — save each contributing file on its ' \
|
|
107
|
+
'own first, then extract.',
|
|
108
|
+
"Can't extract: this object has no declaration of its own " \
|
|
109
|
+
'to move — it may be an auto-created intermediate ' \
|
|
110
|
+
'container from nested-container shorthand; extract a ' \
|
|
111
|
+
'child that does, or a shallower ancestor that does.'
|
|
97
112
|
],
|
|
98
113
|
:notes => 'An object can also be told to save itself: ' \
|
|
99
114
|
'`tell my_obj to save`. When several files contribute to ' \
|
|
100
115
|
'one container (the namespace pattern), each file save only ' \
|
|
101
|
-
"rewrites that file's own declarations."
|
|
116
|
+
"rewrites that file's own declarations. Extraction (save " \
|
|
117
|
+
'... to {path}) only moves a subtree owned by exactly one ' \
|
|
118
|
+
'file -- one already spread across several files (that ' \
|
|
119
|
+
'same namespace pattern, applied inside the subtree being ' \
|
|
120
|
+
'extracted) is refused rather than guessed at.',
|
|
102
121
|
:examples => <<~EXAMPLES.strip
|
|
103
122
|
> save
|
|
104
123
|
> save my_obj
|
|
105
124
|
> save my_obj to sub/my_obj
|
|
125
|
+
> save app.core.settings to config/settings
|
|
106
126
|
EXAMPLES
|
|
107
127
|
}
|
|
108
128
|
end
|
data/lib/gloo/verbs/unless.rb
CHANGED
|
@@ -11,7 +11,11 @@ module Gloo
|
|
|
11
11
|
KEYWORD = 'unless'.freeze
|
|
12
12
|
KEYWORD_SHORT = 'if!'.freeze
|
|
13
13
|
DO = 'do'.freeze
|
|
14
|
+
THEN = 'then'.freeze
|
|
14
15
|
ELSE = 'else'.freeze
|
|
16
|
+
# 'do' and 'then' are interchangeable -- whichever appears first
|
|
17
|
+
# in the tokens is the actual separator (see Tokens#index_of).
|
|
18
|
+
SEPARATORS = [ DO, THEN ].freeze
|
|
15
19
|
MISSING_EXPR_ERR = 'Missing Expression!'.freeze
|
|
16
20
|
|
|
17
21
|
#
|
|
@@ -21,7 +25,7 @@ module Gloo
|
|
|
21
25
|
value = value_tokens
|
|
22
26
|
return if value.nil?
|
|
23
27
|
|
|
24
|
-
@do = @tokens.expr_after(
|
|
28
|
+
@do = @tokens.expr_after( SEPARATORS, ELSE )
|
|
25
29
|
@else = @tokens.expr_after( ELSE )
|
|
26
30
|
|
|
27
31
|
if evals_false( value )
|
|
@@ -56,7 +60,7 @@ module Gloo
|
|
|
56
60
|
# of the unless command.
|
|
57
61
|
#
|
|
58
62
|
def value_tokens
|
|
59
|
-
value = @tokens.before_token(
|
|
63
|
+
value = @tokens.before_token( SEPARATORS )
|
|
60
64
|
if value && value.count > 1
|
|
61
65
|
# The first token is the verb, so we drop it.
|
|
62
66
|
value = value[ 1..-1 ]
|
|
@@ -114,9 +118,11 @@ module Gloo
|
|
|
114
118
|
:name => KEYWORD,
|
|
115
119
|
:shortcut => KEYWORD_SHORT,
|
|
116
120
|
:description => "Unless an expression is true, do something. " \
|
|
117
|
-
"This is the opposite of the if verb."
|
|
121
|
+
"This is the opposite of the if verb. 'do' and 'then' are " \
|
|
122
|
+
'interchangeable separators -- pick whichever reads better.',
|
|
118
123
|
:syntax => [
|
|
119
124
|
'unless {true} do {command}',
|
|
125
|
+
'unless {true} then {command}',
|
|
120
126
|
'unless {true} do {command} else {else command}'
|
|
121
127
|
],
|
|
122
128
|
:parameters => [
|
|
@@ -126,7 +132,7 @@ module Gloo
|
|
|
126
132
|
],
|
|
127
133
|
:result => 'Unchanged if the expression is true. If not true, ' \
|
|
128
134
|
'then the result will be based on the command specified ' \
|
|
129
|
-
|
|
135
|
+
"after the 'do'/'then' keyword.",
|
|
130
136
|
:errors => [
|
|
131
137
|
"#{MISSING_EXPR_ERR} — No expression is provided as parameter to the verb.",
|
|
132
138
|
'Other errors depend on the command that is run.'
|
|
@@ -148,6 +154,9 @@ module Gloo
|
|
|
148
154
|
put false into unless.x
|
|
149
155
|
unless unless.x do show "second time: " + unless.false_msg
|
|
150
156
|
unless ^.x do show 'F' else show 'T'
|
|
157
|
+
|
|
158
|
+
# 'then' works the same as 'do'
|
|
159
|
+
unless unless.x then show "still false: " + unless.false_msg
|
|
151
160
|
EXAMPLES
|
|
152
161
|
}
|
|
153
162
|
end
|
|
@@ -79,6 +79,23 @@ tests.objs.string [can] :
|
|
|
79
79
|
eval it = 'three'
|
|
80
80
|
assert "expected split (8 100) to clamp to 'three'"
|
|
81
81
|
|
|
82
|
+
word_wrap [test] :
|
|
83
|
+
description [string] : Word-wrap a string to an explicit width
|
|
84
|
+
on_test [script] :
|
|
85
|
+
put 'one two three four five' into ^^.s
|
|
86
|
+
tell ^^.s to word_wrap (10)
|
|
87
|
+
tell ^^.s to count_lines
|
|
88
|
+
eval it = 3
|
|
89
|
+
assert "expected word_wrap (10) to produce 3 lines"
|
|
90
|
+
|
|
91
|
+
word_wrap_keeps_long_word_intact [test] :
|
|
92
|
+
description [string] : word_wrap never breaks a word, even one wider than the width
|
|
93
|
+
on_test [script] :
|
|
94
|
+
put 'http://example.com/a-really-long-url short' into ^^.s
|
|
95
|
+
tell ^^.s to word_wrap (10)
|
|
96
|
+
check ^^.s for substring? ('http://example.com/a-really-long-url')
|
|
97
|
+
assert "expected the long word to remain intact on its own line"
|
|
98
|
+
|
|
82
99
|
split_list [test] :
|
|
83
100
|
description [string] : Split a string into children of a target container
|
|
84
101
|
on_test [script] :
|
|
@@ -101,3 +101,12 @@ tests.objs.text [can] :
|
|
|
101
101
|
put 'hello world' into ^^.b
|
|
102
102
|
tell ^^.b to substring? ('lo w')
|
|
103
103
|
assert "expected it to be true that it contains 'lo w'"
|
|
104
|
+
|
|
105
|
+
word_wrap [test] :
|
|
106
|
+
description [string] : Word-wrap text to an explicit width
|
|
107
|
+
on_test [script] :
|
|
108
|
+
put 'one two three four five' into ^^.b
|
|
109
|
+
tell ^^.b to word_wrap (10)
|
|
110
|
+
tell ^^.b to count_lines
|
|
111
|
+
eval it = 3
|
|
112
|
+
assert "expected word_wrap (10) to produce 3 lines"
|
|
@@ -12,3 +12,24 @@ tests.verbs.if [can] :
|
|
|
12
12
|
eval false
|
|
13
13
|
if ^^.b then eval true
|
|
14
14
|
assert "expected b to be true"
|
|
15
|
+
|
|
16
|
+
if_do [test] :
|
|
17
|
+
description [string] : 'do' is a synonym for 'then'
|
|
18
|
+
on_test [script] :
|
|
19
|
+
eval false
|
|
20
|
+
if ^^.b do eval true
|
|
21
|
+
assert "expected 'do' to work the same as 'then'"
|
|
22
|
+
|
|
23
|
+
if_then_else [test] :
|
|
24
|
+
description [string] : else runs when the condition is false
|
|
25
|
+
on_test [script] :
|
|
26
|
+
eval false
|
|
27
|
+
if false then eval false else eval true
|
|
28
|
+
assert "expected else to run when the condition is false"
|
|
29
|
+
|
|
30
|
+
if_do_else [test] :
|
|
31
|
+
description [string] : else works the same with the 'do' synonym
|
|
32
|
+
on_test [script] :
|
|
33
|
+
eval false
|
|
34
|
+
if false do eval false else eval true
|
|
35
|
+
assert "expected else to run after a 'do' action"
|