Module: Anise::Attribute

Defined in:
lib/anise/attribute.rb

Overview

Annotated Attributes

This framework modifies the major attr_* methods to allow easy addition of annotations. It modifies the built in attribute methods (attr, attr_reader, attr_writer and attr_accessor), to allow annotations to be added to them directly rather than requiring a separate #ann statement.

require 'anise/attribute'

class X
  include Anise::Attribute

  attr :a, :valid => lambda{ |x| x.is_a?(Integer) }
end

See annotation.rb for more information.

NOTE: This library was designed to be backward compatible with the standard versions of the same methods.

Defined Under Namespace

Modules: Aid

Class Method Summary collapse

Class Method Details

.annotatable_attribute_method(base, attr_method_name) ⇒ Object



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
73
74
75
# File 'lib/anise/attribute.rb', line 42

def self.annotatable_attribute_method(base, attr_method_name)
  base.module_eval do
    define_method(attr_method_name) do |*args|
      args.flatten!

      harg={}; while args.last.is_a?(Hash)
        harg.update(args.pop)
      end

      raise ArgumentError if args.empty? and harg.empty?

      if args.empty?  # hash mode
        harg.each { |a,h| __send__(attr_method_name,a,h) }
      else
        klass = harg[:class] = args.pop if args.last.is_a?(Class)

        #attr_method.call(*args)
        super(*args)

        args.each{|a| ann(a.to_sym,harg)}

        instance_attributes!.concat(args)  #merge!

        # Use this callback to customize for your needs.
        if respond_to?(:attr_callback)
          attr_callback(self, args, harg)
        end

        # return the names of the attributes created
        return args
      end
    end
  end
end

.included(base) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/anise/attribute.rb', line 30

def self.included(base)
  base.send(:include, Annotation) #.append_features(base)
  base.extend Aid

  #inheritor :instance_attributes, [], :|
  base_class = (class << base; self; end)
  base_class.attribute_methods.each do |attr_method|
    annotatable_attribute_method(base_class, attr_method)
  end
end