Module: Arcade::Support::Array

Included in:
Array
Defined in:
lib/support/conversions.rb

Instance Method Summary collapse

Instance Method Details

#allocate_model(autoload = false) ⇒ Object

convert query results into Arcade::Base-objects

handles  [query: => [{ result }, {result} ], too


73
74
75
76
77
78
79
80
# File 'lib/support/conversions.rb', line 73

def allocate_model autoload=false
  if size==1 && first.is_a?( Hash ) && !first.keys.include?( :@type )
      # Load only the associated record, not the entire structure
      first.values.flatten.map{ |x| x.allocate_model(false) }
  else
    map{ |x| _allocate_model x, autoload }
  end
end

#analyseObject

used to enable def abc *key where key is a Range, an comma separated List or an item aimed to support #compose_where



38
39
40
41
42
43
44
45
46
# File 'lib/support/conversions.rb', line 38

def analyse # :nodoc:
  if first.is_a?(Range)
    first
  elsif size ==1
    first
  else
    self
  end
end

#arcade_flattenObject



48
49
50
51
52
53
54
# File 'lib/support/conversions.rb', line 48

def arcade_flatten
  while( first.is_a?(Array) )
    self.flatten!(1)
  end
  self.compact!
  self ## return object
end

#from_dbObject



17
18
19
# File 'lib/support/conversions.rb', line 17

def from_db
  map &:from_db
end

#select_result(condition = nil) ⇒ Object

chainable model-extaction method

Used to get the results of a query where a projection is specified q = DB.query(“ select <projection> from …”) –> [<projection> => [ { result (hash) } , … ]] q.select_result( <projection> ) –> An array or plain results or

-->  an array of Arcade::Base-objects


65
66
67
68
# File 'lib/support/conversions.rb', line 65

def select_result condition=nil
  condition = first.keys.first if condition.nil?
  map{|x| x[condition.to_sym]}.flatten.allocate_model
end

#to_dbObject

Class extentions to manage to_db and from_db



5
6
7
8
9
10
11
# File 'lib/support/conversions.rb', line 5

def to_db
  if all?{ |x| x.respond_to?(:rid?)}  && any?( &:rid? )
    "["+ map{|x| x.rid? ? x.rid : x.to_or }.join(', ') + ']'
  else
    map(&:to_db) # .join(',')
  end
end

#to_htmlObject



25
26
27
28
29
30
31
32
# File 'lib/support/conversions.rb', line 25

def to_html
  # all elements are treated equally
   if first.is_a? Arcade::Base
     IRuby::Table map(&:invariant_attributes)
   else
     IRuby.display IRuby.html "<p> #{map(&:to_human).join("<br>")}</p>"
   end
end

#to_humanObject



21
22
23
# File 'lib/support/conversions.rb', line 21

def to_human
  map &:to_human
end

#to_orObject



13
14
15
# File 'lib/support/conversions.rb', line 13

def to_or
  "["+ map( &:to_or).join(', ')+"]"
end