Sha256: f1af4ae9350f505d225fa8109ca7e5b86ae289f22e3992178403ec682c4b36f6

Contents?: true

Size: 1.64 KB

Versions: 47

Compression:

Stored size: 1.64 KB

Contents

module Annotatable

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def self.extended(base)
      base.instance_eval do
        alias :inherited_without_annotatable :inherited
        alias :inherited :inherited_with_annotatable
      end
    end
    
    def annotate(*attrs)
      options = {}
      options = attrs.pop if attrs.last.kind_of?(Hash)
      options.symbolize_keys!
      inherit = options[:inherit]
      if inherit
        @inherited_annotations ||= []
        @inherited_annotations += attrs
      end
      attrs.each do |attr|
        class_eval %{
          def self.#{attr}(string = nil)
            @#{attr} = string || @#{attr}
          end
          def #{attr}(string = nil)
            self.class.#{attr}(string)
          end
          def self.#{attr}=(string = nil)
            #{attr}(string)
          end
          def #{attr}=(string = nil)
            self.class.#{attr}=(string)
          end
        }
      end
      attrs
    end

    def inherited_with_annotatable(subclass)
      inherited_without_annotatable(subclass)
      (["inherited_annotations"] + (@inherited_annotations || [])).each do |t|
        ivar = "@#{t}" 
        subclass.instance_variable_set(ivar, instance_variable_get(ivar))
      end
    end
  end

end

# We don't necessarily have ActiveSupport loaded yet!
class Hash
  # Return a new hash with all keys converted to symbols.
  def symbolize_keys
    inject({}) do |options, (key, value)|
      options[key.to_sym || key] = value
      options
    end
  end

  # Destructively convert all keys to symbols.
  def symbolize_keys!
    self.replace(self.symbolize_keys)
  end
end

Version data entries

47 entries across 47 versions & 6 rubygems

Version Path
trusty-cms-1.0.0 lib/annotatable.rb
radiant-1.1.4 lib/annotatable.rb
radiant-1.1.3 lib/annotatable.rb
radiant-1.1.2 lib/annotatable.rb
radiant-1.1.1 lib/annotatable.rb
radiant-1.1.0 lib/annotatable.rb
radiant-1.1.0.rc1 lib/annotatable.rb
radiant-1.1.0.beta lib/annotatable.rb
radiant-1.0.1 lib/annotatable.rb
radiant-1.1.0.alpha lib/annotatable.rb
radiant-1.0.0 lib/annotatable.rb
radiant-1.0.0.rc5 lib/annotatable.rb
radiant-1.0.0.rc4 lib/annotatable.rb
radiant-1.0.0.rc3 lib/annotatable.rb
kajam-1.0.3.rc2 lib/annotatable.rb
radiant-1.0.0.rc2 lib/annotatable.rb
radiant-1.0.0.rc1 lib/annotatable.rb
radiant-rails3-0.1 lib/annotatable.rb
radiantcms-couchrest_model-0.2.4 lib/annotatable.rb
radiantcms-couchrest_model-0.2.2 lib/annotatable.rb