Class: Saper::Argument
- Inherits:
-
Object
- Object
- Saper::Argument
- Defined in:
- lib/saper/core/argument.rb
Direct Known Subclasses
Saper::Arguments::Attribute, Saper::Arguments::Recipe, Saper::Arguments::Text, Saper::Arguments::Timezone, Saper::Arguments::Variable, Saper::Arguments::XPath
Class Method Summary collapse
-
.[](type) ⇒ Saper::Argument
Returns a subclass with specified type.
-
.exists?(type) ⇒ Boolean
Returns ‘true` if there is a subclass with specified type.
-
.inherited(base) ⇒ Class
Tracks subclasses of Saper::Argument.
-
.new(*args, &block) ⇒ Saper::Argument
Returns a new instance of Saper::Argument.
-
.subclasses ⇒ Hash
Returns a hash of subclasses.
-
.type ⇒ String
Returns class name as an underscored string.
Instance Method Summary collapse
- #action ⇒ Object
-
#initialize(opts = {}) ⇒ Argument
constructor
A new instance of Argument.
- #mandatory? ⇒ Boolean
- #normalize(value) ⇒ Object
- #optional? ⇒ Boolean
- #serialize ⇒ Object
- #set(value) ⇒ Object
- #to_string ⇒ Object
- #valid?(value) ⇒ Boolean
- #value ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Argument
TODO:
Returns a new instance of Argument.
47 48 49 50 51 52 53 |
# File 'lib/saper/core/argument.rb', line 47 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(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 |
.subclasses ⇒ Hash
Returns a hash of subclasses.
12 13 14 |
# File 'lib/saper/core/argument.rb', line 12 def self.subclasses @subclasses ||= {} end |
.type ⇒ String
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
#action ⇒ Object
TODO:
91 92 93 |
# File 'lib/saper/core/argument.rb', line 91 def action @opts[:action] end |
#mandatory? ⇒ Boolean
TODO:
86 87 88 |
# File 'lib/saper/core/argument.rb', line 86 def mandatory? not optional? end |
#normalize(value) ⇒ Object
TODO:
71 72 73 |
# File 'lib/saper/core/argument.rb', line 71 def normalize(value) value end |
#optional? ⇒ Boolean
TODO:
96 97 98 |
# File 'lib/saper/core/argument.rb', line 96 def optional? @opts[:optional] == true end |
#serialize ⇒ Object
TODO:
81 82 83 |
# File 'lib/saper/core/argument.rb', line 81 def serialize value end |
#set(value) ⇒ Object
TODO:
56 57 58 59 60 61 62 63 |
# File 'lib/saper/core/argument.rb', line 56 def set(value) unless valid?(value) raise InvalidArgument, value else @value = normalize(value) end self end |
#to_string ⇒ Object
TODO:
101 102 103 |
# File 'lib/saper/core/argument.rb', line 101 def to_string value.to_s.inspect end |
#valid?(value) ⇒ Boolean
TODO:
66 67 68 |
# File 'lib/saper/core/argument.rb', line 66 def valid?(value) true end |
#value ⇒ Object
TODO:
76 77 78 |
# File 'lib/saper/core/argument.rb', line 76 def value @value end |