dibber 0.0.2

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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Rob Nichols
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,14 @@
1
+ == Dibber
2
+
3
+ A set of tool to tidy up rails seeds.rb files.
4
+
5
+ Dibber has two compoments:
6
+
7
+ === Seeder
8
+ is designed to simplify the process of pulling attributes from
9
+ YAML files, and populating ActiveRecord objects with those attributes.
10
+
11
+ === ProcessLog
12
+ provides Seeder with a simple before and after reporting tool.
13
+
14
+ Have a look at the examples folder, for a guide to how Dibber is used.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'rake/clean'
5
+ require 'rdoc/task'
6
+ require 'rake/testtask'
7
+
8
+ Rake::RDocTask.new do |rdoc|
9
+ files =['README.rdoc', 'MIT-LICENSE', 'lib/**/*.rb']
10
+ rdoc.rdoc_files.add(files)
11
+ rdoc.main = "README.rdoc" # page to start on
12
+ rdoc.title = "Dibber Docs"
13
+ rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
14
+ rdoc.options << '--line-numbers'
15
+ end
16
+
17
+ Rake::TestTask.new do |t|
18
+ t.test_files = FileList['test/**/*.rb']
19
+ end
data/lib/dibber.rb ADDED
@@ -0,0 +1,7 @@
1
+ require_relative 'dibber/process_log'
2
+ require_relative 'dibber/seeder'
3
+ require_relative 'dibber/version'
4
+
5
+ module Dibber
6
+
7
+ end
@@ -0,0 +1,33 @@
1
+ module Dibber
2
+ class ProcessLog
3
+
4
+ def initialize
5
+ @log = {}
6
+ end
7
+
8
+ def start(name, command)
9
+ @log[name] = {
10
+ :start => eval(command),
11
+ :command => command
12
+ }
13
+ end
14
+
15
+ def finish(name)
16
+ @log[name][:finish] = eval(@log[name][:command])
17
+ end
18
+
19
+ def raw
20
+ @log
21
+ end
22
+
23
+ def report
24
+ @report = []
25
+ @log.each do |name, log|
26
+ finish(name) unless @log[name][:finish]
27
+ @report << "#{name.to_s.capitalize.gsub(/_/, ' ')} was #{log[:start]}, now #{log[:finish]}."
28
+ end
29
+ return @report
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,66 @@
1
+ module Dibber
2
+ class Seeder
3
+ attr_accessor :klass, :file, :method
4
+
5
+ def self.process_log
6
+ @process_log || start_process_log
7
+ end
8
+
9
+ def self.start_process_log
10
+ @process_log = ProcessLog.new
11
+ @process_log.start :time, 'Time.now.to_s(:long_time)'
12
+ return @process_log
13
+ end
14
+
15
+ def self.report
16
+ @process_log.report
17
+ end
18
+
19
+ def self.monitor(klass)
20
+ process_log.start(klass.to_s.tableize.to_sym, "#{klass}.count")
21
+ end
22
+
23
+ def self.objects_from(file)
24
+ YAML.load_file("#{seeds_path}#{file}")
25
+ end
26
+
27
+ def self.seeds_path
28
+ @seeds_path || raise_no_seeds_path_error
29
+ end
30
+
31
+ def self.seeds_path=(path)
32
+ path = path + '/' unless path =~ /\/$/
33
+ @seeds_path = path
34
+ end
35
+
36
+
37
+ def initialize(klass, file, method = 'attributes')
38
+ @klass = klass
39
+ @file = file
40
+ @method = method
41
+ end
42
+
43
+ def build
44
+ start_log
45
+ objects.each do |name, attributes|
46
+ object = klass.find_or_initialize_by_name(name)
47
+ object.send("#{method}=", attributes)
48
+ object.save
49
+ end
50
+ end
51
+
52
+ def start_log
53
+ self.class.monitor(klass)
54
+ end
55
+
56
+ def objects
57
+ self.class.objects_from(file)
58
+ end
59
+
60
+ private
61
+ def self.raise_no_seeds_path_error
62
+ raise "You must set the path to your seed files via Seeder.seeds_path = 'path/to/seed/files'"
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module Dibber
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'process_log'
2
+
3
+ process_log = ProcessLog.new
4
+ process_log.start :time_one, 'Time.now'
5
+ sleep(2)
6
+ process_log.finish :time_one
7
+
8
+ process_log.start :time_two, 'Time.now'
9
+ sleep(4)
10
+
11
+ puts "\nReport:"
12
+ puts process_log.report
13
+ puts "\nRaw:"
14
+ p process_log.raw
15
+ puts "\n"
@@ -0,0 +1,37 @@
1
+ Seeder = Dibble::Seeder
2
+
3
+ # Set up the path to seed YAML files
4
+ Seeder.seeds_path = "#{Rails.root}/db/seeds"
5
+
6
+ # Example 1. Seeder is used to monitor the process
7
+ # and grab the attributes from the YAML file
8
+ Seeder.monitor Borough
9
+ Seeder.objects_from("boroughs.yml").each do |holder, borough|
10
+ Borough.find_or_create_by_name(borough)
11
+ end
12
+
13
+ # Example 2. Seeder is only used to monitor the process
14
+ Seeder.monitor AdminUser
15
+ admin_email = 'admin@undervale.co.uk'
16
+ password = 'change_me'
17
+ AdminUser.create!(
18
+ :email => admin_email,
19
+ :password => password,
20
+ :password_confirmation => password
21
+ ) unless AdminUser.exists?(:email => admin_email)
22
+
23
+ # Example 3. Seeder grabs the attributes from the YAML and build a
24
+ # set of Fee objects with those attributes (or updates them if
25
+ # they already exist.
26
+ # Note that the build process requires the model to have a name field.
27
+ Seeder.new(Fee, 'fees.yml').build
28
+
29
+ # Example 4. Seeder working with a name spaced object
30
+ Seeder.new(Disclaimer::Document, 'disclaimer/documents.yml').build
31
+
32
+ # Example 5. Seeder using values in the yaml file to set a single field
33
+ Seeder.new(Category, 'categories.yml', 'description').build
34
+
35
+ # Output a report showing how the numbers of each type of object
36
+ # have changed through the process.
37
+ puts Seeder.report
@@ -0,0 +1,6 @@
1
+ stratford: Stratford on Avon
2
+ rugby: Rugby
3
+ north_warwick: North Warwickshire
4
+ nuneaton: Nuneaton and Bedworth
5
+ warwick: Warwick
6
+
@@ -0,0 +1,3 @@
1
+ one: First
2
+ two: Second
3
+ three: Third
@@ -0,0 +1,4 @@
1
+ main:
2
+ title: Disclaimer
3
+ header: Don't do it.
4
+ footer: Click to accept.
@@ -0,0 +1,7 @@
1
+ annual:
2
+ description: One hundred
3
+ value: 100
4
+
5
+ deposit:
6
+ description: Seventy five
7
+ value: 75
@@ -0,0 +1,48 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'../..','lib')
2
+
3
+ require 'test/unit'
4
+ require 'dibber'
5
+
6
+ module Dibber
7
+ class ProcessLogTest < Test::Unit::TestCase
8
+ def setup
9
+ @process_log = ProcessLog.new
10
+
11
+ end
12
+
13
+ def test_one
14
+ @process_log.start(:one, '1')
15
+ @process_log.finish(:one)
16
+
17
+ expected = {:one => {:start => 1, :finish => 1, :command => '1'}}
18
+ assert_equal(expected, @process_log.raw)
19
+ end
20
+
21
+ def test_two
22
+ test_one
23
+ @process_log.start(:two, '2')
24
+ @process_log.finish(:two)
25
+
26
+ expected = {
27
+ :one => {:start => 1, :finish => 1, :command => '1'},
28
+ :two => {:start => 2, :finish => 2, :command => '2'}
29
+ }
30
+ assert_equal(expected, @process_log.raw)
31
+ end
32
+
33
+ def test_report
34
+ test_two
35
+ expected = [
36
+ 'One was 1, now 1.',
37
+ 'Two was 2, now 2.'
38
+ ]
39
+ assert_equal(expected, @process_log.report)
40
+ end
41
+
42
+ def test_report_with_no_finish
43
+ @process_log.start(:no_finish, '1')
44
+ expected = ['No finish was 1, now 1.']
45
+ assert_equal(expected, @process_log.report)
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dibber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rob Nichols
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-07 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Packages up code needed to pull data from YAML files when seeding, and
15
+ adds a process log.
16
+ email:
17
+ - rob@undervale.co.uk
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/examples/process_logs.rb
23
+ - lib/examples/seeds/boroughs.yml
24
+ - lib/examples/seeds/disclaimer/documents.yml
25
+ - lib/examples/seeds/fees.yml
26
+ - lib/examples/seeds/categories.yml
27
+ - lib/examples/seeds.rb
28
+ - lib/dibber.rb
29
+ - lib/dibber/version.rb
30
+ - lib/dibber/process_log.rb
31
+ - lib/dibber/seeder.rb
32
+ - MIT-LICENSE
33
+ - Rakefile
34
+ - README.rdoc
35
+ - test/dibber/process_log_test.rb
36
+ homepage: https://github.com/reggieb/Dibber
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.24
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Tool for seeding database from YAML.
60
+ test_files:
61
+ - test/dibber/process_log_test.rb