fidius-common 0.0.3 → 0.0.4beta0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -4,3 +4,4 @@ Gemfile.lock
4
4
  pkg/*
5
5
  doc/
6
6
  .yardoc/
7
+ coverage/
data/CREDITS.md CHANGED
@@ -3,3 +3,12 @@ Third-party tools
3
3
  * `lib/fidius-common/ip-helper.rb`: `get_my_ip` methods
4
4
  * Credit goes to Coderr
5
5
  * https://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
6
+
7
+ * `lib/fidius-common/yamldb/serialization_helper.rb` and
8
+ `lib/fidius-common/yamldb/yaml_db.rb`
9
+ * Copied and modified from yamldb-gem
10
+ * Credits:
11
+ * Created by Orion Henry and Adam Wiggins.
12
+ * Major updates by Ricardo Chimal, Jr.
13
+ * Patches contributed by Michael Irwin, Tom Locke, and Tim Galeckas.
14
+ * https://github.com/ludicast/yaml_db
data/Rakefile CHANGED
@@ -20,7 +20,9 @@ begin
20
20
  '--private', # include private methods
21
21
  '--protected', # include protected methods
22
22
  '--files', static_files,
23
- '--readme', 'README.md'
23
+ '--readme', 'README.md',
24
+ '--exclude', 'lib/fidius-common/yamldb/serialization_helper.rb',
25
+ '--exclude', 'lib/fidius-common/yamldb/yaml_db.rb'
24
26
  ]
25
27
  end
26
28
  rescue LoadError
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
-
21
+ s.add_development_dependency 'simplecov', '>= 0.4.0'
22
22
  s.add_runtime_dependency 'activesupport', '>= 3.0.0'
23
23
 
24
24
  s.rdoc_options << '--title' << s.name <<
@@ -2,7 +2,7 @@ module FIDIUS
2
2
  module Common
3
3
 
4
4
  # Current version number
5
- VERSION = "0.0.3"
5
+ VERSION = "0.0.4beta0"
6
6
 
7
7
  end # module Common
8
8
  end # module FIDIUS
@@ -0,0 +1,255 @@
1
+ # Copied and modified from yamldb gem (https://github.com/ludicast/yaml_db)
2
+ #
3
+ # Credits:
4
+ # Created by Orion Henry and Adam Wiggins.
5
+ # Major updates by Ricardo Chimal, Jr.
6
+ # Patches contributed by Michael Irwin, Tom Locke, and Tim Galeckas.
7
+ #
8
+ #This module deals with the serialization of the database schema and
9
+ #tables. It was extended by the establishment of the ActiveRecord
10
+ #connection to use the module without a Rails environment.
11
+ #
12
+ # dump_schema and load-schema are modified ActiveRecord rake-tasks.
13
+
14
+ module SerializationHelper
15
+
16
+ class Base
17
+ attr_reader :extension
18
+
19
+ def initialize(helper, config_filename, db_entry)
20
+ @dumper = helper.dumper
21
+ @loader = helper.loader
22
+ @extension = helper.extension
23
+
24
+ establish_connection(config_filename, db_entry)
25
+ end
26
+
27
+ # Set configuration for the ActiveRecord connection.
28
+ #
29
+ # @param [String] path to yaml configuration file
30
+ # @param [String] name of the db entry in the configuration file
31
+ def establish_connection(yml_file, db_entry)
32
+ if yml_file.class == String
33
+ raise "#{yml_file} does not exist" unless File.exists? File.expand_path(yml_file)
34
+ yml_config = YAML.load(File.read(yml_file))
35
+ db_config = yml_config[db_entry]
36
+ elsif yml_file.class == Hash
37
+ # also react on connection settings given as hash
38
+ db_config = yml_file
39
+ else
40
+ raise "please input string or hash"
41
+ end
42
+ unless db_config
43
+ raise "No entry '#{db_entry}' found in #{yml_file}"
44
+ else
45
+ @db_entry = db_entry
46
+ ActiveRecord::Base.establish_connection db_config
47
+ ActiveRecord::Base.connection
48
+ end
49
+ end
50
+
51
+ # copied and modified activerecord-3.0.6/lib/active_record/railties/database.rake
52
+ def dump_schema
53
+ require 'active_record/schema_dumper'
54
+ File.open(File.join(@data_dir ,'schema.rb'), "w") do |file|
55
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
56
+ end
57
+ end
58
+
59
+ def load_schema
60
+ file = File.join(@data_dir ,'schema.rb')
61
+ if File.exists?(file)
62
+ ActiveSupport::Dependencies::Loadable.load(file)
63
+ else
64
+ puts "#{file} does not exist"
65
+ end
66
+ end
67
+
68
+ # Added creation of directories with timestamps.
69
+ # @param [String] path to target dir
70
+ # @param [Boolean] true, if timestamp should be added
71
+ def dump(target_dir, timestamp)
72
+ unless target_dir.empty? and !Dir.exists?(target_dir)
73
+ target_dir += '/'
74
+ end
75
+ dir = target_dir + @db_entry
76
+ if timestamp
77
+ timestamp = Time.now
78
+ dir += "_#{timestamp.strftime("%Y-%m-%d_%H%M%S")}"
79
+ end
80
+
81
+ Dir.mkdir dir
82
+ @data_dir = File.expand_path(dir)
83
+ @data_filename = "#{@data_dir}/#{@db_entry}.#{@extension}"
84
+
85
+ disable_logger
86
+ dump_schema
87
+ @dumper.dump(File.new(@data_filename, "w"))
88
+ reenable_logger
89
+ @data_dir
90
+ end
91
+
92
+ def load(import_dir ,truncate = true)
93
+ if Dir.exists? import_dir
94
+ @data_dir = import_dir
95
+ disable_logger
96
+ load_schema
97
+ @loader.load(File.new("#{@data_dir}/#{@db_entry}.#{@extension}", "r"), truncate)
98
+ reenable_logger
99
+ else
100
+ puts "#{import_dir} does not exist!"
101
+ end
102
+ end
103
+
104
+ def disable_logger
105
+ @@old_logger = ActiveRecord::Base.logger
106
+ ActiveRecord::Base.logger = nil
107
+ end
108
+
109
+ def reenable_logger
110
+ ActiveRecord::Base.logger = @@old_logger
111
+ end
112
+ end
113
+
114
+ class Load
115
+ def self.load(io, truncate = true)
116
+ ActiveRecord::Base.connection.transaction do
117
+ load_documents(io, truncate)
118
+ end
119
+ end
120
+
121
+ def self.truncate_table(table)
122
+ begin
123
+ ActiveRecord::Base.connection.execute("TRUNCATE #{SerializationHelper::Utils.quote_table(table)}")
124
+ rescue Exception
125
+ ActiveRecord::Base.connection.execute("DELETE FROM #{SerializationHelper::Utils.quote_table(table)}")
126
+ end
127
+ end
128
+
129
+ def self.load_table(table, data, truncate = true)
130
+ column_names = data['columns']
131
+ if truncate
132
+ truncate_table(table)
133
+ end
134
+ load_records(table, column_names, data['records'])
135
+ reset_pk_sequence!(table)
136
+ end
137
+
138
+ def self.load_records(table, column_names, records)
139
+ if column_names.nil?
140
+ return
141
+ end
142
+ columns = column_names.map{|cn| ActiveRecord::Base.connection.columns(table).detect{|c| c.name == cn}}
143
+ quoted_column_names = column_names.map { |column| ActiveRecord::Base.connection.quote_column_name(column) }.join(',')
144
+ quoted_table_name = SerializationHelper::Utils.quote_table(table)
145
+ records.each do |record|
146
+ quoted_values = record.zip(columns).map{|c| ActiveRecord::Base.connection.quote(c.first, c.last)}.join(',')
147
+ ActiveRecord::Base.connection.execute("INSERT INTO #{quoted_table_name} (#{quoted_column_names}) VALUES (#{quoted_values})")
148
+ end
149
+ end
150
+
151
+ def self.reset_pk_sequence!(table_name)
152
+ if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!)
153
+ ActiveRecord::Base.connection.reset_pk_sequence!(table_name)
154
+ end
155
+ end
156
+
157
+
158
+ end
159
+
160
+ module Utils
161
+
162
+ def self.unhash(hash, keys)
163
+ keys.map { |key| hash[key] }
164
+ end
165
+
166
+ def self.unhash_records(records, keys)
167
+ records.each_with_index do |record, index|
168
+ records[index] = unhash(record, keys)
169
+ end
170
+
171
+ records
172
+ end
173
+
174
+ def self.convert_booleans(records, columns)
175
+ records.each do |record|
176
+ columns.each do |column|
177
+ next if is_boolean(record[column])
178
+ record[column] = (record[column] == 't' or record[column] == '1')
179
+ end
180
+ end
181
+ records
182
+ end
183
+
184
+ def self.boolean_columns(table)
185
+ columns = ActiveRecord::Base.connection.columns(table).reject { |c| silence_warnings { c.type != :boolean } }
186
+ columns.map { |c| c.name }
187
+ end
188
+
189
+ def self.is_boolean(value)
190
+ value.kind_of?(TrueClass) or value.kind_of?(FalseClass)
191
+ end
192
+
193
+ def self.quote_table(table)
194
+ ActiveRecord::Base.connection.quote_table_name(table)
195
+ end
196
+
197
+ end
198
+
199
+ class Dump
200
+ def self.before_table(io, table)
201
+
202
+ end
203
+
204
+ def self.dump(io)
205
+ tables.each do |table|
206
+ before_table(io, table)
207
+ dump_table(io, table)
208
+ after_table(io, table)
209
+ end
210
+ end
211
+
212
+ def self.after_table(io, table)
213
+
214
+ end
215
+
216
+ def self.tables
217
+ ActiveRecord::Base.connection.tables.reject { |table| ['schema_info', 'schema_migrations'].include?(table) }
218
+ end
219
+
220
+ def self.dump_table(io, table)
221
+ return if table_record_count(table).zero?
222
+
223
+ dump_table_columns(io, table)
224
+ dump_table_records(io, table)
225
+ end
226
+
227
+ def self.table_column_names(table)
228
+ ActiveRecord::Base.connection.columns(table).map { |c| c.name }
229
+ end
230
+
231
+
232
+ def self.each_table_page(table, records_per_page=1000)
233
+ total_count = table_record_count(table)
234
+ pages = (total_count.to_f / records_per_page).ceil - 1
235
+ id = table_column_names(table).first
236
+ boolean_columns = SerializationHelper::Utils.boolean_columns(table)
237
+ quoted_table_name = SerializationHelper::Utils.quote_table(table)
238
+
239
+ (0..pages).to_a.each do |page|
240
+ sql = ActiveRecord::Base.connection.add_limit_offset!("SELECT * FROM #{quoted_table_name} ORDER BY #{id}",
241
+ :limit => records_per_page, :offset => records_per_page * page
242
+ )
243
+ records = ActiveRecord::Base.connection.select_all(sql)
244
+ records = SerializationHelper::Utils.convert_booleans(records, boolean_columns)
245
+ yield records
246
+ end
247
+ end
248
+
249
+ def self.table_record_count(table)
250
+ ActiveRecord::Base.connection.select_one("SELECT COUNT(*) FROM #{SerializationHelper::Utils.quote_table(table)}").values.first.to_i
251
+ end
252
+
253
+ end
254
+
255
+ end
@@ -0,0 +1,72 @@
1
+ #Copied and modified from yamldb gem (https://github.com/ludicast/yaml_db)
2
+ #
3
+ # Credits:
4
+ # Created by Orion Henry and Adam Wiggins.
5
+ # Major updates by Ricardo Chimal, Jr.
6
+ # Patches contributed by Michael Irwin, Tom Locke, and Tim Galeckas.
7
+
8
+ require 'yaml'
9
+ require 'active_record'
10
+ require 'fidius-common/yamldb/serialization_helper'
11
+ require 'active_support/core_ext/kernel/reporting'
12
+
13
+ module YamlDb
14
+ module Helper
15
+ def self.loader
16
+ YamlDb::Load
17
+ end
18
+
19
+ def self.dumper
20
+ YamlDb::Dump
21
+ end
22
+
23
+ def self.extension
24
+ "yml"
25
+ end
26
+ end
27
+
28
+
29
+ module Utils
30
+ def self.chunk_records(records)
31
+ yaml = [ records ].to_yaml
32
+ yaml.sub!("--- \n", "")
33
+ yaml.sub!('- - -', ' - -')
34
+ yaml
35
+ end
36
+ end
37
+
38
+ class Dump < SerializationHelper::Dump
39
+
40
+ def self.dump_table_columns(io, table)
41
+ io.write("\n")
42
+ io.write({ table => { 'columns' => table_column_names(table) } }.to_yaml)
43
+ end
44
+
45
+ def self.dump_table_records(io, table)
46
+ table_record_header(io)
47
+
48
+ column_names = table_column_names(table)
49
+
50
+ each_table_page(table) do |records|
51
+ rows = SerializationHelper::Utils.unhash_records(records, column_names)
52
+ io.write(YamlDb::Utils.chunk_records(records))
53
+ end
54
+ end
55
+
56
+ def self.table_record_header(io)
57
+ io.write(" records: \n")
58
+ end
59
+
60
+ end
61
+
62
+ class Load < SerializationHelper::Load
63
+ def self.load_documents(io, truncate = true)
64
+ YAML.load_documents(io) do |ydoc|
65
+ ydoc.keys.each do |table_name|
66
+ next if ydoc[table_name].nil?
67
+ load_table(table_name, ydoc[table_name], truncate)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,32 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "fidius-common/yamldb/yaml_db"
4
+
5
+ module FIDIUS
6
+ module Common
7
+ # Provides methods for the export/import of database dumps to/from YAML.
8
+ module Db
9
+ # Exports database schema and content to yml file. This should work
10
+ # for all ActiveRecord database adapters.
11
+ #
12
+ # @param [String] path to yml file with database configuration
13
+ # @param [String] name of the db to export, must match an entry in the yml configuration file.
14
+ # @param [String] target dir where the exported data should be stored
15
+ # @param [Boolean] create target dir with timestamp in the dirname
16
+ def self.export(config_filename, db_entry, target_dir = "", timestamp = true)
17
+ dumper = SerializationHelper::Base.new(YamlDb::Helper, config_filename, db_entry)
18
+ dumper.dump(target_dir, timestamp)
19
+ end
20
+
21
+ # Imports the content of a previously exported yml file.
22
+ #
23
+ # @param [String] path to yml file with database configuration
24
+ # @param [String] name of the db to export, must match an entry in the yml configuration file.
25
+ # @param [String] dir to the previously exported yml file
26
+ def self.import(config_filename, db_entry, import_dir)
27
+ loader = SerializationHelper::Base.new(YamlDb::Helper, config_filename, db_entry)
28
+ loader.load(import_dir)
29
+ end
30
+ end
31
+ end
32
+ end
data/lib/fidius-common.rb CHANGED
@@ -7,9 +7,10 @@ module FIDIUS
7
7
  # non-FIDIUS context, since there are no other FIDIUS-related
8
8
  # dependencies.
9
9
  module Common
10
-
11
- autoload :VERSION, './fidius-common/version'
12
-
10
+
11
+ autoload :VERSION, 'fidius-common/version'
12
+ autoload :Db, 'fidius-common/yamldb'
13
+
13
14
  # Since this addon changes behaviour of +to_json+, you have to
14
15
  # require it explicitly either with this helper method or manually
15
16
  # with
@@ -24,6 +25,6 @@ module FIDIUS
24
25
  def self.install_json_symbol_addon
25
26
  require 'fidius-common/json_symbol_addon'
26
27
  end
27
-
28
+
28
29
  end # module Common
29
30
  end # module FIDIUS
data/test/t_helper.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+ require 'test/unit'
4
+
@@ -1,5 +1,5 @@
1
+ require 't_helper'
1
2
  require 'fidius-common/ip-helper'
2
- require 'test/unit'
3
3
 
4
4
  class IPHelperTest < Test::Unit::TestCase
5
5
  def test_get_localhost
@@ -1,5 +1,5 @@
1
+ require 't_helper'
1
2
  require 'fidius-common/json_symbol_addon'
2
- require 'test/unit'
3
3
 
4
4
 
5
5
  class JSONSymbolAddonTest < Test::Unit::TestCase
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fidius-common
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ prerelease: true
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4beta0
9
+ version: 0.0.4beta0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Dominik Menke
@@ -15,13 +15,28 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-17 00:00:00 +01:00
18
+ date: 2011-04-20 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: activesupport
22
+ name: simplecov
23
23
  prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 0
31
+ - 4
32
+ - 0
33
+ version: 0.4.0
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: activesupport
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
25
40
  none: false
26
41
  requirements:
27
42
  - - ">="
@@ -32,7 +47,7 @@ dependencies:
32
47
  - 0
33
48
  version: 3.0.0
34
49
  type: :runtime
35
- version_requirements: *id001
50
+ version_requirements: *id002
36
51
  description: |-
37
52
  Common useful libraries and methods for the FIDIUS C&C server, the FIDIUS 'architecture' and maybe other components.
38
53
 
@@ -59,6 +74,10 @@ files:
59
74
  - lib/fidius-common/ip-helper.rb
60
75
  - lib/fidius-common/json_symbol_addon.rb
61
76
  - lib/fidius-common/version.rb
77
+ - lib/fidius-common/yamldb.rb
78
+ - lib/fidius-common/yamldb/serialization_helper.rb
79
+ - lib/fidius-common/yamldb/yaml_db.rb
80
+ - test/t_helper.rb
62
81
  - test/test_iphelper.rb
63
82
  - test/test_json_addon.rb
64
83
  has_rdoc: true
@@ -76,6 +95,9 @@ rdoc_options:
76
95
  - lib/fidius-common/ip-helper.rb
77
96
  - lib/fidius-common/json_symbol_addon.rb
78
97
  - lib/fidius-common/version.rb
98
+ - lib/fidius-common/yamldb.rb
99
+ - lib/fidius-common/yamldb/serialization_helper.rb
100
+ - lib/fidius-common/yamldb/yaml_db.rb
79
101
  - README.md
80
102
  - LICENSE
81
103
  - CREDITS.md
@@ -92,11 +114,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
114
  required_rubygems_version: !ruby/object:Gem::Requirement
93
115
  none: false
94
116
  requirements:
95
- - - ">="
117
+ - - ">"
96
118
  - !ruby/object:Gem::Version
97
119
  segments:
98
- - 0
99
- version: "0"
120
+ - 1
121
+ - 3
122
+ - 1
123
+ version: 1.3.1
100
124
  requirements: []
101
125
 
102
126
  rubyforge_project: ""