yarf 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b07b267c97b9b30ffdf5d00a657b22ae29ac3b98
4
+ data.tar.gz: 11f08f6c9fe346c62766b732f943ff52754db3dc
5
+ SHA512:
6
+ metadata.gz: c1db2b6dfaf2e1417cc9ff39c6236c5c6b5a7e10a3d8aaad5cafff4385206bf9f0deb45681d4784e106bb4a9e6b41173393231dcd3dc5f0c0905cee92beb6451
7
+ data.tar.gz: f0b4e95061723c827a09c4325e988d7ff1e2f680dd1cbf06a448548afbf5b5cdc0283e40c708bd3d03712c4f9d96e7fda5c1d5ebdc8d238b4f5f67395a6679c7
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yarf.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 akm
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # yarf
2
+
3
+ yarf means "Yet Another Rails Fixture"
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'yarf', group: :development
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install yarf
20
+
21
+ ## Usage
22
+
23
+
24
+ At the point where you want to take the database snapshot, just do like this.
25
+
26
+ ```
27
+ Yarf.record("snapshot_name1")
28
+ ```
29
+
30
+ And you can load the snapshot from fixtures to database like this
31
+ ```
32
+ Yarf.load_fixtures("000-snapshot_name1")
33
+ ```
34
+
35
+ The number might be different from "000", please check the file name at spec/fixtures.
36
+
37
+
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. Run `bundle exec yarf` to use the gem in this directory, ignoring other installed copies of this gem.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/yarf.
48
+
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
53
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "yarf"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "yarf"
@@ -0,0 +1,28 @@
1
+ require "yarf/version"
2
+
3
+ # Yarf means "Yet Another Rails Fixture"
4
+ module Yarf
5
+ autoload :Config , "yarf/config"
6
+ autoload :ModelConfig, "yarf/model_config"
7
+ autoload :Dumper , "yarf/dumper"
8
+ autoload :Recorder , "yarf/recorder"
9
+
10
+ class << self
11
+ attr_writer :config_path
12
+ def config_path
13
+ @config_path ||= Dir.glob("**/spec/fixtures/config.yml").first
14
+ end
15
+ def instance
16
+ @instance ||= Config.load_file(config_path)
17
+ end
18
+ def record(name)
19
+ instance.record(name)
20
+ end
21
+ def run_scenarios(*args)
22
+ instance.run_scenarios(*args)
23
+ end
24
+ def load_fixtures(*args)
25
+ instance.load_fixtures(*args)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,122 @@
1
+ require 'yarf'
2
+ require 'tsort'
3
+
4
+ module Yarf
5
+ class Config
6
+
7
+ class << self
8
+ def load_file(path)
9
+ new(YAML.load_file_with_erb(path))
10
+ end
11
+ end
12
+
13
+ DEFAULT_CONFIG_MODELS = {
14
+ "common" => {
15
+ "ignored_columns" => %w[created_at updated_at],
16
+ }
17
+ }.freeze
18
+
19
+ attr_reader :base_dir
20
+ attr_reader :model_configs
21
+ def initialize(hash)
22
+ hash ||= {}
23
+ @base_dir = File.expand_path(hash["base_dir"] || 'spec/fixtures')
24
+ @models_paths = hash["app_models_paths"] || (defined?(Rails) ? [Rails.root.join("app/models").to_s] : raise("no app_models_paths found"))
25
+ @model_configs = load_model_configs(hash["models"] || DEFAULT_CONFIG_MODELS.dup)
26
+ end
27
+
28
+ def load_model_configs(hash)
29
+ common_mc = hash.delete("common") || {}
30
+ unloaded = hash.keys
31
+ class_names = @models_paths.map do |path|
32
+ Dir.chdir(path) do
33
+ ignores = File.readable?(".yarfignore") ? File.read(".yarfignore").lines.map(&:strip) : []
34
+ Dir.glob('**/*.rb').
35
+ # reject app/models/concerns to avoid "Circular dependency detected while autoloading constant Concerns::Traceable"
36
+ reject{|path| ignores.any?{|ig| path =~ /\A#{Regexp.escape(ig)}/} }.
37
+ map{|s| s.sub(/\.rb\z/,'').camelize }
38
+ end
39
+ end
40
+ class_names.flatten!
41
+ model_classes = class_names.map(&:constantize).select{|c| c < ActiveRecord::Base }
42
+ configs = model_classes.map do |model_class|
43
+ mc = common_mc.dup.update(hash[model_class.name] || {})
44
+ unloaded.delete(model_class.name)
45
+ ModelConfig.new(model_class, mc)
46
+ end
47
+ configs += unloaded.map do |class_name|
48
+ mc = common_mc.dup.update(hash[class_name] || {})
49
+ klass = class_name.constantize
50
+ ModelConfig.new(klass, mc)
51
+ end
52
+ tsort(configs)
53
+ end
54
+
55
+ def tsort(configs)
56
+ sources = configs.each_with_object({}){|c,d| d[c.name] = c}
57
+ deps = configs.each_with_object({}) do |c,d|
58
+ d[c.name] = c.model_class.reflections.values.
59
+ select{|r| r.macro == :belongs_to }.
60
+ map{|r| r.polymorphic? ? nil : r.klass.name }.compact
61
+ if c.dependencies
62
+ d[c.name] += c.dependencies
63
+ end
64
+ end
65
+ deps.singleton_class.module_eval do
66
+ include TSort
67
+ alias tsort_each_node each_key
68
+ def tsort_each_child(node, &block)
69
+ fetch(node).each(&block)
70
+ end
71
+ end
72
+ deps.tsort.map{|name| sources[name]}
73
+ end
74
+
75
+ def recorder
76
+ @recorder ||= Recorder.new(self)
77
+ end
78
+ def dumper
79
+ @dumper ||= Dumper.new(self)
80
+ end
81
+
82
+ def target_model_configs
83
+ model_configs.select(&:target?)
84
+ end
85
+ def skipped_model_configs
86
+ model_configs.select(&:skip?)
87
+ end
88
+
89
+ def target_model_names
90
+ target_model_configs.map(&:name)
91
+ end
92
+ def skipped_model_names
93
+ skipped_model_configs.map(&:name)
94
+ end
95
+
96
+ def delete_targets
97
+ target_model_configs.reverse.each(&:delete_all)
98
+ end
99
+
100
+ def destroy_targets
101
+ target_model_configs.reverse.each(&:destroy_all)
102
+ end
103
+
104
+ def record(name)
105
+ recorder.dump(name)
106
+ end
107
+
108
+ def run_scenarios(*args)
109
+ args.each do |path|
110
+ puts "executing ... #{path}"
111
+ load(path)
112
+ end
113
+ end
114
+
115
+ def load_fixtures(fixtures_dir)
116
+ delete_targets
117
+ dir = File.join(base_dir, fixtures_dir)
118
+ target_model_configs.each{ |mc| mc.load_fixtures(dir) }
119
+ end
120
+
121
+ end
122
+ end
@@ -0,0 +1,24 @@
1
+ require 'yarf'
2
+ require 'fileutils'
3
+
4
+ module Yarf
5
+ class Dumper
6
+
7
+ attr_reader :config
8
+ def initialize(config)
9
+ @config = config
10
+ end
11
+
12
+ def dump(dest_dir)
13
+ config.target_model_configs.each do |mc|
14
+ columns = mc.selected_columns.map(&:name)
15
+ rows = mc.model_class.connection.select("select * from #{mc.model_class.table_name}").to_hash.map do |r|
16
+ columns.each_with_object({}){|c,d| d[c] = r[c]}
17
+ end
18
+ path = File.expand_path(mc.name.underscore + ".yml", dest_dir)
19
+ FileUtils.mkdir_p(File.dirname(path))
20
+ open(path, "w"){|f| f.puts(YAML.dump(rows))}
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,55 @@
1
+ require 'yarf'
2
+
3
+ module Yarf
4
+ class ModelConfig
5
+
6
+ attr_reader :model_class, :name, :ignored_columns, :dependencies
7
+ def initialize(model_class, hash)
8
+ @model_class = model_class
9
+ @name = model_class.name
10
+ @skip = !!hash["skip"]
11
+ @ignored_columns = hash["ignored_columns"] || []
12
+ @dependencies = hash["dependencies"] || []
13
+ end
14
+
15
+ def skip?
16
+ @skip
17
+ end
18
+
19
+ def target?
20
+ not(skip?) and not(@model_class.abstract_class?)
21
+ end
22
+
23
+ def selected_columns
24
+ @selected_columns ||= model_class.columns.reject{|c| ignored_columns.include?(c.name)}
25
+ end
26
+
27
+ def delete_all
28
+ # puts "deleting #{name}"
29
+ model_class.delete_all
30
+ end
31
+
32
+ def destroy_all
33
+ puts "destroying #{name}"
34
+ model_class.destroy_all
35
+ end
36
+
37
+ def load_fixtures(fixtures_dir)
38
+ puts model_class.all.map(&:inspect)
39
+ [:create, :save].each{|a| model_class.skip_callback(a)}
40
+ path = File.join(fixtures_dir, name.underscore + ".yml")
41
+ unless File.readable?(path)
42
+ Rails.logger.warn("fixture #{path} not found for #{name}")
43
+ return
44
+ end
45
+ begin
46
+ rows = YAML.load_file(path)
47
+ rows.each do |row|
48
+ model_class.connection.insert_fixture(row, model_class.table_name)
49
+ end
50
+ ensure
51
+ [:create, :save].each{|a| model_class.set_callback(a)}
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ require 'yarf'
3
+ require 'fileutils'
4
+
5
+ module Yarf
6
+ class Recorder
7
+
8
+ attr_accessor :config
9
+ def initialize(config)
10
+ @config = config
11
+ @no = (ENV["NO"] || 0).to_i # @test フィクスチャを作成する際にyarfが使用する番号の初期値
12
+ end
13
+
14
+ def dump(name)
15
+ name.sub!(/\.rb\z/, '')
16
+ dir = File.expand_path("%03d-%s" % [@no, name], config.base_dir)
17
+ FileUtils.mkdir_p(dir)
18
+ config.dumper.dump(dir)
19
+ @no += 1
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module Yarf
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yarf/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yarf"
8
+ spec.version = Yarf::VERSION
9
+ spec.authors = ["akm"]
10
+ spec.email = ["t-akima@groovenauts.jp"]
11
+
12
+ spec.summary = %q{yarf means Yet Another Rails Fixture}
13
+ spec.description = %q{yarf means Yet Another Rails Fixture}
14
+ spec.homepage = "https://github.com/groovenauts/yarf"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yarf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - akm
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: yarf means Yet Another Rails Fixture
56
+ email:
57
+ - t-akima@groovenauts.jp
58
+ executables:
59
+ - yarf
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - exe/yarf
73
+ - lib/yarf.rb
74
+ - lib/yarf/config.rb
75
+ - lib/yarf/dumper.rb
76
+ - lib/yarf/model_config.rb
77
+ - lib/yarf/recorder.rb
78
+ - lib/yarf/version.rb
79
+ - yarf.gemspec
80
+ homepage: https://github.com/groovenauts/yarf
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.4.5.1
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: yarf means Yet Another Rails Fixture
104
+ test_files: []