Class: Schemaker::Models
- Inherits:
-
Object
- Object
- Schemaker::Models
- 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
-
#join_model ⇒ Object
Returns the value of attribute join_model.
-
#object_model ⇒ Object
Returns the value of attribute object_model.
-
#subject_model ⇒ Object
Returns the value of attribute subject_model.
Class Method Summary collapse
Instance Method Summary collapse
-
#configure ⇒ Object
configure each model in turn.
-
#get_class(type) ⇒ Class
retrieves a given Class ie.
-
#initialize(subject_class, object_class, join_class, options = {}) ⇒ Models
constructor
Sets up the models that take part in the model relationship to be configured.
-
#key(type) ⇒ Object
creates a key for a given type.
- #logs ⇒ Object
-
#make_key(class_name) ⇒ Object
creates a key from a class name fx UsersRoles becomes :user_roles, where only the last part is pluralised!.
- #quick_join ⇒ Object
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
31 32 33 34 35 36 37 38 |
# File 'lib/schemaker/models.rb', line 31 def initialize subject_class, object_class, join_class, = {} 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, [:subject_key] @object_model = ObjectModel.new self, object_class @join_model = JoinModel.new(self, join_class) if join_class end |
Instance Attribute Details
#join_model ⇒ Object
Returns the value of attribute join_model.
24 25 26 |
# File 'lib/schemaker/models.rb', line 24 def join_model @join_model end |
#object_model ⇒ Object
Returns the value of attribute object_model.
24 25 26 |
# File 'lib/schemaker/models.rb', line 24 def object_model @object_model end |
#subject_model ⇒ Object
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_types ⇒ Object
65 66 67 |
# File 'lib/schemaker/models.rb', line 65 def self.model_types [:object, :subject, :join] end |
Instance Method Details
#configure ⇒ Object
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
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
42 43 44 |
# File 'lib/schemaker/models.rb', line 42 def key type make_key get_class(type) end |
#logs ⇒ Object
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!
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_join ⇒ Object
54 55 56 |
# File 'lib/schemaker/models.rb', line 54 def quick_join subject_model.quick_join end |