gvars 0.9.10 → 0.10.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6230a213eea87d923eaf4e8196bb168d3d52342023fda70adf189b336a4fea36
4
- data.tar.gz: ba9b1449a5fe00221ebb389e903bf19d10d29f264351d60e7f6369318190582b
3
+ metadata.gz: 6cec95dcb820444abd65614b00b8d58228f257aa801c57fd258d205cdfa3cca6
4
+ data.tar.gz: '0343590213ebc76ede529e8e469bebd69439c62826ef134852d37870da417f16'
5
5
  SHA512:
6
- metadata.gz: d0fe558f8ee687fb1c02429c52a1ccc0745c1f33c04f521dbdce03b987ab44910c274726a48f2283b01f13973489e3d71c9ceacaa93ff1f9aa3d570403bd75fd
7
- data.tar.gz: e39330d0bdb1800bf58d45877da5af404f1f599e4f7f77ad8f5f02ac715da2ec97f7cce83c7b13da5cb9d70d17ccab047fb395bea894a05b200944f77bbb0d2f
6
+ metadata.gz: 0db69cfd69b689e2d93f5b62780c197b28929933b43d77241e89bd1658f52d676e637d1f1943a27723e8a9ba5179b3ba0bb8c9c229b768ff82e92c9431dcd560
7
+ data.tar.gz: 1ecd051d8c68d123131c2ef8d96917c70cb17fe6f08063c9525ba0afdca29c6dd053eb0dd3aad2a35af151c919d61d27e7fc7ba54eab7dc58660ad3b441a7790
data/README.md CHANGED
@@ -31,8 +31,8 @@ puts $TIME.monday? #=> true
31
31
 
32
32
  # You can also provide a custom setter
33
33
  GVars.virtual(:$PWD,
34
- getter: ->{ Dir.pwd },
35
- setter: ->new{ Dir.chdir(new) })
34
+ getter: ->{ Dir.pwd },
35
+ setter: ->new{ Dir.chdir(new) })
36
36
  $PWD = '/tmp'
37
37
  p $PWD #=> "/private/tmp" (macOS symlinks `/tmp` to `/private/tmp`)
