Module: FeduxOrgStdlib::Models::ClassBasedModel::ClassMethods
- Defined in:
- lib/fedux_org_stdlib/models/class_based_model.rb
Instance Method Summary collapse
- #build_class_constant(name) ⇒ Object
- #check_klass(klass, *methods) ⇒ Object
- #check_method ⇒ Object
- #exception_for_model ⇒ Object
- #load_from_filesystem ⇒ Object
- #require_path(name) ⇒ Object
- #suffix ⇒ Object
Instance Method Details
#build_class_constant(name) ⇒ Object
33 34 35 36 37 |
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 33 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
29 30 31 |
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 29 def check_klass( klass , *methods) raise 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_method ⇒ Object
39 40 41 |
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 39 def check_method raise FeduxOrgStdlib::Models::Exceptions::MethodNeedsToBeImplemented, "Method \"check_method\" does not exist." end |
#exception_for_model ⇒ Object
23 24 25 26 27 |
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 23 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_filesystem ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 47 def load_from_filesystem files = find_files FeduxOrgStdlib.logger.debug(self) { "Files found at path \"#{path_to_instances}\": #{ files.join(", ") }" } raise 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
16 17 18 19 20 21 |
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 16 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 |
#suffix ⇒ Object
43 44 45 |
# File 'lib/fedux_org_stdlib/models/class_based_model.rb', line 43 def suffix '.rb' end |