| Class | AttributedClass::Attribute |
| In: |
lib/attributed_class.rb
|
| Parent: | Object |
| default | [RW] | |
| default_proc | [R] | |
| descr | [R] | |
| klass | [R] | |
| mandatory | [RW] | |
| name | [R] |
# 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
# File lib/attributed_class.rb, line 92 def self.set_default_proc ( &block ) @@default_proc = block end
# 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
# 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
# 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