module FeduxOrgStdlib::Models::FilesystemBasedModel::ClassMethods

Class methods

Public Instance Methods

init() click to toggle source

initialize model

# File lib/fedux_org_stdlib/models/filesystem_based_model.rb, line 15
def init
  load_from_filesystem
end

Private Instance Methods

find_files() click to toggle source
# File lib/fedux_org_stdlib/models/filesystem_based_model.rb, line 52
def find_files
  files = []
  Find.find(path_to_instances) do |path|
    next unless File.file? path
    next if suffix && path !~ /#{suffix}$/
    next if path =~ /^\.\.?/
    next if path =~ /#{ignore}/

    files << path
  end

  files
rescue
  raise FeduxOrgStdlib::Models::Exceptions::NoImplementationsForModelFound, "You might need to create the directory\"#{File.dirname(path_to_instances)}\"."
end
forbidden_keywords() click to toggle source
# File lib/fedux_org_stdlib/models/filesystem_based_model.rb, line 89
def forbidden_keywords
  []
end
fqcn() click to toggle source
# File lib/fedux_org_stdlib/models/filesystem_based_model.rb, line 28
def fqcn
  to_s.split(/::/)
end
ignore() click to toggle source
# File lib/fedux_org_stdlib/models/filesystem_based_model.rb, line 48
def ignore
  'BORING_STRING_WHICH_SHOULD_NEVER_MATCH'
end
library_name() click to toggle source
# File lib/fedux_org_stdlib/models/filesystem_based_model.rb, line 36
def library_name
  fqcn.first
end
load_from_filesystem() click to toggle source
# File lib/fedux_org_stdlib/models/filesystem_based_model.rb, line 85
def load_from_filesystem
  fail FeduxOrgStdlib::Models::Exceptions::MethodNeedsToBeImplemented
end
model_name() click to toggle source
# File lib/fedux_org_stdlib/models/filesystem_based_model.rb, line 32
def model_name
  fqcn.last
end
model_path() click to toggle source
# File lib/fedux_org_stdlib/models/filesystem_based_model.rb, line 44
def model_path
  fail FeduxOrgStdlib::Models::Exceptions::MethodNeedsToBeImplemented, "Please defined the method \"model_path\" to make the library work."
end
module_name() click to toggle source
# File lib/fedux_org_stdlib/models/filesystem_based_model.rb, line 21
def module_name
  name = fqcn
  name.pop

  name.join('::')
end
name(path) click to toggle source
# File lib/fedux_org_stdlib/models/filesystem_based_model.rb, line 76
def name(path)
  name = File.basename(path, suffix).to_sym
  fail FeduxOrgStdlib::Models::Exceptions::UnauthorizedUseOfKeyword if forbidden_keywords.include? name

  FeduxOrgStdlib.logger.debug(self) { "Name of model: #{name}" }

  name
end
path_to_instances() click to toggle source
# File lib/fedux_org_stdlib/models/filesystem_based_model.rb, line 68
def path_to_instances
  return_path = ::File.expand_path("../../#{model_name.pluralize.underscore}", model_path)

  FeduxOrgStdlib.logger.debug(self) { "Path to instances of model: #{return_path}" }

  return_path
end
suffix() click to toggle source
# File lib/fedux_org_stdlib/models/filesystem_based_model.rb, line 40
def suffix
  fail FeduxOrgStdlib::Models::Exceptions::MethodNeedsToBeImplemented, "Please defined the method \"suffix\" to make the library work."
end