Module: DbAgile::Tools::Tuple

Included in:
Command::Bulk::Import, Plugin, Restful::Client, Restful::Middleware::OneDatabase
Defined in:
lib/dbagile/tools/tuple.rb

Instance Method Summary collapse

Instance Method Details

#check_tuple_heading(heading, environment) ⇒ Object

Checks a tuple heading, displaying a warning message on the environment if something goes bad…



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dbagile/tools/tuple.rb', line 14

def check_tuple_heading(heading, environment)
  heading.each_pair{|k,v|
    if NilClass == v
      environment.say("WARNING: NilClass in heading (type inference failure), using String")
      heading[k] = String
    elsif !DbAgile::RECOGNIZED_DOMAINS.include?(v)
      environment.say("WARNING: Unrecognized domain #{v} in heading, using String")
      heading[k] = String
    end
  }
  heading
end

#tuple_has_key?(tuple, keys) ⇒ Boolean

Checks if a given tuple contains value for at least one key.

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/dbagile/tools/tuple.rb', line 36

def tuple_has_key?(tuple, keys)
  return false if keys.nil?
  !keys.find{|k| k.all?{|a| !tuple[a].nil? }}.nil?
end

#tuple_heading(tuple) ⇒ Object

Returns the heading of a given tuple



6
7
8
9
10
# File 'lib/dbagile/tools/tuple.rb', line 6

def tuple_heading(tuple)
  heading = {}
  tuple.each_pair{|name, value| heading[name] = value.class}
  heading
end

#tuple_key(tuple, keys) ⇒ Object

Extract the key/value pairs that form a key on a tuple, given keys information. Returns tuple if no such better tuple can be found.



43
44
45
46
47
# File 'lib/dbagile/tools/tuple.rb', line 43

def tuple_key(tuple, keys)
  return tuple if keys.nil?
  key = keys.find{|k| k.all?{|a| !tuple[a].nil? }}
  key.nil? ? tuple : tuple_project(tuple, key)
end

#tuple_project(tuple, columns) ⇒ Object

Projects a tuple over some columns



28
29
30
31
32
# File 'lib/dbagile/tools/tuple.rb', line 28

def tuple_project(tuple, columns)
  proj = {}
  columns.collect{|col| proj[col] = tuple[col]}
  proj
end

#tuple_to_querystring(tuple) ⇒ Object

Converts a tuple to a query string



50
51
52
# File 'lib/dbagile/tools/tuple.rb', line 50

def tuple_to_querystring(tuple)
  tuple.collect{|k,v| "#{CGI::escape(k.to_s)}=#{CGI::escape(v.to_s)}"}.reverse.join('&')
end