Class: Magiq::Param

Inherits:
Object
  • Object
show all
Defined in:
lib/magiq/param.rb

Constant Summary collapse

OPTS =
[
  TYPE  = :type,
  SOLO  = :solo,
  LIMIT = :limit,
  ALIAS = :alias,
  ARRAY = :array
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, opts = {}) ⇒ Param

Returns a new instance of Param.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/magiq/param.rb', line 15

def initialize(key, opts = {})
  @key     = key.to_sym
  @type    = Types.lookup(opts[TYPE] || :string)
  @solo    = opts[SOLO]  ? true : false
  @limit   = opts[LIMIT] || Magiq[:array_param_limit]
  @aliases = opts[ALIAS] ? Array(opts[:alias]) : []
  @keys    = [@key].concat(@aliases).map(&:to_sym)

  @array = case opts[ARRAY]
  when :always
    :always
  when :allow
    :allow
  when nil, false
    false
  else
    raise ArgumentError, ":array option must be :always, :allow, or false, " \
    "not: #{opts[ARRAY].inspect}"
  end
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



5
6
7
# File 'lib/magiq/param.rb', line 5

def aliases
  @aliases
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/magiq/param.rb', line 5

def key
  @key
end

#keysObject (readonly)

Returns the value of attribute keys.



5
6
7
# File 'lib/magiq/param.rb', line 5

def keys
  @keys
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/magiq/param.rb', line 5

def type
  @type
end

Instance Method Details

#accepts_array?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/magiq/param.rb', line 41

def accepts_array?
  @array ? true : false
end

#clean(raw_value) ⇒ Object



36
37
38
39
# File 'lib/magiq/param.rb', line 36

def clean(raw_value)
  v = raw_value.to_s.strip
  v == '' ? nil : v
end

#extract(raw_value) ⇒ Object



49
50
51
52
53
54
55
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
# File 'lib/magiq/param.rb', line 49

def extract(raw_value)
  return unless raw_value

  if raw_value.is_a?(Array) && !accepts_array?
    raise BadParamError, "An array of values was passed to the `#{key}` " \
    "parameter but it is not permitted to accept more than one value."
  end

  value = case @array
  when :always
    raw_value.is_a?(Array) ? raw_value : raw_value.split(',')
  when :allow
    if raw_value.is_a?(Array)
      raw_value
    elsif raw_value.include?(',')
      raw_value.split(',')
    else
      raw_value
    end
  else
    raw_value
  end

  if value.is_a?(Array) && @limit && value.size > @limit
    raise BadParamError, "The number of items passed to the `#{key}` " \
    "parameter is #{value.size} which exceeds the permitted maxium of " \
    "#{@limit} items."
  end

  if value.is_a?(Array)
    return value.map { |v| @type.cast(clean(v)) }
  end

  return unless (v = clean(value))

  @type.cast(v)
end

#solo?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/magiq/param.rb', line 45

def solo?
  @solo
end