flextures 3.0.1 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,57 +1 @@
1
- # encoding: utf-8
2
-
3
- require 'rubygems'
4
- require 'bundler'
5
- begin
6
- Bundler.setup(:default, :development)
7
- rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
10
- exit e.status_code
11
- end
12
- require 'rake'
13
-
14
- require 'jeweler'
15
- Jeweler::Tasks.new do |gem|
16
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
- gem.name = "flextures"
18
- gem.homepage = "http://github.com/baban/flextures"
19
- gem.license = "MIT"
20
- gem.summary = %Q{load and dump fixtures}
21
- gem.description = %Q{load and dump fixtures}
22
- gem.email = "babanba.n@gmail.com"
23
- gem.authors = ["baban"]
24
- # dependencies defined in Gemfile
25
- end
26
- Jeweler::RubygemsDotOrgTasks.new
27
-
28
- require 'rake/testtask'
29
- Rake::TestTask.new(:test) do |test|
30
- test.libs << 'lib' << 'test'
31
- test.pattern = 'test/**/test_*.rb'
32
- test.verbose = true
33
- end
34
-
35
- =begin
36
- require 'rcov/rcovtask'
37
- Rcov::RcovTask.new do |test|
38
- test.libs << 'test'
39
- test.pattern = 'test/**/test_*.rb'
40
- test.verbose = true
41
- test.rcov_opts << '--exclude "gems/*"'
42
- end
43
- =end
44
-
45
- task :default => :test
46
-
47
- require 'rdoc/task'
48
- Rake::RDocTask.new do |rdoc|
49
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
-
51
- rdoc.rdoc_dir = 'rdoc'
52
- rdoc.title = "flextures #{version}"
53
- rdoc.rdoc_files.include('README*')
54
- rdoc.rdoc_files.include('lib/**/*.rb')
55
- rdoc.options = ["--charset", "utf-8", "--line-numbers"]
56
- end
57
-
1
+ require "bundler/gem_tasks"
@@ -1,75 +1,23 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'flextures/version'
5
5
 
6
- Gem::Specification.new do |s|
7
- s.name = "flextures"
8
- s.version = "3.0.1"
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "flextures"
8
+ spec.version = Flextures::VERSION
9
+ spec.authors = ["baban"]
10
+ spec.email = ["babanba.n@gmail.com"]
11
+ spec.summary = %q{load and dump fixtures.}
12
+ spec.description = %q{load and dump fixtures.}
13
+ spec.homepage = "http://github.com/baban/flextures"
14
+ spec.license = "MIT"
9
15
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["baban"]
12
- s.date = "2013-06-27"
13
- s.description = "load and dump fixtures"
14
- s.email = "babanba.n@gmail.com"
15
- s.extra_rdoc_files = [
16
- "LICENSE.txt",
17
- "README.ja.rdoc",
18
- "README.rdoc"
19
- ]
20
- s.files = [
21
- ".document",
22
- "CHANGELOG",
23
- "Gemfile",
24
- "Gemfile.lock",
25
- "History.txt",
26
- "LICENSE.txt",
27
- "README.ja.rdoc",
28
- "README.rdoc",
29
- "Rakefile",
30
- "VERSION",
31
- "flextures.gemspec",
32
- "lib/flextures.rb",
33
- "lib/flextures/flextures.rake",
34
- "lib/flextures/flextures.rb",
35
- "lib/flextures/flextures_base_config.rb",
36
- "lib/flextures/flextures_command.rb",
37
- "lib/flextures/flextures_dumper.rb",
38
- "lib/flextures/flextures_extension_modules.rb",
39
- "lib/flextures/flextures_factory.rb",
40
- "lib/flextures/flextures_loader.rb",
41
- "lib/flextures/flextures_railtie.rb",
42
- "lib/flextures/rspec_flextures_support.rb",
43
- "lib/flextures/testunit_flextures_support.rb",
44
- "lib/flextures/version.rb",
45
- "test/test_helper.rb",
46
- "test/unit/test_flextures.rb",
47
- "test/unit/test_flextures_args.rb",
48
- "test/unit/test_flextures_dumper.rb",
49
- "test/unit/test_flextures_extention_modules.rb",
50
- "test/unit/test_flextures_hooks.rb",
51
- "test/unit/test_flextures_loader.rb",
52
- "test/unit/test_simple.rb"
53
- ]
54
- s.homepage = "http://github.com/baban/flextures"
55
- s.licenses = ["MIT"]
56
- s.require_paths = ["lib"]
57
- s.rubygems_version = "1.8.10"
58
- s.summary = "load and dump fixtures"
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
59
20
 
