Sha256: bcf6cbab5d56eb1737f0010eaab0294e4ae6493994c97c395530cd23991a5589

Contents?: true

Size: 1.62 KB

Versions: 92

Compression:

Stored size: 1.62 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.is_a?(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!
    replace(symbolize_keys)
  end
end

Version data entries

92 entries across 92 versions & 1 rubygems

Version Path
trusty-cms-7.0.22 lib/annotatable.rb
trusty-cms-7.0.21 lib/annotatable.rb
trusty-cms-7.0.20 lib/annotatable.rb
trusty-cms-7.0.19 lib/annotatable.rb
trusty-cms-7.0.18 lib/annotatable.rb
trusty-cms-7.0.17 lib/annotatable.rb
trusty-cms-7.0.16 lib/annotatable.rb
trusty-cms-7.0.14 lib/annotatable.rb
trusty-cms-7.0.13 lib/annotatable.rb
trusty-cms-7.0.12 lib/annotatable.rb
trusty-cms-7.0.15 lib/annotatable.rb
trusty-cms-7.0.9.1 lib/annotatable.rb
trusty-cms-7.0.11 lib/annotatable.rb
trusty-cms-7.0.10 lib/annotatable.rb
trusty-cms-7.0.9 lib/annotatable.rb
trusty-cms-7.0.8 lib/annotatable.rb
trusty-cms-7.0.7 lib/annotatable.rb
trusty-cms-7.0.6 lib/annotatable.rb
trusty-cms-7.0.5 lib/annotatable.rb
trusty-cms-7.0.4 lib/annotatable.rb