Sha256: c8afeb2d54bfcee0e83cf31ace67640991eb8febd047add849ad918ac8aebd37

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'active_support/core_ext/string/inflections'
require 'fedux_org/stdlib/models/filesystem_based_model'

module FeduxOrg
  module Stdlib
    module Models
      # model for import action
      module ClassBasedModel

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

        module ClassMethods

          def require_path(name)
            File.join( module_name.underscore , model_name.pluralize.underscore , name.to_s )
          end

          def exception_for_model
            "#{library_name}::Exceptions::Invalid#{model_name}".constantize
          rescue
            raise FeduxOrg::Stdlib::Models::Exceptions::ExceptionNeedsToBeImplemented, "Exception \"#{library_name}::Exceptions::Invalid#{model_name}\" does not exist."
          end

          def check_klass( klass , method)
            raise exception_for_model, "A valid \"#{model_name}\"-class needs to respond to \"#{ method }\"." unless klass.new.respond_to? method
          end

          def build_class_constant(name)
            "#{library_name}::#{model_name.pluralize}::#{name.to_s.camelcase}".constantize
          rescue
            raise exception_for_model , "The filename needs to be snakecase and needs to be convertible to the action class name: filename in camelcase."
          end

          def check_method
            raise FeduxOrg::Stdlib::Models::Exceptions::MethodNeedsToBeImplemented, "Method \"check_method\" does not exist."
          end

          def suffix
            '.rb'
          end

          def load_from_filesystem
            files = Dir.glob( path_to_instances )

            files.each do |f| 
              instance_name = name( f )

              require require_path( instance_name )

              instance_klass = build_class_constant( instance_name )
              check_klass( instance_klass, check_method )
              create( instance_name , instance_klass.new )
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fedux_org-stdlib-0.0.3 lib/fedux_org/stdlib/models/class_based_model.rb