Class: Ooor::Relation
- Inherits:
-
Object
- Object
- Ooor::Relation
- Defined in:
- lib/ooor/relation.rb
Overview
Similar to Active Record Relation
Instance Attribute Summary collapse
-
#count_field ⇒ Object
Returns the value of attribute count_field.
-
#create_with_value ⇒ Object
Returns the value of attribute create_with_value.
-
#eager_load_values ⇒ Object
Returns the value of attribute eager_load_values.
-
#from_value ⇒ Object
Returns the value of attribute from_value.
-
#group_values ⇒ Object
Returns the value of attribute group_values.
-
#having_values ⇒ Object
Returns the value of attribute having_values.
-
#includes_values ⇒ Object
Returns the value of attribute includes_values.
-
#joins_values ⇒ Object
Returns the value of attribute joins_values.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#limit_value ⇒ Object
Returns the value of attribute limit_value.
-
#loaded ⇒ Object
(also: #loaded?)
readonly
Returns the value of attribute loaded.
-
#lock_value ⇒ Object
Returns the value of attribute lock_value.
-
#offset_value ⇒ Object
Returns the value of attribute offset_value.
-
#options ⇒ Object
Returns the value of attribute options.
-
#order_values ⇒ Object
Returns the value of attribute order_values.
-
#page_value ⇒ Object
Returns the value of attribute page_value.
-
#per_value ⇒ Object
Returns the value of attribute per_value.
-
#preload_values ⇒ Object
Returns the value of attribute preload_values.
-
#readonly_value ⇒ Object
Returns the value of attribute readonly_value.
-
#reorder_flag ⇒ Object
Returns the value of attribute reorder_flag.
-
#select_values ⇒ Object
Returns the value of attribute select_values.
-
#where_values ⇒ Object
Returns the value of attribute where_values.
Instance Method Summary collapse
-
#all(*args) ⇒ Object
A convenience wrapper for
find(:all, *args)
. - #apply_finder_options(options) ⇒ Object
-
#build_where(opts, other = []) ⇒ Object
TODO OpenERP domain is more than just the intersection of restrictions.
- #count(column_name = nil, options = {}) ⇒ Object
- #eager_loading? ⇒ Boolean
-
#initialize(klass, options = {}) ⇒ Relation
constructor
A new instance of Relation.
- #initialize_copy(other) ⇒ Object
-
#limit(value) ⇒ Object
def having(*args) relation = clone relation.having_values += build_where(*args) unless args.blank? relation end.
- #new(*args, &block) ⇒ Object
- #offset(value) ⇒ Object
- #order(*args) ⇒ Object
- #reload ⇒ Object
- #reset ⇒ Object
- #to_a ⇒ Object
- #where(opts, *rest) ⇒ Object
Constructor Details
#initialize(klass, options = {}) ⇒ Relation
Returns a new instance of Relation.
66 67 68 69 70 71 72 73 74 |
# File 'lib/ooor/relation.rb', line 66 def initialize(klass, ={}) @klass = klass @where_values = [] @loaded = false @options = @count_field = false @offset_value = 0 @order_values = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (protected)
161 162 163 164 165 166 167 168 169 |
# File 'lib/ooor/relation.rb', line 161 def method_missing(method, *args, &block) if Array.method_defined?(method) to_a.send(method, *args, &block) elsif @klass.respond_to?(method) @klass.send(method, *args, &block) else @klass.rpc_execute(method.to_s, to_a.map {|record| record.id}, *args) end end |
Instance Attribute Details
#count_field ⇒ Object
Returns the value of attribute count_field.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def count_field @count_field end |
#create_with_value ⇒ Object
Returns the value of attribute create_with_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def create_with_value @create_with_value end |
#eager_load_values ⇒ Object
Returns the value of attribute eager_load_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def eager_load_values @eager_load_values end |
#from_value ⇒ Object
Returns the value of attribute from_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def from_value @from_value end |
#group_values ⇒ Object
Returns the value of attribute group_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def group_values @group_values end |
#having_values ⇒ Object
Returns the value of attribute having_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def having_values @having_values end |
#includes_values ⇒ Object
Returns the value of attribute includes_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def includes_values @includes_values end |
#joins_values ⇒ Object
Returns the value of attribute joins_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def joins_values @joins_values end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def klass @klass end |
#limit_value ⇒ Object
Returns the value of attribute limit_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def limit_value @limit_value end |
#loaded ⇒ Object (readonly) Also known as: loaded?
Returns the value of attribute loaded.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def loaded @loaded end |
#lock_value ⇒ Object
Returns the value of attribute lock_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def lock_value @lock_value end |
#offset_value ⇒ Object
Returns the value of attribute offset_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def offset_value @offset_value end |
#options ⇒ Object
Returns the value of attribute options.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def @options end |
#order_values ⇒ Object
Returns the value of attribute order_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def order_values @order_values end |
#page_value ⇒ Object
Returns the value of attribute page_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def page_value @page_value end |
#per_value ⇒ Object
Returns the value of attribute per_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def per_value @per_value end |
#preload_values ⇒ Object
Returns the value of attribute preload_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def preload_values @preload_values end |
#readonly_value ⇒ Object
Returns the value of attribute readonly_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def readonly_value @readonly_value end |
#reorder_flag ⇒ Object
Returns the value of attribute reorder_flag.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def reorder_flag @reorder_flag end |
#select_values ⇒ Object
Returns the value of attribute select_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def select_values @select_values end |
#where_values ⇒ Object
Returns the value of attribute where_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def where_values @where_values end |
Instance Method Details
#all(*args) ⇒ Object
A convenience wrapper for find(:all, *args)
. You can pass in all the same arguments to this method as you can to find(:all)
.
114 115 116 |
# File 'lib/ooor/relation.rb', line 114 def all(*args) args.any? ? (args.first).to_a : to_a end |
#apply_finder_options(options) ⇒ Object
98 99 100 101 102 |
# File 'lib/ooor/relation.rb', line 98 def () relation = clone relation. = #TODO this may be too simplified for chainability, merge smartly instead? relation end |
#build_where(opts, other = []) ⇒ Object
TODO OpenERP domain is more than just the intersection of restrictions
18 19 20 21 22 23 24 25 |
# File 'lib/ooor/relation.rb', line 18 def build_where(opts, other = [])#TODO OpenERP domain is more than just the intersection of restrictions case opts when Array || '|' || '&' [opts] when Hash opts.keys.map {|key|["#{key}", "=", opts[key]]} end end |
#count(column_name = nil, options = {}) ⇒ Object
61 62 63 64 |
# File 'lib/ooor/relation.rb', line 61 def count(column_name = nil, = {}) column_name, = nil, column_name if column_name.is_a?(Hash) calculate(:count, column_name, ) end |
#eager_loading? ⇒ Boolean
155 156 157 |
# File 'lib/ooor/relation.rb', line 155 def eager_loading? false end |
#initialize_copy(other) ⇒ Object
87 88 89 |
# File 'lib/ooor/relation.rb', line 87 def initialize_copy(other) reset end |
#limit(value) ⇒ Object
def having(*args)
relation = clone
relation.having_values += build_where(*args) unless args.blank?
relation
end
43 44 45 46 47 |
# File 'lib/ooor/relation.rb', line 43 def limit(value) relation = clone relation.limit_value = value relation end |
#new(*args, &block) ⇒ Object
76 77 78 79 |
# File 'lib/ooor/relation.rb', line 76 def new(*args, &block) #TODO inject current domain in *args @klass.new(*args, &block) end |
#offset(value) ⇒ Object
49 50 51 52 53 |
# File 'lib/ooor/relation.rb', line 49 def offset(value) relation = clone relation.offset_value = value relation end |
#order(*args) ⇒ Object
55 56 57 58 59 |
# File 'lib/ooor/relation.rb', line 55 def order(*args) relation = clone relation.order_values += args.flatten unless args.blank? relation end |
#reload ⇒ Object
81 82 83 84 85 |
# File 'lib/ooor/relation.rb', line 81 def reload reset to_a # force reload self end |
#reset ⇒ Object
91 92 93 94 95 96 |
# File 'lib/ooor/relation.rb', line 91 def reset @first = @last = @to_sql = @order_clause = @scope_for_create = @arel = @loaded = nil @should_eager_load = @join_dependency = nil @records = [] self end |
#to_a ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/ooor/relation.rb', line 118 def to_a if loaded? @records else if @order_values.empty? search_order = false else search_order = @order_values.join(", ") end if @options && @options[:name_search] name_search = @klass.name_search(@options[:name_search], where_values, 'ilike', @options[:context], @limit_value) @records = name_search.map do |tuple| r = @klass.new({name: tuple[1]}, []) r.id = tuple[0] r #TODO load the fields optionally end else if @per_value && @page_value offset = @per_value * @page_value limit = @per_value else offset = @offset_value limit = @limit_value || false end @loaded = true opts = @options.merge({ domain: where_values, offset: offset, limit: limit, order: search_order, }) @records = @klass.find(:all, opts) end end end |
#where(opts, *rest) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/ooor/relation.rb', line 27 def where(opts, *rest) relation = clone if opts.is_a?(Array) && opts.any? {|e| e.is_a? Array} relation.where_values = opts else relation.where_values += build_where(opts, rest) unless opts.blank? end relation end |