Class: AttributedClass::Attribute
Direct Known Subclasses
Constant Summary collapse
- @@default_proc =
nil
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#default_proc ⇒ Object
readonly
Returns the value of attribute default_proc.
-
#descr ⇒ Object
readonly
Returns the value of attribute descr.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#mandatory ⇒ Object
Returns the value of attribute mandatory.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #compute_default(default_proc_args) ⇒ Object
- #help(output = STDERR) ⇒ Object
-
#initialize(name, descr, *args, &block) ⇒ Attribute
constructor
A new instance of Attribute.
- #inspect ⇒ Object
- #invisible? ⇒ Boolean
- #mandatory? ⇒ Boolean
- #to_form(b) ⇒ Object
- #valid?(anObject) ⇒ Boolean
- #visible? ⇒ Boolean
Constructor Details
#initialize(name, descr, *args, &block) ⇒ Attribute
Returns a new instance of Attribute.
19 20 21 22 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/attributed_class.rb', line 19 def initialize ( name, descr, *args, &block ) raise ArgumentError, 'need a symbol' unless name.is_a? Symbol @name, @descr = name, descr @default, @klass = nil, nil @mandatory = false @visible = true if block_given? @default_proc = block else @default_proc = @@default_proc end other = [] args.each do |arg| case arg when :mandatory then @mandatory = true when :invisible then @visible = false else other << arg end end case other.size when 1 if other[0].is_a? Class @klass = [other[0]] elsif other[0].is_a? Array and other[0].all? { |k| k.is_a? Class } @klass = other[0] else @default = other[0] end when 2 a, b = other if a.is_a? Class and b.is_a? a @klass, @default = [a], b elsif a.is_a? Array and a.all? { |k| k.is_a? Class } and a.any? { |k| b.is_a? k } @klass, @default = a, b elsif b.is_a? Class and a.is_a? b @klass, @default = [b], a elsif b.is_a? Array and b.all? { |k| k.is_a? Class } and b.any? { |k| a.is_a? k } @klass, @default = b, a else raise ArgumentError, 'need a Class and a default value' end when 0 else raise ArgumentError, 'too many values' end # removed because it causes some probleme when distributed such a class #[@name, @descr, @default].each { |x| x.freeze } end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
15 16 17 |
# File 'lib/attributed_class.rb', line 15 def default @default end |
#default_proc ⇒ Object (readonly)
Returns the value of attribute default_proc.
14 15 16 |
# File 'lib/attributed_class.rb', line 14 def default_proc @default_proc end |
#descr ⇒ Object (readonly)
Returns the value of attribute descr.
14 15 16 |
# File 'lib/attributed_class.rb', line 14 def descr @descr end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
14 15 16 |
# File 'lib/attributed_class.rb', line 14 def klass @klass end |
#mandatory ⇒ Object
Returns the value of attribute mandatory.
15 16 17 |
# File 'lib/attributed_class.rb', line 15 def mandatory @mandatory end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/attributed_class.rb', line 14 def name @name end |
Class Method Details
.set_default_proc(&block) ⇒ Object
92 93 94 |
# File 'lib/attributed_class.rb', line 92 def self.set_default_proc ( &block ) @@default_proc = block end |
Instance Method Details
#compute_default(default_proc_args) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/attributed_class.rb', line 126 def compute_default ( default_proc_args ) unless @default_proc.nil? pr = @default_proc if pr.arity == default_proc_args.size @default = pr[*default_proc_args] else @default = pr[self, *default_proc_args] end end return @default end |
#help(output = STDERR) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/attributed_class.rb', line 97 def help ( output=STDERR ) output << "#@name: #@descr" output << " [#@klass]" unless @klass.nil? other = [] other << 'mandatory' if mandatory? other << 'invisible' if invisible? output << " (#{other.join(', ')})" unless other.empty? output end |
#inspect ⇒ Object
107 108 109 |
# File 'lib/attributed_class.rb', line 107 def inspect help('#<') + '>' end |
#invisible? ⇒ Boolean
78 79 80 |
# File 'lib/attributed_class.rb', line 78 def invisible? !@visible end |
#mandatory? ⇒ Boolean
82 83 84 |
# File 'lib/attributed_class.rb', line 82 def mandatory? @mandatory end |
#to_form(b) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/attributed_class.rb', line 111 def to_form ( b ) if (!@default.nil?) or @klass.nil? gfx = @default elsif @klass.is_a? Array and @klass.size == 1 gfx = @klass.first.default_instance else gfx = @klass end gfx = gfx.to_gfx_element gfx.object = 'test' gfx.builder = b gfx.attribute = self gfx.to_form end |
#valid?(anObject) ⇒ Boolean
86 87 88 89 |
# File 'lib/attributed_class.rb', line 86 def valid? ( anObject ) return ! mandatory? if anObject.nil? @klass.nil? or @klass.empty? or @klass.any? { |k| anObject.is_a? k } end |
#visible? ⇒ Boolean
74 75 76 |
# File 'lib/attributed_class.rb', line 74 def visible? @visible end |