Class: Schemaker::Models

Inherits:
Object
  • Object
show all
Defined in:
lib/schemaker/models.rb

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



29
30
31
32
33
# File 'lib/schemaker/models.rb', line 29

def initialize subject_class, object_class, join_class, options = {}
  @subject_model  = SubjectModel.new self, subject_class, options[:subject_key]
  @object_model   = ObjectModel.new self, object_class
  @join_model     = JoinModel.new self, join_class
end

Instance Attribute Details

#join_modelObject

Returns the value of attribute join_model.



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

def join_model
  @join_model
end

#object_modelObject

Returns the value of attribute object_model.



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

def object_model
  @object_model
end

#subject_modelObject

Returns the value of attribute subject_model.



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

def subject_model
  @subject_model
end

Class Method Details

.model_typesObject



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

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

Instance Method Details

#configureObject

configure each model in turn



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

def configure
  [subject_model, object_model, join_model].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



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/schemaker/models.rb', line 74

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



37
38
39
# File 'lib/schemaker/models.rb', line 37

def key type
  make_key get_class(type)
end

#logsObject



48
49
50
51
52
53
# File 'lib/schemaker/models.rb', line 48

def logs
  @logs ||= [subject_model, object_model, join_model].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



91
92
93
94
# File 'lib/schemaker/models.rb', line 91

def make_key class_name
  name = class_name.to_s.pluralize.gsub(/::/, '__').underscore
  only_last_part_plural(name).to_sym
end