Module: Moon::ArgsHelper

Defined in:
lib/moon/packages/std/args_helper.rb

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.get_args(array, format, *args) {|result| ... } ⇒ Object

format specifiers - Based on mruby mrb_get_args

*:      rest                       -> (ary)
A:      Array                      -> (ary)
a:      Array (expanded)           -> (*ary)
b:      Boolean                    -> (bool)
C:      Class                      -> (klass)
f:      Float                      -> (flt)
H:      Hash                       -> (hsh)
i:      Integer                    -> (int)
n:      Symbol                     -> (sym)
o:      Object                     -> (obj)
O:      Object (of type) [Class]   -> (obj)
S:      String                     -> (str)
|:      optional

Parameters:

  • array (Array)

    source array

  • format (String)

    args format

  • args (Object)

    helper args

Yields:

  • (result)

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/moon/packages/std/args_helper.rb', line 56

def self.get_args(array, format, *args)
  result = []
  arg_index = 0
  ary_index = 0
  optional = false
  valid = true

  next_arg = lambda do
    value = args[arg_index]
    arg_index += 1
    value
  end

  next_ary = lambda do
    value = array[ary_index]
    ary_index += 1
    value
  end

  add_arg_as = lambda do |klass|
    if test_arg_push(result, next_ary.(), klass) || optional
      true
    else
      valid = false
      false
    end
  end

  concat_arg_as = lambda do |klass|
    if test_arg_concat(result, next_ary.(), klass) || optional
      true
    else
      valid = false
      false
    end
  end

  format.split('').each_with_index do |char, i|
    case char
    when '*'
      result << array[ary_index, array.size - ary_index]
    when 'A'
      break unless add_arg_as.(Array)
    when 'a'
      break unless concat_arg_as.(Array)
    when 'b'
      break unless add_arg_as.(Boolean)
    when 'C'
      break unless add_arg_as.(Class)
    when 'f'
      break unless add_arg_as.(Float)
    when 'H'
      break unless add_arg_as.(Hash)
    when 'i'
      break unless add_arg_as.(Integer)
    when 'n'
      break unless add_arg_as.(Symbol)
    when 'o'
      break unless add_arg_as.(Object)
    when 'O'
      break unless add_arg_as.(next_arg.())
    when 'S'
      break unless add_arg_as.(String)
    when '|'
      optional = true
    end
  end

  yield(*result) if valid && block_given?

  Result.new(valid, result)
end

.test_arg?(arg, expected_klass) ⇒ Boolean

Returns:


17
18
19
# File 'lib/moon/packages/std/args_helper.rb', line 17

def self.test_arg?(arg, expected_klass)
  arg.is_a?(expected_klass)
end

.test_arg_concat(target, arg, expected_klass) ⇒ Object


30
31
32
33
34
35
36
37
# File 'lib/moon/packages/std/args_helper.rb', line 30

def self.test_arg_concat(target, arg, expected_klass)
  if test_arg?(arg, expected_klass)
    target.concat(arg)
    true
  else
    false
  end
end

.test_arg_push(target, arg, expected_klass) ⇒ Object


21
22
23
24
25
26
27
28
# File 'lib/moon/packages/std/args_helper.rb', line 21

def self.test_arg_push(target, arg, expected_klass)
  if test_arg?(arg, expected_klass)
    target.push(arg)
    true
  else
    false
  end
end