Class: Schemaker::Models

Inherits:
Object
  • Object
show all
Defined in:
lib/schemaker/models.rb,
lib/schemaker/models/base_model.rb,
lib/schemaker/models/join_model.rb,
lib/schemaker/models/object_model.rb,
lib/schemaker/models/subject_model.rb

Defined Under Namespace

Classes: BaseModel, JoinModel, ObjectModel, SubjectModel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject_class, object_class, join_class, options = {}) ⇒ Models

Sets up the models that take part in the model relationship to be configured

Parameters:

  • subject_class (Class)
  • object_class (Class)
  • join_class (Class)
  • options (Hash) (defaults to: {})
    • contains the key to be used for the main field (subject key) and possibly other options to configure the models more precisely as needed

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
# 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

Instance Attribute Details

#join_modelObject

Returns the value of attribute join_model.



24
25
26
# File 'lib/schemaker/models.rb', line 24

def join_model
  @join_model
end

#object_modelObject

Returns the value of attribute object_model.



24
25
26
# File 'lib/schemaker/models.rb', line 24

def object_model
  @object_model
end

#subject_modelObject

Returns the value of attribute subject_model.



24
25
26
# File 'lib/schemaker/models.rb', line 24

def subject_model
  @subject_model
end

Class Method Details

.model_typesObject



65
66
67
# File 'lib/schemaker/models.rb', line 65

def self.model_types
  [:object, :subject, :join]
end

Instance Method Details

#configureObject

configure each model in turn



47
48
49
50
51
52
# 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) ⇒ Class

retrieves a given Class ie. a type of model

Parameters:

  • which (Class, String, Symbol, BaseModel)

    class to get

Returns:

  • (Class)

    the Class (model) of interest



84
85
86
87
88
89
90
91
92
93
94
95
96
# 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) ⇒ Object

creates a key for a given type

Parameters:

  • type (Symbol)
    • either :object, :subject or :join



42
43
44
# File 'lib/schemaker/models.rb', line 42

def key type
  make_key get_class(type)
end

#logsObject



58
59
60
61
62
63
# 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) ⇒ Object

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

Parameters:

  • the (String)

    class name



101
102
103
104
# 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_joinObject



54
55
56
# File 'lib/schemaker/models.rb', line 54

def quick_join
  subject_model.quick_join
end