Module: FeduxOrgStdlib::Models::ClassBasedModel::ClassMethods

Defined in:
lib/fedux_org_stdlib/models/class_based_model.rb

Overview

Class methods

Instance Method Summary collapse

Instance Method Details

#build_class_constant(name) ⇒ Object



34
35
36
37
38
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 34

def build_class_constant(name)
  "#{library_name}::#{model_name.pluralize}::#{name.to_s.camelcase}".constantize
rescue
  raise exception_for_model, "Class \"#{library_name}::#{model_name.pluralize}::#{name.to_s.camelcase}\" cannot be found in file \"#{require_path(name)}\"."
end

#check_klass(klass, *methods) ⇒ Object



30
31
32
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 30

def check_klass(klass, *methods)
  fail exception_for_model, "A valid \"#{model_name}\"-class needs to respond to \"#{ methods.flatten.join(', ') }\"." unless methods.flatten.all? { |m| klass.new.respond_to? m }
end

#check_methodObject



40
41
42
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 40

def check_method
  fail FeduxOrgStdlib::Models::Exceptions::MethodNeedsToBeImplemented, "Method \"check_method\" does not exist."
end

#exception_for_modelObject



24
25
26
27
28
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 24

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

#load_from_filesystemObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 48

def load_from_filesystem
  files = find_files

  FeduxOrgStdlib.logger.debug(self) { "Files found at path \"#{path_to_instances}\": #{ files.join(', ') }" }

  fail FeduxOrgStdlib::Models::Exceptions::NoImplementationsForModelFound, "You might need to store files at \"#{File.dirname(path_to_instances)}\" to make the library work." if files.blank?

  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

#require_path(name) ⇒ Object



17
18
19
20
21
22
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 17

def require_path(name)
  path = File.join(library_name.underscore, model_name.pluralize.underscore, name.to_s)
  FeduxOrgStdlib.logger.debug(self) { "Path to instances of model \"#{name}\": #{path}" }

  path
end

#suffixObject



44
45
46
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 44

def suffix
  '.rb'
end