Sha256: cce764348fff649245f3b712aca189997d3398efd329c51c683edc2f491f519f

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

# inherit Micon::Managed
#  scope [:session | :thread | :instance | :application | :<custom>
#  
#  inject :attribute => Session
#  
#  inject :attribute => [:scope, :variable]
#
#
class Micon
  module Managed
    module ClassMethods
    end
    
    class << self
      def hook! klass = Module
        klass.class_eval do
          def inject attributes
            ::Micon::Managed.inject self, attributes
          end

          def scope scope
            ::Micon::Managed.scope self, scope
          end
        end
      end
    
      def inject klass, attributes
        raise_without_self "Invalid argument!", Micon unless attributes.is_a? Hash
        attributes.each do |name, specificator|
          raise_without_self "Attribute name should be a Symbol!", Micon unless name.is_a? Symbol        
      
          if [Class, Module].include? specificator.class
            specificator = specificator.name
          elsif specificator.is_a? Symbol
            specificator = ":#{specificator}"
          else
            specificator = "\"#{specificator}\""
          end

          script = %{\
    def #{name}
      ::Micon[#{specificator}]
    end

    def #{name}= value
      ::Micon[#{specificator}] = value
    end}
          klass.class_eval script, __FILE__, __LINE__
        end
      end 
      
      def scope klass, scope
        raise_without_self "Micon Name shoul be a Symbol!", Micon unless scope.is_a? Symbol
        ::Micon.register(klass, scope){klass.new}
      end
    end
  end
end

Micon::Managed.hook! Micon::Managed::ClassMethods

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby-ext-0.4.2 lib/micon/managed.rb
ruby-ext-0.4.1 lib/micon/managed.rb
ruby-ext-0.4.0 lib/micon/managed.rb