Class AttributedClass::Attribute
In: lib/attributed_class.rb
Parent: Object

Methods

Attributes

default  [RW] 
default_proc  [R] 
descr  [R] 
klass  [R] 
mandatory  [RW] 
name  [R] 

Public Class methods

[Source]

# 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

[Source]

# File lib/attributed_class.rb, line 92
    def self.set_default_proc ( &block )
      @@default_proc = block
    end

Public Instance methods

[Source]

# 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

[Source]

# 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

[Source]

# File lib/attributed_class.rb, line 107
    def inspect
      help('#<') + '>'
    end

[Source]

# File lib/attributed_class.rb, line 78
    def invisible?
      !@visible
    end

[Source]

# File lib/attributed_class.rb, line 82
    def mandatory?
      @mandatory
    end

[Source]

# 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

[Source]

# 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

[Source]

# File lib/attributed_class.rb, line 74
    def visible?
      @visible
    end

[Validate]