Class: Cell
- Inherits:
-
Object
- Object
- Cell
- Defined in:
- lib/args.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#repeatable ⇒ Object
Returns the value of attribute repeatable.
-
#required ⇒ Object
Returns the value of attribute required.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #command ⇒ Object
- #desc ⇒ Object
- #full? ⇒ Boolean
-
#initialize(object, hash = { }) ⇒ Cell
constructor
A new instance of Cell.
- #missing_value ⇒ Object
- #name ⇒ Object
-
#set(value, target) ⇒ Object
return nil on success, error message on failure.
Constructor Details
#initialize(object, hash = { }) ⇒ Cell
Returns a new instance of Cell.
6 7 8 9 10 11 12 |
# File 'lib/args.rb', line 6 def initialize(object, hash = { }) @obj = object hash.each {|k,v| instance_variable_set("@#{k}",v)} if @repeatable @value = [ ] end end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
4 5 6 |
# File 'lib/args.rb', line 4 def default @default end |
#repeatable ⇒ Object
Returns the value of attribute repeatable.
4 5 6 |
# File 'lib/args.rb', line 4 def repeatable @repeatable end |
#required ⇒ Object
Returns the value of attribute required.
4 5 6 |
# File 'lib/args.rb', line 4 def required @required end |
#value ⇒ Object
Returns the value of attribute value.
4 5 6 |
# File 'lib/args.rb', line 4 def value @value end |
Instance Method Details
#command ⇒ Object
18 19 20 |
# File 'lib/args.rb', line 18 def command @obj.class <= Command ? @obj : nil end |
#desc ⇒ Object
70 71 72 |
# File 'lib/args.rb', line 70 def desc @obj.desc end |
#full? ⇒ Boolean
14 15 16 |
# File 'lib/args.rb', line 14 def full? @full end |
#missing_value ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/args.rb', line 53 def missing_value if @value.nil? && @default set(@default, nil) end return false unless @required # @required is true if @repeatable && @value == [ ] true else @value.nil? end end |
#name ⇒ Object
66 67 68 |
# File 'lib/args.rb', line 66 def name @obj.name end |
#set(value, target) ⇒ Object
return nil on success, error message on failure
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/args.rb', line 23 def set(value, target) case when @obj.class == Arg error = @obj.validate?(value, target) if error if @repeatable @full = true end return error end if @obj.validator.class == Hash value = @obj.validator[value] end if @repeatable @value << value else @value = value @full = true end when @obj.class <= Command if value == @obj.name @value = @obj @full = true else return "incorrect command name" end end nil end |