Class: Schemaker::Models
- Inherits:
-
Object
- Object
- Schemaker::Models
- Defined in:
- lib/schemaker/models.rb
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!.
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
29 30 31 32 33 |
# File 'lib/schemaker/models.rb', line 29 def initialize subject_class, object_class, join_class, = {} @subject_model = SubjectModel.new self, subject_class, [:subject_key] @object_model = ObjectModel.new self, object_class @join_model = JoinModel.new self, join_class end |
Instance Attribute Details
#join_model ⇒ Object
Returns the value of attribute join_model.
22 23 24 |
# File 'lib/schemaker/models.rb', line 22 def join_model @join_model end |
#object_model ⇒ Object
Returns the value of attribute object_model.
22 23 24 |
# File 'lib/schemaker/models.rb', line 22 def object_model @object_model end |
#subject_model ⇒ Object
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_types ⇒ Object
55 56 57 |
# File 'lib/schemaker/models.rb', line 55 def self.model_types [:object, :subject, :join] end |
Instance Method Details
#configure ⇒ Object
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
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
37 38 39 |
# File 'lib/schemaker/models.rb', line 37 def key type make_key get_class(type) end |
#logs ⇒ Object
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!
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 |