Module: VirtualBox::AbstractModel::Relatable::ClassMethods
- Defined in:
- lib/virtualbox/abstract_model/relatable.rb
Instance Method Summary collapse
-
#has_relationship?(name) ⇒ Boolean
Returns a boolean of whether a relationship exists.
-
#inherited(subclass) ⇒ Object
Used to propagate relationships to subclasses.
-
#relationship(name, klass, options = {}) ⇒ Object
Define a relationship.
-
#relationships ⇒ Array
Returns an array of the relationships in order of being added.
-
#relationships_hash ⇒ Hash
Returns a hash of all the relationships.
Instance Method Details
#has_relationship?(name) ⇒ Boolean
Returns a boolean of whether a relationship exists.
169 170 171 |
# File 'lib/virtualbox/abstract_model/relatable.rb', line 169 def has_relationship?(name) !!relationships.detect { |r| r[0] == name } end |
#inherited(subclass) ⇒ Object
Used to propagate relationships to subclasses. This method makes sure that subclasses of a class with VirtualBox::AbstractModel::Relatable included will inherit the relationships as well, which would be the expected behaviour.
176 177 178 179 180 181 182 |
# File 'lib/virtualbox/abstract_model/relatable.rb', line 176 def inherited(subclass) super rescue NoMethodError relationships.each do |name, | subclass.relationship(name, nil, ) end end |
#relationship(name, klass, options = {}) ⇒ Object
Define a relationship. The name and class must be specified. This class will be used to call the ‘populate_relationship, `save_relationship`, etc. methods.
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/virtualbox/abstract_model/relatable.rb', line 140 def relationship(name, klass, = {}) name = name.to_sym relationships << [name, { :klass => klass }.merge()] # Define the method to read the relationship define_method(name) { read_relationship(name) } # Define the method to set the relationship define_method("#{name}=") { |*args| set_relationship(name, *args) } end |
#relationships ⇒ Array
Returns an array of the relationships in order of being added.
162 163 164 |
# File 'lib/virtualbox/abstract_model/relatable.rb', line 162 def relationships @relationships ||= [] end |
#relationships_hash ⇒ Hash
Returns a hash of all the relationships.
155 156 157 |
# File 'lib/virtualbox/abstract_model/relatable.rb', line 155 def relationships_hash Hash[*relationships.flatten] end |