Class: Serel::Relation
- Inherits:
-
Object
- Object
- Serel::Relation
- Defined in:
- lib/serel/relation.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#qty ⇒ Object
readonly
Returns the value of attribute qty.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#all ⇒ Object
Finder methods.
-
#find(*ids) ⇒ Serel::Response
Finds an object by an ID or list of IDs.
-
#get ⇒ Object
Makes a request.
-
#initialize(type, qty) ⇒ Relation
constructor
A new instance of Relation.
-
#merge(relation) ⇒ Object
Public: Merges two relation objects together.
- #method_missing(sym, *attrs, &block) ⇒ Object
- #network ⇒ Object
- #new_relation ⇒ Object
-
#request ⇒ Object
Request stuff.
-
#scoping ⇒ Object
Scoping returns our internal scope defition.
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *attrs, &block) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/serel/relation.rb', line 38 def method_missing(sym, *attrs, &block) # If the base relation class responds to the method, call # it and merge in the resulting relation scope if @klass.respond_to?(sym) relation = @klass.send(sym, *attrs, &block) merge(relation) self end super(sym, *attrs, &block) end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
3 4 5 |
# File 'lib/serel/relation.rb', line 3 def klass @klass end |
#qty ⇒ Object (readonly)
Returns the value of attribute qty.
3 4 5 |
# File 'lib/serel/relation.rb', line 3 def qty @qty end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/serel/relation.rb', line 3 def type @type end |
Instance Method Details
#all ⇒ Object
Finder methods
86 87 88 89 90 91 92 |
# File 'lib/serel/relation.rb', line 86 def all if klass.respond_to?(:all) all_helper(1) else raise NoMethodError end end |
#find(*ids) ⇒ Serel::Response
101 102 103 104 105 106 107 108 |
# File 'lib/serel/relation.rb', line 101 def find(*ids) if klass.respond_to?(:find) arg = ids.length > 1 ? ids.join(';') : ids.pop url("#{@type}s/#{arg}").request else raise NoMethodError end end |
#get ⇒ Object
Makes a request. If the URL is already set we just call #request, else we set the URL to the plural of the type and make the request.
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/serel/relation.rb', line 112 def get if scoping[:url] request else # Best check that the generic page getter is enabled here. if klass.respond_to?(:get) url("#{@type}s").request else raise NoMethodError end end end |
#merge(relation) ⇒ Object
Public: Merges two relation objects together. This is used in our awesome
new scoping engine!
relation - A Serel::Relation object with the same base class as the
current relation
Returns self
22 23 24 25 26 27 |
# File 'lib/serel/relation.rb', line 22 def merge(relation) if relation.instance_variable_get(:@type) != @type raise ArgumentError, 'You cannot merge two relation objects based on different classes' end @scope.merge!(relation.scoping) end |
#network ⇒ Object
77 78 79 80 |
# File 'lib/serel/relation.rb', line 77 def network @network = true self end |
#new_relation ⇒ Object
34 35 36 |
# File 'lib/serel/relation.rb', line 34 def new_relation self end |
#request ⇒ Object
Request stuff
126 127 128 129 130 131 |
# File 'lib/serel/relation.rb', line 126 def request if (klass.network || @network) @scope[:network] = true end Serel::Request.new(@type, scoping, @qty).execute end |
#scoping ⇒ Object
Scoping returns our internal scope defition. Things like url etc.
30 31 32 |
# File 'lib/serel/relation.rb', line 30 def scoping @scope end |