Sha256: 2f171f68c6bed5b668559300de388dafa25a20f4e5cf6997051c6d524d01387f

Contents?: true

Size: 1.83 KB

Versions: 11

Compression:

Stored size: 1.83 KB

Contents

class Class

  @@anns = {}
  @@attr_anns = {}

  # Defines annotation(s) for the next defined +attr_reader+ or
  # +attr_accessor+. The +anns+ argument should be a Hash defining annotations
  # for the associated attr. Example:
  #
  #   require 'rscm/annotations'
  #
  #   class EmailSender
  #     ann :description => "IP address of the mail server", :tip => "Use 'localhost' if you have a good box, sister!"
  #     attr_accessor :server
  #   end
  #
  # The EmailSender class' annotations can then be accessed like this:
  #
  #   EmailSender.server[:description] # => "IP address of the mail server"
  #
  # Yeah right, cool, whatever. What's this good for? It's useful for example if you want to
  # build some sort of user interface (for example in on Ruby on Rails) that allows editing of
  # fields, and you want to provide an explanatory text and a tooltip in the UI.
  #
  # You may also use annotations to specify more programmatically meaningful metadata. More power to you.
  # 
  def ann(anns)
    @@attr_anns ||= {}
    @@attr_anns.merge!(anns)
  end
  
  def method_missing(sym, *args) #:nodoc:
    anns = @@anns[self]
    return anns[sym] if(anns && anns[sym])
    return superclass.method_missing(sym, *args) if superclass
    return {}
  end

  alias old_attr_reader attr_reader #:nodoc:
  def attr_reader(*syms) #:nodoc:
    @@anns[self] ||= {}
    syms.each do |sym|
      @@anns[self][sym] = @@attr_anns.dup if @@attr_anns
    end
    @@attr_anns = nil
    old_attr_reader(*syms)
  end

  def attr_accessor(*syms) #:nodoc:
    attr_reader(*syms)
    attr_writer(*syms)
  end
end

class Object
  def anns(attr_name)
    self.class.send(attr_name)
  end

  def __attr_accessors
    attrs = []
    methods.each do |method|
      if(method =~ /(.*)=/)
        attrs << "@#{$1}" if methods.index($1) && $1 != "=="
      end
    end
    attrs.sort
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rscm-0.3.10 lib/rscm/annotations.rb
rscm-0.3.1 lib/rscm/annotations.rb
rscm-0.3.5 lib/rscm/annotations.rb
rscm-0.3.2 lib/rscm/annotations.rb
rscm-0.3.0 lib/rscm/annotations.rb
rscm-0.3.3 lib/rscm/annotations.rb
rscm-0.3.4 lib/rscm/annotations.rb
rscm-0.3.7 lib/rscm/annotations.rb
rscm-0.3.9 lib/rscm/annotations.rb
rscm-0.3.8 lib/rscm/annotations.rb
rscm-0.3.6 lib/rscm/annotations.rb