require 'spec_helper' require 'fedux_org/stdlib/models/filesystem_based_model' describe Models::FilesystemBasedModel do before(:each) do @model = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel def self.load_from_filesystem [ :hello, :world ].each { |s| create( s ) } end end @model.clear #where a name for a class is relevant module Test123 module Models class TestClassForFilesystem < FeduxOrg::Stdlib::Models::BaseModel include FeduxOrg::Stdlib::Models::FilesystemBasedModel end end end end it "finds all available filter" do @model.init filter = @model.find(:hello) expect(filter.name).to eq(:hello) end it "is default that all found filters are disabled" do @model.init all_filter = @model.all all_not_enabled_filter = @model.find_all(enabled: false) expect(all_filter.size).to eq(all_not_enabled_filter.size) end it "enables a filter by request" do @model.init @model.enable(:hello) all_enabled_filter = @model.find_all( enabled: true ) filter = @model.find( :hello ) expect( all_enabled_filter ).to eq( [ filter ] ) end it "s find method supports a hash based syntax" do @model.init @model.enable(:hello) enabled_filter = @model.find(name: :hello, enabled: true) expect(enabled_filter.name).to eq(:hello) end it "s find method supports a symbol based syntax as well. The search string is compared against the name method." do @model.init enabled_filter = @model.find(:hello) expect(enabled_filter.name).to eq(:hello) enabled_filter = @model.find_all(:hello) expect(enabled_filter.first.name).to eq(:hello) end it "s find method supports a string based syntax as well. The search string is compared against the name method." do @model.init enabled_filter = @model.find('hello') expect(enabled_filter.name).to eq(:hello) enabled_filter = @model.find_all('hello') expect(enabled_filter.first.name).to eq(:hello) end it "raises an error if an unknown search criteria is used" do @model.init expect { enabled_filter = @model.find(unknown: 'hello') }.to raise_error FeduxOrg::Stdlib::Models::Exceptions::InvalidSearchCriteria end it "returns the module name" do expect( Test123::Models::TestClassForFilesystem.send(:module_name) ).to eq('Test123::Models') end it "returns the model name" do expect( Test123::Models::TestClassForFilesystem.send(:model_name) ).to eq('TestClassForFilesystem') end context "#library_name" do it "returns the library name" do expect( Test123::Models::TestClassForFilesystem.send(:library_name) ).to eq('Test123') end end context "#path_to_instances" do klass = Class.new( FeduxOrg::Stdlib::Models::BaseModel ) do include FeduxOrg::Stdlib::Models::FilesystemBasedModel def self.model_name 'FindFiles' end def self.path File.join( examples_dir, 'models', 'filesystem_based', 'find_files') end end it "returns the path to the instances" do expect( klass.send(:path_to_instances) ).to eq( File.join( examples_dir, 'models', 'find_files') ) end end context "#find_files" do klass = Class.new( FeduxOrg::Stdlib::Models::BaseModel ) do include FeduxOrg::Stdlib::Models::FilesystemBasedModel def self.path_to_instances File.join( examples_dir, 'models', 'filesystem_based', 'find_files') end def self.suffix '.rb' end end it "search for available instances in filesystem" do expect( klass.send(:find_files) ).to eq( [ "/home/d/work/projects/fedux_org-stdlib/spec/examples/models/filesystem_based/find_files/abc.rb", "/home/d/work/projects/fedux_org-stdlib/spec/examples/models/filesystem_based/find_files/cde.rb", ] ) end end context "#init" do it "init should only add unknown instances" do @model.init count1 = @model.all.size @model.init count2 = @model.all.size expect(count2).to eq(count1) end end end