Module: DbAgile::IO::TypeSafe

Included in:
CSV, JSON, YAML
Defined in:
lib/dbagile/io/type_safe.rb

Instance Method Summary collapse

Instance Method Details

#from_typesafe_relation(type_system, relation) ⇒ Object

Decodes an type-safe encoded relation



40
41
42
43
# File 'lib/dbagile/io/type_safe.rb', line 40

def from_typesafe_relation(type_system, relation)
  return relation if type_system.nil?
  relation.collect{|t| from_typesafe_tuple(type_system, t)}
end

#from_typesafe_tuple(type_system, tuple) ⇒ Object

Decodes a type-safe tuple to a tuple



29
30
31
32
33
34
35
36
37
# File 'lib/dbagile/io/type_safe.rb', line 29

def from_typesafe_tuple(type_system, tuple)
  return tuple if type_system.nil?
  copy = {}
  tuple.each_pair{|k,v| 
    k = k.to_sym unless k.kind_of?(Symbol)
    copy[k] = type_system.parse_literal(v)
  }
  copy
end

#from_typesafe_xxx(data, options, &block) ⇒ Object

Implements the from_xxx emitter spec



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dbagile/io/type_safe.rb', line 46

def from_typesafe_xxx(data, options, &block)
  if block.nil?
    from_typesafe_relation(options[:type_system], data)
  else
    if data.respond_to?(:each)
      ts = options[:type_system]
      data.each do |d|
        raise DbAgile::InvalidFormatError, "Loaded tuple should be an hash (#{d.inspect})" unless d.kind_of?(Hash)
        yield(from_typesafe_tuple(ts, d))
      end
    else 
      raise DbAgile::InvalidFormatError, "Loaded data should be an array of tuples (#{data.inspect})"
    end
    nil
  end
end

#to_typesafe_relation(type_system, relation) ⇒ Object

Encodes a tuple to a type safe tuple



23
24
25
26
# File 'lib/dbagile/io/type_safe.rb', line 23

def to_typesafe_relation(type_system, relation)
  return relation if type_system.nil?
  relation.collect{|t| to_typesafe_tuple(type_system, t)}
end

#to_typesafe_tuple(type_system, tuple) ⇒ Object

Encodes a tuple to a type safe tuple



6
7
8
9
10
11
# File 'lib/dbagile/io/type_safe.rb', line 6

def to_typesafe_tuple(type_system, tuple)
  return tuple if type_system.nil?
  copy = {}
  tuple.each_pair{|k,v| copy[k] = type_system.to_literal(v)}
  copy
end

#with_type_safe_relation(data, options, &block) ⇒ Object

Yields the block with each tuple in turn



14
15
16
17
18
19
20
# File 'lib/dbagile/io/type_safe.rb', line 14

def with_type_safe_relation(data, options, &block)
  if ts = options[:type_system]
    data.each{|t| block.call(to_typesafe_tuple(ts, t))}
  else
    data.each(&block)
  end
end