38
38
  ```
@@ -51,9 +51,33 @@ p $COUNTER #=> 11
51
51
  # You can also specify a state, which is given to the getter (and setter),
52
52
  # but isn't updated by the getter's return value.
53
53
  GVars.hooked(:$RAND,
54
- state: Random.new,
55
- getter: ->random{ random.rand },
56
- setter: ->(value,random){ random.srand(value) }) # doesn't actually work.. `Random#srand` isn't a thing
54
+ state: Random.new,
55
+ getter: ->random{ random.rand },
56
+ setter: ->(value,random){ random.srand(value) }) # doesn't actually work.. `Random#srand` isn't a thing
57
+ ```
58
+
59
+ ## `OptParse` support
60
+ If you've ever wanted to use `OptParse` to parse command line options _directly_ into global variables, look no further! `GVars.[]` and `GVars.[]=` act similarly to `GVars.get` and `GVars.set`, except they allow you to omit the leading `$`, and all `-` are translated to `_`:
61
+
62
+ ```ruby
63
+ require 'optparse'
64
+ require 'gvars'
65
+
66
+ OptParse.new do |op|
67
+ op.on '--[no-]read'
68
+ op.on '--[no-]write'
69
+ op.on '--[no-]execute'
70
+ op.on '--timeout=SECONDS', Float
71
+ op.on '--cache-dir=PATH'
72
+
73
+ op.parse!(
74
+ %w[--read --no-execute --timeout=10 --cache-dir=/foo/bar],
75
+ into: GVars
76
+ )
77
+
78
+ p [$read, $write, $execute, $timeout, $cache_dir]
79
+ #=> [true, nil, false, 10.0, "/foo/bar"]
80
+ end
57
81
  ```
58
82
 
59
83
  ## Known problems
data/ext/gvars/gvars.c CHANGED
@@ -2,51 +2,82 @@
2
2
  #define dbg(...) (fprintf(stderr, "%s:%s:%d: ", __FILE__, __FUNCTION__, __LINE__), fprintf(stderr,__VA_ARGS__),fprintf(stderr,"\n"),fflush(stderr))
3
3
 
4
4
  VALUE gvars_module;
5
+ static ID id_debug;
6
+
7
+ static bool
8
+ gvars_debug_enabled(void)
9
+ {
10
+ VALUE debug_mode = rb_ivar_get(gvars_module, id_debug);
11
+ return debug_mode == Qnil ? ruby_debug : RTEST(debug_mode);
12
+ }
13
+
14
+ static VALUE
15
+ gvars_f_defined_p(VALUE self)
16
+ {
17
+ return gvars_debug_enabled() ? Qtrue : Qfalse;
18
+ }
5
19
 
6
20
  // Converts `name` to a global variable name, ensures it's valid, and returns it.
7
21
  //
8
22
  // for checking, it makes that `name` starts with `$`. This isn't really required, as ruby supports
9
23
  // globals that don't start with `$` (but doesn't expose any methods to interact with them)
10
- static char *get_global_name(VALUE *name, bool raise) {
24
+ static char *get_global_name(VALUE *name, bool translate) {
11
25
  // If `name` is a symbol, get its backing string
12
26
  if (RB_SYMBOL_P(*name)) {
13
27
  *name = rb_sym2str(*name);
14
28
  }
15
29
 
30
+ if (translate) {
31
+ // Convert from `-` to `_`
32
+ *name = rb_funcall(StringValue(*name), rb_intern("tr"), 2,
33
+ rb_str_new_cstr("-"), rb_str_new_cstr("_"));
34
+ }
35
+
16
36
  char *namestr = StringValueCStr(*name);
17
37
 
18
38
  // Ensure it starts with a dollar sign
19
- if (raise && namestr[0] != '$') {
39
+ if (!translate && namestr[0] != '$') {
20
40
  rb_raise(rb_eNameError, "'%s' is not allowed as a global variable name", namestr);
21
41
  }
22
42
 
23
43
  return namestr;
24
44
  }
25
45
 
26
-
27
46
  static VALUE
28
47
  gvars_f_global_variable_get(VALUE self, VALUE name)
29
48
  {
30
- return rb_gv_get(get_global_name(&name, true));
49
+ return rb_gv_get(get_global_name(&name, false));
31
50
  }
32
51
 
33
52
  static VALUE
34
53
  gvars_f_global_variable_aref(VALUE self, VALUE name)
35
54
  {
36
- return rb_gv_get(get_global_name(&name, false));
55
+ return rb_gv_get(get_global_name(&name, true));
37
56
  }
38
57
 
39
58
  static VALUE
40
59
  gvars_f_global_variable_set(VALUE self, VALUE name, VALUE value)
41
60
  {
42
- return rb_gv_set(get_global_name(&name, true), value);
61
+ return rb_gv_set(get_global_name(&name, false), value);
43
62
  }
44
63
 
45
-
46
64
  static VALUE
47
65
  gvars_f_global_variable_aset(VALUE self, VALUE name, VALUE value)
48
66
  {
49
- return rb_gv_set(get_global_name(&name, false), value);
67
+ char *sname = get_global_name(&name, true);
68
+ if (gvars_debug_enabled()) {
69
+ char *sname_without_dollar = sname;
70
+ if (sname_without_dollar[0] == '$') {
71
+ ++sname_without_dollar;
72
+ }
73
+
74
+ VALUE msg = rb_sprintf("GVars: setting global $%s to: %"PRIsVALUE"\n",
75
+ sname_without_dollar, rb_inspect(value));
76
+
77
+ rb_io_write(rb_gv_get("$stderr"), msg);
78
+ }
79
+
80
+ return rb_gv_set(sname, value);
50
81
  }
51
82
 
52
83
  static VALUE
@@ -268,6 +299,7 @@ static VALUE convert_to_proc(const char *name, VALUE input, bool is_hooked) {
268
299
 
269
300
  VALUE proc = rb_convert_type(input, T_DATA, "Proc", "to_proc");
270
301
 
302
+ // Check_Type
271
303
  if (NIL_P(proc) || !rb_obj_is_proc(proc)) {
272
304
  rb_raise(rb_eTypeError, "wrong %s type %s (expected Proc)", name, rb_obj_classname(proc));
273
305
  }
@@ -351,6 +383,7 @@ void
351
383
  Init_gvars(void)
352
384
  {
353
385
  gvars_module = rb_define_module("GVars");
386
+ VALUE gvars_singleton = rb_singleton_class(gvars_module);
354
387
  rb_ivar_set(gvars_module, rb_intern("vars"), rb_hash_new());
355
388
 
356
389
  virtual_method_keyword_ids[VIRTUAL_METHOD_KEYWORDS_GETTER] = rb_intern("getter");
@@ -359,6 +392,12 @@ Init_gvars(void)
359
392
  virtual_method_keyword_ids[VIRTUAL_METHOD_KEYWORDS_STATE] = rb_intern("state");
360
393
  virtual_method_keyword_ids[VIRTUAL_METHOD_KEYWORDS_READONLY] = rb_intern("readonly");
361
394
 
395
+
396
+ // Define debugging construct
397
+ id_debug = rb_intern_const("@debug");
398
+ rb_define_attr(gvars_singleton, "debug", 1, 1);
399
+ rb_define_singleton_method(gvars_module, "debug?", gvars_f_defined_p, 0);
400
+
362
401
  // Define module-level functions that can be used as mixins
363
402
  rb_define_module_function(gvars_module, "global_variable_get", gvars_f_global_variable_get, 1);
364
403
  rb_define_module_function(gvars_module, "global_variable_set", gvars_f_global_variable_set, 2);
@@ -380,7 +419,6 @@ Init_gvars(void)
380
419
  rb_define_singleton_method(gvars_module, "[]=", gvars_f_global_variable_aset, 2);
381
420
 
382
421
  // Aliases
383
- VALUE gvars_singleton = rb_singleton_class(gvars_module);
384
422
  rb_define_alias(gvars_singleton, "get", "global_variable_get");
385
423
  rb_define_alias(gvars_singleton, "set", "global_variable_set");
386
424
  rb_define_alias(gvars_singleton, "alias", "alias_global_variable");
data/lib/gvars/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GVars
4
- VERSION = '0.9.10'
4
+ VERSION = '0.10.1'
5
5
  end
data/sig/gvars.rbs CHANGED
@@ -1,6 +1,9 @@
1
1
  module GVars
2
2
  VERSION: String
3
3
 
4
+ attr_accessor self.debug: bool?
5
+ def self.debug?: () -> bool
6
+
4
7
  # Mixin methods
5
8
  def self?.global_variable_get: (interned name) -> untyped
6
9
  def self?.global_variable_set: [T] (interned name, T value) -> T
data/tmp.rb CHANGED
@@ -1,3 +1,22 @@
1
+ require 'optparse'
2
+ require 'gvars'
3
+
4
+ OptParse.new do |op|
5
+ op.on '--[no-]read'
6
+ op.on '--[no-]write'
7
+ op.on '--[no-]execute'
8
+ op.on '--timeout=SECONDS', Float
9
+ op.on '--cache-dir=PATH'
10
+
11
+ op.parse!(
12
+ %w[--read --no-execute --timeout=10 --cache-dir=/foo/bar],
13
+ into: GVars
14
+ )
15
+
16
+ p [$read, $write, $execute, $timeout, $cache_dir]
17
+ end
18
+
19
+ __END__
1
20
  require 'gvars'
2
21
  p GVars::VERSION
3
22
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gvars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.10
4
+ version: 0.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Westerman