Class: ParametersExtra::Args::Arg

Inherits:
Object
  • Object
show all
Defined in:
lib/parameters_extra/args.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, default = nil) ⇒ Arg

Returns a new instance of Arg.



8
9
10
# File 'lib/parameters_extra/args.rb', line 8

def initialize(name, type, default = nil)
  @name, @type, @default = name, type, default
end

Instance Attribute Details

#arg_listObject

Returns the value of attribute arg_list.



5
6
7
# File 'lib/parameters_extra/args.rb', line 5

def arg_list
  @arg_list
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/parameters_extra/args.rb', line 6

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/parameters_extra/args.rb', line 6

def type
  @type
end

Instance Method Details

#default?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/parameters_extra/args.rb', line 24

def default?
  !@default.nil?
end

#default_value(*args) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/parameters_extra/args.rb', line 28

def default_value(*args)
  return nil if @default.nil?
  receiver ||= arg_list.owning_method.receiver
  raise "You must specify a receiver for the defaul value" if receiver.nil?
  raise "You must evaluate defaults in the context of a matching class. #{receiver.class.name} is not a #{arg_list.cls.name}." unless receiver.is_a?(arg_list.cls)
  receiver.instance_eval(@default)
end

#optional?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/parameters_extra/args.rb', line 16

def optional?
  @type == :optional
end

#required?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/parameters_extra/args.rb', line 12

def required?
  @type == :required
end

#splat?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/parameters_extra/args.rb', line 20

def splat?
  @type == :splat
end