Class Subtle::Options < Object

Methods

public class

  1. init

public instance

  1. gravity
  2. match
  3. method_missing
  4. on_match
  5. style

Attributes

params [RW] Params list
styles [RW]

Public class methods

init → Subtle::Options

Create a new Options object

1 options = Subtle::Options.new
2 => #<Subtle::Options:xxx>
[show source]
static VALUE
RubyOptionsInit(VALUE self)
{
  rb_iv_set(self, "@params", rb_hash_new());

  return Qnil;
}

Public instance methods

gravity → nil

Overwrite global gravity method

1 option.gravity :center
2 => nil
[show source]
static VALUE
RubyOptionsGravity(VALUE self,
  VALUE gravity)
{
  VALUE params = rb_iv_get(self, "@params");

  /* Just store param */
  return rb_hash_aset(params, CHAR2SYM("gravity"), gravity);
}
match → nil

Append match hashes if called multiple times

1 option.match :name => /foo/
2 => nil
[show source]
static VALUE
RubyOptionsMatch(VALUE self,
  VALUE value)
{
  VALUE params = Qnil, ary = Qnil, hash = Qnil, sym = Qnil;

  /* Get params and convert value to hash */
  params = rb_iv_get(self, "@params");
  hash   = RubyValueToHash(value);
  sym    = CHAR2SYM("match");

  /* Check if hash contains key and add or otherwise create it */
  if(T_ARRAY != rb_type(ary = rb_hash_lookup(params, sym)))
    {
      ary = rb_ary_new();

      rb_hash_aset(params, sym, ary);
    }

  /* Finally add value to array */
  rb_ary_push(ary, hash);

  return Qnil;
}
method_missing (...)

Dispatcher for DSL proc methods - internal use only

[show source]
static VALUE
RubyOptionsDispatcher(int argc,
  VALUE *argv,
  VALUE self)
{
  VALUE ret = Qnil, missing = Qnil, args[4] = { Qnil };

  rb_scan_args(argc, argv, "23", &missing,
    &args[0], &args[1], &args[2], &args[3]);

  /* Check whether missing is included in methods array
   * and dispatch to config or just store param*/
  if(RTEST(config_methods) && rb_ary_includes(config_methods, missing))
    {
      ret = rb_funcall2(config_instance, rb_to_id(missing), --argc, ++argv);
    }
  else
    {
      VALUE arg = Qnil;

      /* Convert multiple argument to one array */
      if(2 < argc)
        {
          int i;

          /* Move args into array */
          arg = rb_ary_new();

          for(i = 0; i < LENGTH(args); i++)
            if(!NIL_P(args[i])) rb_ary_push(arg, args[i]);
        }
      else arg = args[0];

      ret = rb_hash_aset(rb_iv_get(self, "@params"), missing, arg);
    }

  return ret;
}
on_match(, blk) → nil

Add a tag on match proc

1 tag "test" do
2   match "foobar"
3 
4   on_match do |c|
5     c.gravity = :foobar
6   end
7 end
[show source]
static VALUE
RubyOptionsOnMatch(int argc,
  VALUE *argv,
  VALUE self)
{
  VALUE value = Qnil;

  rb_scan_args(argc, argv, "01", &value);

  if(subtle->flags & SUB_SUBTLE_CHECK) return Qnil; ///< Skip on check

  if(rb_block_given_p()) value = rb_block_proc(); ///< Get proc

  rb_hash_aset(rb_iv_get(self, "@params"),
    CHAR2SYM("on_match"), value);

  return Qnil;
}
style(name) → nil

Overwrite global style method

1 option.style :urgent do
2   foreground "#fecf35"
3 end
4 => nil
[show source]
static VALUE
RubyOptionsStyle(VALUE self,
  VALUE name)
{
  /* Check if block is given */
  if(rb_block_given_p())
    {
      VALUE klass = Qnil, options = Qnil, styles = rb_iv_get(self, "@styles");

      /* Create styles hash if necessary */
      if(NIL_P(styles))
        {
          styles = rb_hash_new();
          rb_iv_set(self, "@styles", styles);
        }

      /* Collect options */
      klass   = rb_const_get(mod, rb_intern("Options"));
      options = rb_funcall(klass, rb_intern("new"), 1, self);
      rb_obj_instance_eval(0, 0, options);

      rb_hash_aset(styles, name, options);
    }
  else
    {
      VALUE params = rb_iv_get(self, "@params");

      /* Just append to params */
      rb_hash_aset(params, CHAR2SYM("style"), name);
    }

  return Qnil;
}