60
- if s.respond_to? :specification_version then
61
- s.specification_version = 3
62
-
63
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
64
- s.add_development_dependency(%q<bundler>, ["= 1.3.5"])
65
- s.add_development_dependency(%q<jeweler>, ["= 1.8.3"])
66
- else
67
- s.add_dependency(%q<bundler>, ["= 1.3.5"])
68
- s.add_dependency(%q<jeweler>, ["= 1.8.3"])
69
- end
70
- else
71
- s.add_dependency(%q<bundler>, ["= 1.3.5"])
72
- s.add_dependency(%q<jeweler>, ["= 1.8.3"])
73
- end
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
74
23
  end
75
-
@@ -2,16 +2,15 @@
2
2
 
3
3
  require 'ostruct'
4
4
  require 'csv'
5
-
6
5
  require "flextures/flextures_base_config"
7
6
  require "flextures/flextures_extension_modules"
8
7
  require "flextures/flextures_factory"
9
8
 
10
9
  module Flextures
11
- # ActiveRecord Model is created that guess by table_name
10
+ # guessing ActiveRecord Model name by table_name and create
12
11
  # @params [String|Symbol] table_name
13
12
  # @params [ActiveRecord::Base] model class
14
- def self.create_model table_name
13
+ def self.create_model( table_name )
15
14
  # when Model is defined in FactoryFilter
16
15
  a = ->{
17
16
  f = Factory::FACTORIES[table_name.to_sym]
@@ -26,7 +25,7 @@ module Flextures
26
25
  end
27
26
  }
28
27
  # when cannot guess Model name
29
- c = ->{
28
+ c = ->{
30
29
  Class.new(ActiveRecord::Base){ |o| o.table_name=table_name }
31
30
  }
32
31
  a.call || b.call || c.call
@@ -36,16 +35,16 @@ module Flextures
36
35
  def self.init_load
37
36
  if defined?(Rails) and Rails.root
38
37
  [
39
- "#{Rails.root.to_path}/config/flextures.config.rb",
40
- "#{Rails.root.to_path}/config/flextures.factory.rb",
41
- ].each { |fn| load(fn) if File.exist?(fn) }
38
+ File.join( Rails.root.to_path,"/config/flextures.config.rb" ),
39
+ File.join( Rails.root.to_path,"/config/flextures.factory.rb" ),
40
+ ].each { |fn| File.exist?(fn) && load(fn) }
42
41
  end
43
42
  end
44
43
 
45
44
  # @return [Array] flextures useable table names
46
45
  def self.deletable_tables
47
46
  tables = ActiveRecord::Base.connection.tables
48
- Flextures::Config.ignore_tables.each { |name| tables.delete name }
47
+ Flextures::Config.ignore_tables.each { |name| tables.delete( name.to_s ) }
49
48
  tables
50
49
  end
51
50
 
@@ -61,7 +60,7 @@ module Flextures
61
60
  end
62
61
  end
63
62
 
64
- def self.delete_tables *tables
63
+ def self.delete_tables( *tables )
65
64
  tables.each do |name|
66
65
  # if 'name' variable is 'database view', raise error
67
66
  begin
@@ -73,7 +72,7 @@ module Flextures
73
72
 
74
73
  # It is debug method to use like 'tab' method
75
74
  # @params [Proc] dumper write dump information
76
- def self.table_tap &dumper
75
+ def self.table_tap( &dumper )
77
76
  tables = Flextures::deletable_tables
78
77
  tables.each do |name|
79
78
  # if 'name' variable is 'database view', raise error
@@ -85,10 +84,10 @@ module Flextures
85
84
  end
86
85
  end
87
86
 
88
- # parse arguments functions
87
+ # parse arguments functions.
89
88
  module ARGS
90
89
  # parse rake ENV parameters
91
- def self.parse option={}
90
+ def self.parse( option={} )
92
91
  table_names = []
93
92
  if v = (ENV["T"] or ENV["TABLE"])
94
93
  table_names = v.split(',').map{ |name| { table: name, file: name } }
