gvars 0.9.10 → 0.9.11

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: dd8a8d29639c250dbe4ef1354ca98ce1e94510753c111ba62965af0f8bca77cb
4
+ data.tar.gz: f1d88f86c4fba91ad7e842a17c00b1ec8ee1038bb7780ff6a4368eadcc133443
5
5
  SHA512:
6
- metadata.gz: d0fe558f8ee687fb1c02429c52a1ccc0745c1f33c04f521dbdce03b987ab44910c274726a48f2283b01f13973489e3d71c9ceacaa93ff1f9aa3d570403bd75fd
7
- data.tar.gz: e39330d0bdb1800bf58d45877da5af404f1f599e4f7f77ad8f5f02ac715da2ec97f7cce83c7b13da5cb9d70d17ccab047fb395bea894a05b200944f77bbb0d2f
6
+ metadata.gz: ccea699c50675a7f3a6d65d52b684d6acb77ff99177c019111f48fdf724cd4c3753bdb70e65b02437ddf259187104472f9bbb149370d2881a7c9bbb6912b2f72
7
+ data.tar.gz: 8e2344c225d5f304391bfdadf51f9f416d41a1c0bcdd83755fcc11d9b20b1ed3851e4c822aee286de9b4022594e06666e45a93e324ec608e8be96908ef2c4cc0
data/README.md CHANGED
@@ -56,6 +56,30 @@ GVars.hooked(:$RAND,
56
56
  setter: ->(value,random){ random.srand(value) }) # doesn't actually work.. `Random#srand` isn't a thing
57
57
  ```
58
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
81
+ ```
82
+
59
83
  ## Known problems
60
84
  1. Unfortunately, Ruby's C-level `rb_gv_get` / `rb_gv_set` methods only let you manipulate ASCII identifiers... A fix _may_ be possible, but it'll have to resort to the `eval` hack.
61
85
  2. Because `$_`, and regex variables (`$~`, ``$` ``, etc) are all local to the function they're called within, you can't actually access them from within procs; As a workaround, you can pass a string that'll be `eval`d each time, but it's not terribly efficient. I don't know if there's a better solution
data/ext/gvars/gvars.c CHANGED
@@ -7,16 +7,22 @@ VALUE gvars_module;
7
7
  //
8
8
  // for checking, it makes that `name` starts with `$`. This isn't really required, as ruby supports
9
9
  // 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) {
10
+ static char *get_global_name(VALUE *name, bool translate) {
11
11
  // If `name` is a symbol, get its backing string
12
12
  if (RB_SYMBOL_P(*name)) {
13
13
  *name = rb_sym2str(*name);
14
14
  }
15
15
 
16
+ if (translate) {
17
+ // Convert from `-` to `_`
18
+ *name = rb_funcall(StringValue(*name), rb_intern("tr"), 2,
19
+ rb_str_new_cstr("-"), rb_str_new_cstr("_"));
20
+ }
21
+
16
22
  char *namestr = StringValueCStr(*name);
17
23
 
18
24
  // Ensure it starts with a dollar sign
19
- if (raise && namestr[0] != '$') {
25
+ if (!translate && namestr[0] != '$') {
20
26
  rb_raise(rb_eNameError, "'%s' is not allowed as a global variable name", namestr);
21
27
  }
22
28
 
@@ -24,29 +30,30 @@ static char *get_global_name(VALUE *name, bool raise) {
24
30
  }
25
31
 
26
32
 
33
+
27
34
  static VALUE
28
35
  gvars_f_global_variable_get(VALUE self, VALUE name)
29
36
  {
30
- return rb_gv_get(get_global_name(&name, true));
37
+ return rb_gv_get(get_global_name(&name, false));
31
38
  }
32
39
 
33
40
  static VALUE
34
41
  gvars_f_global_variable_aref(VALUE self, VALUE name)
35
42
  {
36
- return rb_gv_get(get_global_name(&name, false));
43
+ return rb_gv_get(get_global_name(&name, true));
37
44
  }
38
45
 
39
46
  static VALUE
40
47
  gvars_f_global_variable_set(VALUE self, VALUE name, VALUE value)
41
48
  {
42
- return rb_gv_set(get_global_name(&name, true), value);
49
+ return rb_gv_set(get_global_name(&name, false), value);
43
50
  }
44
51
 
45
52
 
46
53
  static VALUE
47
54
  gvars_f_global_variable_aset(VALUE self, VALUE name, VALUE value)
48
55
  {
49
- return rb_gv_set(get_global_name(&name, false), value);
56
+ return rb_gv_set(get_global_name(&name, true), value);
50
57
  }
51
58
 
52
59
  static VALUE
@@ -268,6 +275,7 @@ static VALUE convert_to_proc(const char *name, VALUE input, bool is_hooked) {
268
275
 
269
276
  VALUE proc = rb_convert_type(input, T_DATA, "Proc", "to_proc");
270
277
 
278
+ // Check_Type
271
279
  if (NIL_P(proc) || !rb_obj_is_proc(proc)) {
272
280
  rb_raise(rb_eTypeError, "wrong %s type %s (expected Proc)", name, rb_obj_classname(proc));
273
281
  }
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.9.11'
5
5
  end
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.9.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Westerman