Module: PuppetGenerator::Models::ClassBasedModel::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#build_class_constant(name) ⇒ Object



26
27
28
29
30
# File 'lib/puppet_generator/models/class_based_model.rb', line 26

def build_class_constant(name)
  "PuppetGenerator::#{model_name.pluralize}::#{name.to_s.camelcase}".constantize
rescue
  raise exception_for_model , "The filename needs to be snakecase and needs to be convertible to the action class name: filename in camelcase."
end

#check_klass(klass, method) ⇒ Object



22
23
24
# File 'lib/puppet_generator/models/class_based_model.rb', line 22

def check_klass( klass , method)
  raise exception_for_model, "A valid \"#{model_name}\"-class needs to respond to \"#{ method }\"." unless klass.new.respond_to? method
end

#check_methodObject



32
33
34
# File 'lib/puppet_generator/models/class_based_model.rb', line 32

def check_method
  raise Exceptions::MethodNeedsToBeImplemented, "Method \"check_method\" does not exist."
end

#exception_for_modelObject



16
17
18
19
20
# File 'lib/puppet_generator/models/class_based_model.rb', line 16

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

#load_from_filesystemObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppet_generator/models/class_based_model.rb', line 40

def load_from_filesystem
  files = Dir.glob( path_to_instances )

  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



12
13
14
# File 'lib/puppet_generator/models/class_based_model.rb', line 12

def require_path(name)
  File.join( PuppetGenerator.gem_load_path, model_name.pluralize.underscore , name.to_s )
end

#suffixObject



36
37
38
# File 'lib/puppet_generator/models/class_based_model.rb', line 36

def suffix
  '.rb'
end