@@ -111,24 +110,24 @@ module Flextures
111
110
  names.map{ |name| { table: name, file: name } }
112
111
  end
113
112
  }
114
- # parse filename define parameters
113
+ # parse filename and define parameters.
115
114
  table_names = fixtures_args_parser.call ENV["FIXTURES"] if ENV["FIXTURES"]
116
115
  table_names = fixtures_args_parser.call ENV["FILE"] if ENV["FILE"]
117
116
  table_names = fixtures_args_parser.call ENV["F"] if ENV["F"]
118
117
 
119
- table_names = table_names.map{ |option| option.merge dir: ENV["DIR"] } if ENV["DIR"]
120
- table_names = table_names.map{ |option| option.merge dir: ENV["D"] } if ENV["D"]
118
+ table_names = table_names.map{ |option| option.merge( dir: ENV["DIR"] ) } if ENV["DIR"]
119
+ table_names = table_names.map{ |option| option.merge( dir: ENV["D"] ) } if ENV["D"]
121
120
 
122
- table_names = table_names.map{ |option| option.merge minus: ENV["MINUS"].to_s.split(",") } if ENV["MINUS"]
123
- table_names = table_names.map{ |option| option.merge plus: ENV["PLUS"].to_s.split(",") } if ENV["PLUS"]
121
+ table_names = table_names.map{ |option| option.merge( minus: ENV["MINUS"].to_s.split(",") ) } if ENV["MINUS"]
122
+ table_names = table_names.map{ |option| option.merge( plus: ENV["PLUS"].to_s.split(",") ) } if ENV["PLUS"]
124
123
 
125
- table_names = table_names.map{ |option| option.merge silent: true } if ENV["OPTION"].to_s.split(",").include?("silent")
126
- table_names = table_names.map{ |option| option.merge unfilter: true } if ENV["OPTION"].to_s.split(",").include?("unfilter")
127
- table_names = table_names.map{ |option| option.merge strict: true } if ENV["OPTION"].to_s.split(",").include?("strict")
128
- table_names = table_names.map{ |option| option.merge stair: true } if ENV["OPTION"].to_s.split(",").include?("stair")
124
+ table_names = table_names.map{ |option| option.merge( silent: true ) } if ENV["OPTION"].to_s.split(",").include?("silent")
125
+ table_names = table_names.map{ |option| option.merge( unfilter: true ) } if ENV["OPTION"].to_s.split(",").include?("unfilter")
126
+ table_names = table_names.map{ |option| option.merge( strict: true ) } if ENV["OPTION"].to_s.split(",").include?("strict")
127
+ table_names = table_names.map{ |option| option.merge( stair: true ) } if ENV["OPTION"].to_s.split(",").include?("stair")
129
128
 
130
- # if mode is 'read mode' and file is not exist value is not return
131
- table_names.select! &exist if option[:mode] && option[:mode] == 'read'
129
+ # if mode is 'read mode' and file is not exist, value is not return.
130
+ table_names.select!(&exist) if option[:mode] && option[:mode] == 'read'
132
131
  table_names
133
132
  end
134
133
 
@@ -139,4 +138,3 @@ module Flextures
139
138
  end
140
139
  end
141
140
  end
142
-
@@ -14,6 +14,7 @@ module Flextures
14
14
  module Config
15
15
  @@read_onlys=[]
16
16
  @@configs={
17
+ use_transactional_fixtures: nil, # override activerecord base "use_transactional_fixtures" option if you set value boolean value
17
18
  ignore_tables: ["schema_migrations"], # 'ignore_tables' table data is not deleted by flextures delete_all method
18
19
  fixture_load_directory: "spec/fixtures/", # base load directory
19
20
  fixture_dump_directory: "spec/fixtures/", # dump load directory
@@ -30,5 +31,3 @@ module Flextures
30
31
  end
31
32
  end
32
33
  end
33
-
34
-
@@ -60,4 +60,3 @@ module Flextures
60
60
  end
61
61
  end
62
62
  end
63
-
@@ -65,7 +65,7 @@ module Flextures
65
65
  s = s.sub(/ +/, "") if s[0]==' '
66
66
  is_nl = false
67
67
  is_nl |= s["\n"]
68
- is_nl |= ["[","]","{","}","|","#","@","~","!","'","$","&","^","<",">","?","-","+","=",";",":",".",",","*","`","(",")"].member? s[0]
68
+ is_nl |= ["[","]","{","}","|","#","@","~","!","'","$","&","^","<",">","?","-","+","=",";",":",".",",","*","`","(",")"].member?(s[0])
69
69
  s = s.gsub(/\r\n/,"\n").gsub(/\r/,"\n") # 改行方法統一
70
70
  s = "|-\n " + s.gsub(/\n/,"\n ") if is_nl
71
71
  end
@@ -76,7 +76,7 @@ module Flextures
76
76
  d
77
77
  },
