Class: Ooor::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/ooor/relation.rb

Overview

Similar to Active Record Relation

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  @klass = klass
  @where_values = []
  @loaded = false
  @options = 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_fieldObject

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_valueObject

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_valuesObject

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_valueObject

Returns the value of attribute from_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def from_value
  @from_value
end

#group_valuesObject

Returns the value of attribute group_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def group_values
  @group_values
end

#having_valuesObject

Returns the value of attribute having_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def having_values
  @having_values
end

#includes_valuesObject

Returns the value of attribute includes_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def includes_values
  @includes_values
end

#joins_valuesObject

Returns the value of attribute joins_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def joins_values
  @joins_values
end

#klassObject (readonly)

Returns the value of attribute klass.



12
13
14
# File 'lib/ooor/relation.rb', line 12

def klass
  @klass
end

#limit_valueObject

Returns the value of attribute limit_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def limit_value
  @limit_value
end

#loadedObject (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_valueObject

Returns the value of attribute lock_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def lock_value
  @lock_value
end

#offset_valueObject

Returns the value of attribute offset_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def offset_value
  @offset_value
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def options
  @options
end

#order_valuesObject

Returns the value of attribute order_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def order_values
  @order_values
end

#page_valueObject

Returns the value of attribute page_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def page_value
  @page_value
end

#per_valueObject

Returns the value of attribute per_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def per_value
  @per_value
end

#preload_valuesObject

Returns the value of attribute preload_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def preload_values
  @preload_values
end

#readonly_valueObject

Returns the value of attribute readonly_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def readonly_value
  @readonly_value
end

#reorder_flagObject

Returns the value of attribute reorder_flag.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def reorder_flag
  @reorder_flag
end

#select_valuesObject

Returns the value of attribute select_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def select_values
  @select_values
end

#where_valuesObject

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? ? apply_finder_options(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 apply_finder_options(options)
  relation = clone
  relation.options = options #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, options = {})
  column_name, options = nil, column_name if column_name.is_a?(Hash)
  calculate(:count, column_name, options)
end

#eager_loading?Boolean

Returns:

  • (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

#reloadObject



81
82
83
84
85
# File 'lib/ooor/relation.rb', line 81

def reload
  reset
  to_a # force reload
  self
end

#resetObject



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_aObject



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