Sha256: 3eaba9d2f8c5e6d1fd55c349313d74079614950e8557ebd0f2af6f22b336eaae

Contents?: true

Size: 1.92 KB

Versions: 9

Compression:

Stored size: 1.92 KB

Contents

module Gorillib
  module Model
    module NamedSchema

    protected

      #
      # Returns the meta_module -- a module extending the type, on which all the
      # model methods are inscribed. This allows you to override the model methods
      # and call +super()+ to get the generic behavior.
      #
      # The meta_module is named for the including class, but with 'Meta::'
      # prepended and 'Type' appended -- so Geo::Place has meta_module
      # "Meta::Geo::PlaceType"
      #
      def meta_module
        return @_meta_module if defined?(@_meta_module)
        if self.name
          @_meta_module = ::Gorillib::Model::NamedSchema.get_nested_module("Meta::#{self.name}Type")
        else
          @_meta_module = Module.new
        end
        self.class_eval{ include(@_meta_module) }
        @_meta_module
      end

      def define_meta_module_method(method_name, visibility=:public, &block)
        if (visibility == false) then return               ; end
        if (visibility == true)  then visibility = :public ; end
        Validate.included_in!("visibility", visibility, [:public, :private, :protected])
        meta_module.module_eval{ define_method(method_name, &block) }
        meta_module.module_eval "#{visibility} :#{method_name}", __FILE__, __LINE__
      end

      # Returns a module for the given names, rooted at Object (so
      # implicity with '::').
      # @example
      #   get_nested_module(["This", "That", "TheOther"])
      #   # This::That::TheOther
      def self.get_nested_module(name)
        name.split('::').inject(Object) do |parent_module, module_name|
          # inherit = false makes these methods be scoped to parent_module instead of universally
          if parent_module.const_defined?(module_name, false)
            parent_module.const_get(module_name, false)
          else
            parent_module.const_set(module_name.to_sym, Module.new)
          end
        end
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
gorillib-model-0.0.3 lib/gorillib/model/named_schema.rb
gorillib-model-0.0.1 lib/gorillib/model/named_schema.rb
gorillib-0.6.0 lib/gorillib/model/named_schema.rb
gorillib-0.5.2 lib/gorillib/model/named_schema.rb
gorillib-0.5.0 lib/gorillib/model/named_schema.rb
gorillib-0.4.2 lib/gorillib/model/named_schema.rb
gorillib-0.4.2pre lib/gorillib/model/named_schema.rb
gorillib-0.4.0pre lib/gorillib/model/named_schema.rb
gorillib-0.4.1pre lib/gorillib/model/named_schema.rb