78
78
  }
79
- procs = rules.inject(proc{ |d| d }) { |sum,i| sum * (rule_map[i] || i) }
79
+ procs = rules.reduce(proc{ |d| d }) { |sum,i| sum * (rule_map[i] || i) }
80
80
  procs.call(val)
81
81
  end
82
82
 
@@ -86,13 +86,13 @@ module Flextures
86
86
  procs = (format == :yml)?
87
87
  [:nullstr, :null, proc { |d| Base64.encode64(d) } ] :
88
88
  [:null, proc { |d| Base64.encode64(d) } ]
89
- self.translate_creater d, procs
89
+ self.translate_creater( d, procs )
90
90
  },
91
91
  boolean:->( d, format ){
92
92
  procs = (format == :yml) ?
93
93
  [ :nullstr, proc { !(0==d || ""==d || !d) } ] :
94
94
  [ proc { !(0==d || ""==d || !d) } ]
95
- self.translate_creater d, procs
95
+ self.translate_creater( d, procs )
96
96
  },
97
97
  date:->( d, format ){
98
98
  procs = (format == :yml) ?
@@ -108,19 +108,19 @@ module Flextures
108
108
  },
109
109
  decimal:->( d, format ){
110
110
  procs = (format == :yml) ?
111
- [:nullstr, :blank2num, :bool2num, proc { |d| d.to_f } ] :
111
+ [:nullstr, :blank2num, :bool2num, proc { |d| d.to_f } ] :
112
112
  [:null2blankstr, :bool2num, proc { |d| d.to_f } ]
113
113
  self.translate_creater d, procs
114
114
  },
115
115
  float:->(d, format){
116
116
  procs = (format == :yml) ?
117
- [:nullstr, :blank2num, :bool2num, proc { |d| d.to_f } ] :
117
+ [:nullstr, :blank2num, :bool2num, proc { |d| d.to_f } ] :
118
118
  [:null2blankstr, :bool2num, proc { |d| d.to_f } ]
119
119
  self.translate_creater d, procs
120
120
  },
121
121
  integer:->( d, format ){
122
122
  procs = (format == :yml) ?
123
- [:nullstr, :blank2num, :bool2num, proc { |d| d.to_i } ] :
123
+ [:nullstr, :blank2num, :bool2num, proc { |d| d.to_i } ] :
124
124
  [:null2blankstr, :bool2num, proc { |d| d.to_i } ]
125
125
  self.translate_creater d, procs
126
126
  },
@@ -169,7 +169,7 @@ module Flextures
169
169
  # @params klass dump table Model
170
170
  # @params [Hash] options dump options
171
171
  # @return [Array] columns format information
172
- def self.dump_attributes klass, options
172
+ def self.dump_attributes( klass, options )
173
173
  columns = klass.columns.map { |column| { name: column.name, type: column.type } }
174
174
  # option[:minus] colum is delete columns
175
175
  columns.reject! { |column| options[:minus].include?(column[:name]) } if options[:minus]
@@ -185,7 +185,7 @@ module Flextures
185
185
  # @params [Hash] options options
186
186
  # @params [Symbol] type format type (:yml or :csv)
187
187
  # @return [Proc] filter function
188
- def self.create_filter attr_type, format, type
188
+ def self.create_filter( attr_type, format, type )
189
189
  filter = DumpFilter[format[:table].to_s.to_sym] || {}
