Class: Saper::Argument

Inherits:
Object
  • Object
show all
Defined in:
lib/saper/core/argument.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Saper::Argument

Returns a new instacne of Saper::Argument.


51
52
53
54
55
56
57
# File 'lib/saper/core/argument.rb', line 51

def initialize(opts = {})
  @value = nil
  @opts  = opts
  if mandatory? || !@opts[:value].nil?
    set @opts.delete(:value)
  end
end

Class Method Details

.[](type) ⇒ Saper::Argument

Returns a subclass with specified type.


25
26
27
# File 'lib/saper/core/argument.rb', line 25

def self.[](type)
  subclasses[type.to_s] || raise(Errors::InvalidType, "Invalid argument: %s" % type)
end

.exists?(type) ⇒ Boolean

Returns `true` if there is a subclass with specified type.


32
33
34
# File 'lib/saper/core/argument.rb', line 32

def self.exists?(type)
  subclasses.keys.include?(type.to_s)
end

.inherited(base) ⇒ Class

Tracks subclasses of Saper::Argument.


6
7
8
# File 'lib/saper/core/argument.rb', line 6

def self.inherited(base)
  subclasses[base.type] = base
end

.new(*args, &block) ⇒ Saper::Argument

Returns a new instance of Saper::Argument.


38
39
40
41
42
43
44
# File 'lib/saper/core/argument.rb', line 38

def self.new(*args, &block)
  if self == Argument
    self[args.shift].new(*args, &block)
  else
    super(*args, &block)
  end
end

.subclassesHash

Returns a hash of subclasses.


12
13
14
# File 'lib/saper/core/argument.rb', line 12

def self.subclasses
  @subclasses ||= {}
end

.typeString

Returns class name as an underscored string.


18
19
20
# File 'lib/saper/core/argument.rb', line 18

def self.type
  name.split("::").last.gsub(/([a-z])([A-Z])/,'\1_\2').downcase
end

Instance Method Details

#actionSaper::Action

Returns parent Action instance that this Argument belongs to.


114
115
116
# File 'lib/saper/core/argument.rb', line 114

def action
  @opts[:action]
end

#mandatory?Boolean

Returns true if argument value is mandatory.


102
103
104
# File 'lib/saper/core/argument.rb', line 102

def mandatory?
  not optional?
end

#normalize(value) ⇒ Object

Returns normalized value. Subclasses should overrid this method. Subclasses may override this method.


85
86
87
# File 'lib/saper/core/argument.rb', line 85

def normalize(value)
  value
end

#optional?Boolean

Returns true if argument value is optional.


108
109
110
# File 'lib/saper/core/argument.rb', line 108

def optional?
  @opts[:optional] == true
end

#serializeObject

Returns serialized representation of argument's value. Subclasses may override this method.


96
97
98
# File 'lib/saper/core/argument.rb', line 96

def serialize
  value
end

#set(value) ⇒ self

Sets new value and returns self.


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/saper/core/argument.rb', line 62

def set(value)
  if value.is_a?(Saper::Argument)
    value = value.value
  end
  unless valid?(value)
    raise Errors::InvalidArgument, value
  else
    @value = normalize(value)
  end
  self
end

#to_stringString

Returns String representation of argument's value.


120
121
122
# File 'lib/saper/core/argument.rb', line 120

def to_string
  value.to_s.inspect
end

#valid?(value) ⇒ Boolean

Returns true if supplied value is accepted by this argument type. Subclasses should override this method.


77
78
79
# File 'lib/saper/core/argument.rb', line 77

def valid?(value)
  true
end

#valueObject

Returns argument value. Subclasses may override this method.


91
92
93
# File 'lib/saper/core/argument.rb', line 91

def value
  @value
end