require 'spec_helper' require 'fedux_org/stdlib/models/class_based_model' describe Models::ClassBasedModel do before(:each) do module TestIt module Exceptions class InvalidTestClass < Exception; end end end module Test123 module Models class TestClassForFilesystem < FeduxOrg::Stdlib::Models::BaseModel include FeduxOrg::Stdlib::Models::FilesystemBasedModel include FeduxOrg::Stdlib::Models::ClassBasedModel def self.path File.join( examples_dir, 'models', 'class_based.rb' ) end end end end module Test123 module Models class TestClassForFilesystem2 < FeduxOrg::Stdlib::Models::BaseModel include FeduxOrg::Stdlib::Models::FilesystemBasedModel include FeduxOrg::Stdlib::Models::ClassBasedModel def self.path File.join( examples_dir, 'models', 'forbidden.rb' ) end def self.forbidden_keywords [ :forbidden ] end end end end end it "loads all available instances from the file system and make the classes available." do # all loaded instances need be defined like that # # model: # ------ # class Testmodel # end # # Class to be loaded for instance of model (second level is plural of model name) # Furthermore you need to define a method which is used as a check for a valid # class. That class must not take an argument in #new! All arguments need to be # taken by the '#method'. # -------------------------------------------------------------------------------- # module Bla # module Testmodels # class XY # def method # end # end # end # end test_class = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel include Models::ClassBasedModel def initialize(name, klass) super(name) @klass = klass end def self.model_name 'TestClass' end def self.library_name 'TestIt' end def self.check_method :req_method end def self.path_to_instances File.join( examples_dir, 'models', 'class_based', 'valid_1.rb' ) end def self.require_path(name) File.join( examples_dir, 'models', 'class_based', 'valid_1' ) end end test_class.init expect(test_class.all.first.name).to eq(:valid_1) end it "raises an error if an instance is invalid: name error" do test_class = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel include Models::ClassBasedModel def initialize(name, klass) super(name) @klass = klass end def self.model_name "TestClass" end def self.library_name 'TestIt' end def self.check_method :req_method end def self.path_to_instances File.join( examples_dir, 'models', 'class_based', 'invalid_1.rb' ) end def self.require_path(name) File.join( examples_dir, 'models', 'class_based', 'invalid_1' ) end end expect { test_class.init }.to raise_error end it "an import filter uses a forbidden keywords as name" do test_class = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel include Models::ClassBasedModel def initialize(name, klass) super(name) @klass = klass end def self.model_name "TestClass" end def self.library_name 'TestIt' end def self.path_to_instances File.join( examples_dir, 'models', 'class_based', 'forbidden_keyword.rb' ) end def self.require_path(name) File.join( examples_dir, 'models', 'class_based', 'forbidden_keyword' ) end def self.forbidden_keywords [ :forbidden_keyword ] end end expect { test_class.init }.to raise_error FeduxOrg::Stdlib::Models::Exceptions::UnauthorizedUseOfKeyword end it "raises an error if an import filter is invalid: missing method" do test_class = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel include Models::ClassBasedModel def initialize(name, klass) super(name) @klass = klass end def self.model_name "TestClass" end def self.library_name 'TestIt' end def self.path_to_instances File.join( examples_dir, 'models', 'class_based', 'invalid_2.rb' ) end def self.require_path(name) File.join( examples_dir, 'models', 'class_based', 'invalid_2' ) end def self.check_method :req_method end end expect { test_class.init }.to raise_error TestIt::Exceptions::InvalidTestClass end it "returns the require path" do model = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel def self.require_path(name) File.join( examples_dir, 'models', 'class_based', 'invalid_2' ) end end expect_result = File.join( examples_dir, 'models', 'class_based', 'invalid_2' ) result = model.send( :require_path, :name ) expect( result ).to eq( expect_result ) end it "returns the path to instances" do expected_result= File.join( examples_dir, 'test_class_for_filesystems', '*.rb' ) expect( Test123::Models::TestClassForFilesystem.send(:path_to_instances) ).to eq( expected_result ) end it "returns the name for the instance based on the path to the ruby file" do expect( Test123::Models::TestClassForFilesystem.send(:name, Test123::Models::TestClassForFilesystem.send( :path ) ) ).to eq( :class_based ) end it "check if the name for the instances is forbidden" do expect { Test123::Models::TestClassForFilesystem2.send(:name, Test123::Models::TestClassForFilesystem2.send( :path ) ) }.to raise_error FeduxOrg::Stdlib::Models::Exceptions::UnauthorizedUseOfKeyword end end