190
190
  ->(row) {
191
191
  attr_type.map do |h|
@@ -198,7 +198,7 @@ module Flextures
198
198
  # data dump to csv format
199
199
  # @params [Hash] format file format data
200
200
  # @params [Hash] options dump format options
201
- def self.csv format
201
+ def self.csv( format )
202
202
  klass = PARENT.create_model(format[:table])
203
203
  attr_type = self.dump_attributes klass, format
204
204
  filter = self.create_filter attr_type, format, :csv
@@ -206,7 +206,7 @@ module Flextures
206
206
  end
207
207
 
208
208
  # dump csv format data
209
- def self.dump_csv klass, attr_type, values_filter, format
209
+ def self.dump_csv( klass, attr_type, values_filter, format )
210
210
  # TODO: 拡張子は指定してもしなくても良いようにする
211
211
  file_name = format[:file] || format[:table]
212
212
  dir_name = File.join( Flextures::Config.fixture_dump_directory, format[:dir].to_s )
@@ -226,15 +226,15 @@ module Flextures
226
226
  # data dump to yaml format
227
227
  # @params [Hash] format file format data
228
228
  # @params [Hash] options dump format options
229
- def self.yml format
229
+ def self.yml( format )
230
230
  klass = PARENT::create_model(format[:table])
231
231
  attr_type = self.dump_attributes klass, format
232
232
  filter = self.create_filter attr_type, format, :yml
233
- self.dump_yml klass, filter, format
233
+ self.dump_yml( klass, filter, format )
234
234
  end
235
235
 
236
236
  # dump yml format data
237
- def self.dump_yml klass, values_filter, format
237
+ def self.dump_yml( klass, values_filter, format )
238
238
  # TODO: 拡張子は指定してもしなくても良いようにする
239
239
  table_name = format[:table]
240
240
  file_name = format[:file] || format[:table]
@@ -251,4 +251,3 @@ module Flextures
251
251
  end
252
252
  end
253
253
  end
254
-
@@ -10,7 +10,7 @@ module Flextures
10
10
  (self.methods - ::OpenStruct.new.methods)
11
11
  .select{ |name| name.to_s.match(/\w+=/) }
12
12
  .map{ |name| name.to_s.gsub(/=/,'').to_sym }
13
- .inject({}){ |k,h| h[k]=self.send(k); h }
13
+ .reduce({}){ |k,h| h[k]=self.send(k); h }
14
14
  end
15
15
  end
16
16
 
@@ -21,14 +21,12 @@ module Flextures
21
21
  # @return [Hash] tanslated Hash data
22
22
  # example:
23
23
  # hash = array.extend(Extensions::Array).to_hash(keys)
24
- def to_hash keys
24
+ def to_hash( keys )
25
25
  values = self
26
26
  values = values[0..keys.size-1] if keys.size < values.size
27
27
  values = values+[nil]*(keys.size-values.size) if keys.size > values.size
28
- [keys,values].transpose.inject({}){ |h,pair| k,v=pair; h[k]=v; h }
28
+ [keys,values].transpose.reduce({}){ |h,pair| k,v=pair; h[k]=v; h }
29
29
  end
30
30
  end
31
31
  end
32
32
  end
33
-
34
-
@@ -10,11 +10,11 @@ module Flextures
10
10
  # @params [String] table_name
11
11
  # @params [Array] options arguments ActiveRecord Model
12
12
  # @params [Proc] block FactoryFilter
13
- def self.define table_name, *options, &block
13
+ def self.define( table_name, *options, &block )
14
14
  h={ block: block }
15
15
  options.each do |o|
16
16
  begin
17
- h[:model] = o if o.new.is_a? ActiveRecord::Base
17
+ h[:model] = o if o.new.is_a?(ActiveRecord::Base)
18
18
  rescue
19
19
  end
20
20
  end
@@ -24,7 +24,7 @@ module Flextures
24
24
  # get FactoryFilter
25
25
  # @params [String|Symbol] table_name
26
26
  # @return [Proc] filter block
27
- def self.get table_name
27
+ def self.get( table_name )
28
28
  f = FACTORIES[table_name.to_sym]
29
29
  f && f[:block]
30
30
  end
@@ -40,16 +40,15 @@ module Flextures
40
40
  # @params options
41
41
  # @params block
42
42
  # @return Flextures::Factory
43
- def self.define table_name, hash
43
+ def self.define( table_name, hash )
44
44
  FACTORIES[table_name.to_sym]=hash
45
45
  end
46
46
 
47
47
  # get FactoryFilter
48
- def self.get table_name
48
+ def self.get( table_name )
49
49
  FACTORIES[table_name.to_sym]
50
50
  end
51
51
  def self.[](table_name); self.get(table_name); end
52
52
  end
53
53
  Factory = LoadFilter
54
54
  end
55
-