Class: UzuUzu::Wrapper::Dm

Inherits:
Object show all
Defined in:
lib/uzuuzu-core/wrapper/dm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = nil) ⇒ Dm

Returns a new instance of Dm.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/uzuuzu-core/wrapper/dm.rb', line 15

def initialize(env=nil)
  if env.nil?
  return
  end
  @env = env.clone

  unless @env['logger'].nil?
    DataMapper::logger = ::UzuUzu::Wrapper::Logger.new(@env['logger']).logger
  end
  @repositories = []
  @env.each do |key, value|
    @repositories.push key.to_sym
    if value['adapter']
      @adapter = value['adapter']
      DataMapper.setup(key.to_sym, value)
      if value['logger']
        case(value['adapter'])
        when 'sqlite3'
          DataObjects::Sqlite3.logger = ::UzuUzu::Wrapper::Logger.new(value['logger']).logger
        when 'postgres'
          DataObjects::Postgres.logger = ::UzuUzu::Wrapper::Logger.new(value['logger']).logger
        when 'mysql'
          DataObjects::MySql.logger = ::UzuUzu::Wrapper::Logger.new(value['logger']).logger
        end
      end
    end
  end # env.each
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



8
9
10
# File 'lib/uzuuzu-core/wrapper/dm.rb', line 8

def adapter
  @adapter
end

#repositoriesObject (readonly)

Returns the value of attribute repositories.



10
11
12
# File 'lib/uzuuzu-core/wrapper/dm.rb', line 10

def repositories
  @repositories
end

Instance Method Details

#import_csv(model, csv, overwrite = false) ⇒ Object

import_xml



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/uzuuzu-core/wrapper/dm.rb', line 123

def import_csv(model, csv, overwrite=false)
  resource = model.first(:id => csv[0])
  csv.each_with_index do |culumn, index|
    if resource.nil? || overwrite
      resource ||= model.new
      resource.properties.each_with_index do |property, column_no|
        resource[property.name] = culumn if index = column_no
      end
      resource.save
    end
  end
end

#import_file(model, file, overwrite = false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/uzuuzu-core/wrapper/dm.rb', line 47

def import_file(model, file, overwrite=false)
  unless ::File.file?(file)
    raise "UzuUzu fixture import error file not found : #{file}"
  end
  ext = ::File.extname(file)
  case(ext)
  when '.yml', '.yaml'
    import_yaml(model, ::YAML.load_file(file), overwrite)
  when '.xml'
    # TODO
  when '.json', '.js'
    # TODO
  when '.csv'
    begin
      require 'fastercsv'
      ::FasterCSV.foreach(file) do |row|
        import_csv(model, row, overwrite)
      end
    rescue
      require "csv"
      ::CSV.open(file) do |row|
        import_csv(model, row, overwrite)
      end
    end
  end
  nil
end

#import_json(model, json, overwrite = false) ⇒ Object

import_yaml



115
116
117
# File 'lib/uzuuzu-core/wrapper/dm.rb', line 115

def import_json(model, json, overwrite=false)
  # TODO
end

#import_xml(model, xml, overwrite = false) ⇒ Object

import_json



119
120
121
# File 'lib/uzuuzu-core/wrapper/dm.rb', line 119

def import_xml(model, xml, overwrite=false)
  # TODO
end

#import_yaml(model, yaml, overwrite = false) ⇒ Object

import_file



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/uzuuzu-core/wrapper/dm.rb', line 75

def import_yaml(model, yaml, overwrite=false)
  ::UzuUzu.logger.debug 'import yaml'
  if yaml.kind_of?(Array)
    yaml.each do |map|
      ::UzuUzu.logger.debug "import #{map.inspect}"
      resource = model.first(:id => map[:id]) unless map[:id].nil?
      if resource.nil?
        resource = model.new(map)
        unless resource.valid?
          ::UzuUzu.logger.error resource.errors
        end
        resource.save
      elsif overwrite
        resource.update(map)
        unless resource.valid?
          ::UzuUzu.logger.error resource.errors
        end
      end
    end
  elsif yaml.kind_of?(Hash)
    ::UzuUzu.logger.debug "import #{yaml.inspect}"
    resource = model.first(:id => yaml[:id]) unless yaml[:id].nil?
    if resource.nil?
      resource = model.new(yaml)
      unless resource.valid?
        ::UzuUzu.logger.error resource.errors
      end
      resource.save
    elsif overwrite
      resource.update(yaml)
      unless resource.valid?
        ::UzuUzu.logger.error resource.errors
      end
    end
  else
    ::UzuUzu.logger.error "import type miss match #{yaml.inspect}"
  end
  ::UzuUzu.logger.debug 'imported yaml'
end