class Schemaker::Models

Attributes

join_model[RW]
object_model[RW]
subject_model[RW]

Public Class Methods

model_types() click to toggle source
# File lib/schemaker/models.rb, line 65
def self.model_types
  [:object, :subject, :join]
end
new(subject_class, object_class, join_class, options = {}) click to toggle source

Sets up the models that take part in the model relationship to be configured @param subject_class [Class] @param object_class [Class] @param join_class [Class]

@param options [Hash] - contains the key to be used for the main field (subject key) and possibly other options to configure the models more precisely as needed

# File lib/schemaker/models.rb, line 31
def initialize subject_class, object_class, join_class, options = {}
  raise ArgumentError, "subject class not given" if !subject_class
  raise ArgumentError, "object class not given" if !object_class

  @subject_model  = SubjectModel.new self, subject_class, options[:subject_key]
  @object_model   = ObjectModel.new self, object_class
  @join_model     = JoinModel.new(self, join_class) if join_class
end

Public Instance Methods

configure() click to toggle source

configure each model in turn

# File lib/schemaker/models.rb, line 47
def configure
  return quick_join if !join_model
  [subject_model, object_model, join_model].compact.each do |model| 
    model.configure
  end  
end
get_class(type) click to toggle source

retrieves a given Class ie. a type of model @param [Class, String, Symbol, BaseModel] which class to get @return [Class] the Class (model) of interest

# File lib/schemaker/models.rb, line 84
def get_class type
  case type
  when Class
    type
  when BaseModel
    type.my_class
  when String, Symbol
    return get_class send("#{type}_model") if [:subject, :object, :join].include?(type.to_sym)
    type.to_s.constantize
  else
    raise "Can't determine a class from: #{type}"
  end          
end
key(type) click to toggle source

creates a key for a given type @param type [Symbol] - either :object, :subject or :join

# File lib/schemaker/models.rb, line 42
def key type
  make_key get_class(type)
end
logs() click to toggle source
# File lib/schemaker/models.rb, line 58
def logs
  @logs ||= [subject_model, object_model, join_model].compact.inject([]) do |res, model|
    res << model.logs
    res
  end.flatten
end
make_key(class_name) click to toggle source

creates a key from a class name fx UsersRoles becomes :user_roles, where only the last part is pluralised! @param [String] the class name

# File lib/schemaker/models.rb, line 101
def make_key class_name
  name = class_name.to_s.pluralize.gsub(/::/, '__').underscore
  only_last_part_plural(name).to_sym
end
quick_join() click to toggle source
# File lib/schemaker/models.rb, line 54
def quick_join
  subject_model.quick_join
end

Protected Instance Methods

only_last_part_plural(cls_name) click to toggle source

Takes a composite name and makes a nice sounding key that follows the Rails conventions fx UsersRoles becomes :user_roles, where only the last part is pluralised! @param [String] the class name, fx UsersRoles

# File lib/schemaker/models.rb, line 111
def only_last_part_plural cls_name 
  parts = cls_name.split('_')
  name = parts.inject([]) do |res, part|
    res << (part != parts.last ? part.singularize : part)
    res
  end.join('_')          
end