flextures 4.2.6 → 4.2.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,138 +1,138 @@
1
- # override setup_fixtures function
2
- module ActiveRecord
3
- module TestFixtures
4
- PARENT = self
5
- @@flextures_loader = Flextures::Loader.new
6
- @@all_cached_flextures = {}
7
- @@already_loaded_flextures = {}
8
-
9
- alias :setup_fixtures_bkup :setup_fixtures
10
- def setup_fixtures
11
- Flextures::load_configurations
12
- setup_fixtures_bkup
13
- end
14
-
15
- alias :teardown_fixtures_bkup :teardown_fixtures
16
- def teardown_fixtures
17
- teardown_fixtures_bkup
18
- end
19
-
20
- # load initial fixtures
21
- # There is fixtures load before start rspec
22
- def self.init_load_should_cache_fixtures(table_load_settings)
23
- table_load_settings.each do |load_setting|
24
- if should_cache_setting?(load_setting) and !cached_table?(load_setting)
25
- @@flextures_loader.load(load_setting)
26
- set_cached_settng_list(load_setting)
27
- end
28
- end
29
- end
30
-
31
- # Usually, fixture is cached when is exist under "spec/fixture/" directly.
32
- def self.should_cache_setting?(load_setting)
33
- load_setting.keys.sort == %i[table file loader].sort &&
34
- load_setting[:file].to_s == load_setting[:table].to_s &&
35
- load_setting[:loader] == :fun
36
- end
37
-
38
- # check: same data is exist in DB.
39
- def self.cached_table?(load_setting)
40
- flextures_cached?(load_setting) || fixture_cached?(load_setting)
41
- end
42
-
43
- def self.flextures_cached?(load_setting)
44
- config = @@all_cached_flextures[load_setting[:table]]
45
- config && config == load_setting
46
- end
47
-
48
- # flextures check fixture function already loaded data.
49
- def self.fixture_cached?(load_setting)
50
- default_file_path = File.join(Flextures::Configuration.load_directory, "#{load_setting[:table]}.yml")
51
-
52
- load_setting[:file] == default_file_path &&
53
- yml_fixture_cached?(load_setting[:table])
54
- end
55
-
56
- def self.yml_fixture_cached?(table_name)
57
- connection = ActiveRecord::Base.connection
58
- !!ActiveRecord::FixtureSet.fixture_is_cached?(connection, table_name)
59
- end
60
-
61
- def self.set_cached_settng_list(load_setting)
62
- @@all_cached_flextures[load_setting[:table]] = load_setting
63
- end
64
-
65
- def load_not_cached_fixtures(table_load_settings)
66
- table_load_settings.each do |load_setting|
67
- if PARENT.cached_table?(load_setting) and load_setting[:cache] != false
68
- next
69
- else
70
- @@flextures_loader.load(load_setting)
71
- end
72
- end
73
- end
74
-
75
- def load_all_fixtures(table_load_settings)
76
- table_load_settings.each do |load_setting|
77
- @@flextures_loader.load(load_setting)
78
- end
79
- end
80
-
81
- module ClassMethods
82
- def get_or_initialize_flextures_loader_options
83
- @flextures_loader_options ||= {}
84
- end
85
-
86
- def flextures_loader_options
87
- get_or_initialize_flextures_loader_options
88
- end
89
-
90
- def flextures_loader
91
- PARENT.class_variable_get(:@@flextures_loader)
92
- end
93
-
94
- def flextures(*fixtures)
95
- loads_use_cache_fixtures(*fixtures)
96
- end
97
-
98
- def loads_use_cache_fixtures(*fixtures)
99
- table_load_settings = Flextures::Loader.parse_flextures_options(flextures_loader_options, *fixtures)
100
-
101
- if (respond_to?(:use_transactional_fixtures) && use_transactional_fixtures) || (respond_to?(:use_transactional_tests) && use_transactional_tests)
102
- PARENT.init_load_should_cache_fixtures(table_load_settings)
103
- before do
104
- load_not_cached_fixtures(table_load_settings)
105
- end
106
- else
107
- before do
108
- load_all_fixtures(table_load_settings)
109
- end
110
- end
111
- end
112
-
113
- # delete table data
114
- # @params [Array] _ table names
115
- def flextures_delete(*_)
116
- before do
117
- if _.empty?
118
- Flextures::init_tables
119
- else
120
- Flextures::delete_tables(*_)
121
- end
122
- end
123
- end
124
-
125
- def flextures_set_options(options)
126
- get_or_initialize_flextures_loader_options.merge!(options)
127
- end
128
-
129
- def flextures_instance
130
- self
131
- end
132
-
133
- def flextures_options
134
- @flextures_loader_options
135
- end
136
- end
137
- end
138
- end
1
+ # override setup_fixtures function
2
+ module ActiveRecord
3
+ module TestFixtures
4
+ PARENT = self
5
+ @@flextures_loader = Flextures::Loader.new
6
+ @@all_cached_flextures = {}
7
+ @@already_loaded_flextures = {}
8
+
9
+ alias :setup_fixtures_bkup :setup_fixtures
10
+ def setup_fixtures
11
+ Flextures::load_configurations
12
+ setup_fixtures_bkup
13
+ end
14
+
15
+ alias :teardown_fixtures_bkup :teardown_fixtures
16
+ def teardown_fixtures
17
+ teardown_fixtures_bkup
18
+ end
19
+
20
+ # load initial fixtures
21
+ # There is fixtures load before start rspec
22
+ def self.init_load_should_cache_fixtures(table_load_settings)
23
+ table_load_settings.each do |load_setting|
24
+ if should_cache_setting?(load_setting) and !cached_table?(load_setting)
25
+ @@flextures_loader.load(load_setting)
26
+ set_cached_settng_list(load_setting)
27
+ end
28
+ end
29
+ end
30
+
31
+ # Usually, fixture is cached when is exist under "spec/fixture/" directly.
32
+ def self.should_cache_setting?(load_setting)
33
+ load_setting.keys.sort == %i[table file loader].sort &&
34
+ load_setting[:file].to_s == load_setting[:table].to_s &&
35
+ load_setting[:loader] == :fun
36
+ end
37
+
38
+ # check: same data is exist in DB.
39
+ def self.cached_table?(load_setting)
40
+ flextures_cached?(load_setting) || fixture_cached?(load_setting)
41
+ end
42
+
43
+ def self.flextures_cached?(load_setting)
44
+ config = @@all_cached_flextures[load_setting[:table]]
45
+ config && config == load_setting
46
+ end
47
+
48
+ # flextures check fixture function already loaded data.
49
+ def self.fixture_cached?(load_setting)
50
+ default_file_path = File.join(Flextures::Configuration.load_directory, "#{load_setting[:table]}.yml")
51
+
52
+ load_setting[:file] == default_file_path &&
53
+ yml_fixture_cached?(load_setting[:table])
54
+ end
55
+
56
+ def self.yml_fixture_cached?(table_name)
57
+ connection = ActiveRecord::Base.connection
58
+ !!ActiveRecord::FixtureSet.fixture_is_cached?(connection, table_name)
59
+ end
60
+
61
+ def self.set_cached_settng_list(load_setting)
62
+ @@all_cached_flextures[load_setting[:table]] = load_setting
63
+ end
64
+
65
+ def load_not_cached_fixtures(table_load_settings)
66
+ table_load_settings.each do |load_setting|
67
+ if PARENT.cached_table?(load_setting) and load_setting[:cache] != false
68
+ next
69
+ else
70
+ @@flextures_loader.load(load_setting)
71
+ end
72
+ end
73
+ end
74
+
75
+ def load_all_fixtures(table_load_settings)
76
+ table_load_settings.each do |load_setting|
77
+ @@flextures_loader.load(load_setting)
78
+ end
79
+ end
80
+
81
+ module ClassMethods
82
+ def get_or_initialize_flextures_loader_options
83
+ @flextures_loader_options ||= {}
84
+ end
85
+
86
+ def flextures_loader_options
87
+ get_or_initialize_flextures_loader_options
88
+ end
89
+
90
+ def flextures_loader
91
+ PARENT.class_variable_get(:@@flextures_loader)
92
+ end
93
+
94
+ def flextures(*fixtures)
95
+ loads_use_cache_fixtures(*fixtures)
96
+ end
97
+
98
+ def loads_use_cache_fixtures(*fixtures)
99
+ table_load_settings = Flextures::Loader.parse_flextures_options(flextures_loader_options, *fixtures)
100
+
101
+ if (respond_to?(:use_transactional_fixtures) && use_transactional_fixtures) || (respond_to?(:use_transactional_tests) && use_transactional_tests)
102
+ PARENT.init_load_should_cache_fixtures(table_load_settings)
103
+ before do
104
+ load_not_cached_fixtures(table_load_settings)
105
+ end
106
+ else
107
+ before do
108
+ load_all_fixtures(table_load_settings)
109
+ end
110
+ end
111
+ end
112
+
113
+ # delete table data
114
+ # @params [Array] _ table names
115
+ def flextures_delete(*_)
116
+ before do
117
+ if _.empty?
118
+ Flextures::init_tables
119
+ else
120
+ Flextures::delete_tables(*_)
121
+ end
122
+ end
123
+ end
124
+
125
+ def flextures_set_options(options)
126
+ get_or_initialize_flextures_loader_options.merge!(options)
127
+ end
128
+
129
+ def flextures_instance
130
+ self
131
+ end
132
+
133
+ def flextures_options
134
+ @flextures_loader_options
135
+ end
136
+ end
137
+ end
138
+ end
@@ -1,20 +1,20 @@
1
- require 'flextures/flextures'
2
-
3
- namespace :db do
4
- namespace :flextures do
5
- desc "Dump data to csv format"
6
- task :dump => :environment do
7
- Flextures::Rake::Command::dump
8
- end
9
-
10
- desc "load fixture data csv format"
11
- task :load => :environment do
12
- Flextures::Rake::Command::load
13
- end
14
-
15
- desc "load and dump file (replace) new data file"
16
- task :generate => :environment do
17
- Flextures::Rake::Command::generate
18
- end
19
- end
20
- end
1
+ require 'flextures/flextures'
2
+
3
+ namespace :db do
4
+ namespace :flextures do
5
+ desc "Dump data to csv format"
6
+ task :dump => :environment do
7
+ Flextures::Rake::Command::dump
8
+ end
9
+
10
+ desc "load fixture data csv format"
11
+ task :load => :environment do
12
+ Flextures::Rake::Command::load
13
+ end
14
+
15
+ desc "load and dump file (replace) new data file"
16
+ task :generate => :environment do
17
+ Flextures::Rake::Command::generate
18
+ end
19
+ end
20
+ end
@@ -25,6 +25,21 @@ module Flextures
25
25
  end
26
26
  using ArrayEx
27
27
 
28
+ module TableColumnEx
29
+ refine ActiveRecord::ConnectionAdapters::Column do
30
+ def translater(klass)
31
+ type_name = klass.defined_enums[name.to_s] ? :enum : type
32
+ TRANSLATER[type_name]
33
+ end
34
+
35
+ def completer(klass)
36
+ type_name = klass.defined_enums[name.to_s] ? :enum : type
37
+ COMPLETER[type_name]
38
+ end
39
+ end
40
+ end
41
+ using TableColumnEx
42
+
28
43
  PARENT = Flextures
29
44
  FORMATS = [ %i[csv erb], %i[erb csv], %i[csv], %i[yml erb], %i[erb yml], %i[yml] ]
30
45
 
@@ -93,6 +108,10 @@ module Flextures
93
108
  return nil if d==""
94
109
  DateTime.parse(d.to_s)
95
110
  },
111
+ enum:->(v){
112
+ return v.to_i if v.match(/^\d+$/)
113
+ v
114
+ }
96
115
  }
97
116
 
98
117
  def initialize(*_)
@@ -353,18 +372,23 @@ module Flextures
353
372
  # @return [Proc] translate filter
354
373
  def self.create_filter(klass, factory, filename, ext, options)
355
374
  columns = klass.columns
356
- # data translat array to hash
375
+ # data translate array to hash
357
376
  column_hash = columns.reduce({}) { |h,col| h[col.name] = col; h }
358
- lack_columns = columns.reject { |c| c.null and c.default }.map{ |o| o.name.to_sym }
359
- # default value shound not be null columns
360
- not_nullable_columns = columns.reject(&:null).map(&:name)
377
+ translaters = column_hash.reduce({}){ |h,(k,col)| h[k] = col.translater(klass); h }
361
378
  strict_filter = ->(o,h){
362
379
  # if value is not 'nil', value translate suitable form
363
- h.each{ |k,v| v.nil? || o[k] = (TRANSLATER[column_hash[k].type] && TRANSLATER[column_hash[k].type].call(v)) }
380
+ h.each{ |k,v| v.nil? || o[k] = translaters[k]&.call(v) }
364
381
  # call FactoryFilter
365
382
  factory.call(*[o, filename, ext][0, factory.arity]) if factory and !options[:unfilter]
366
383
  o
367
384
  }
385
+
386
+ return strict_filter if options[:strict]==true
387
+
388
+ lack_columns = columns.reject { |c| c.null and c.default }.map{ |o| o.name.to_sym }
389
+ # default value shound not be null columns
390
+ not_nullable_columns = columns.reject(&:null).map(&:name)
391
+ completers = column_hash.reduce({}){ |h,(k,col)| h[k] = col.completer(klass); h }
368
392
  # receives hased data and translate ActiveRecord Model data
369
393
  # loose filter correct error values
370
394
  # strict filter don't correct errora values and raise error
@@ -374,12 +398,11 @@ module Flextures
374
398
  h.select! { |k,v| column_hash[k] }
375
399
  strict_filter.call(o,h)
376
400
  # set default value if value is 'nil'
377
- not_nullable_columns.each { |k| o[k].nil? && o[k] = (column_hash[k] && COMPLETER[column_hash[k].type] && COMPLETER[column_hash[k].type].call) }
401
+ not_nullable_columns.each { |k| o[k].nil? && o[k] = completers[k]&.call }
378
402
  # fill span values if column is not exist
379
- lack_columns.each { |k| o[k].nil? && o[k] = (column_hash[k] && COMPLETER[column_hash[k].type] && COMPLETER[column_hash[k].type].call) }
403
+ lack_columns.each { |k| o[k].nil? && o[k] = completers[k]&.call }
380
404
  o
381
405
  }
382
- (options[:strict]==true) ? strict_filter : loose_filter
383
406
  end
384
407
  end
385
408
  end
@@ -1,3 +1,3 @@
1
- module Flextures
2
- VERSION="4.2.6".freeze
3
- end
1
+ module Flextures
2
+ VERSION="4.2.7".freeze
3
+ end
@@ -1,17 +1,17 @@
1
- module Flextures
2
- module Generators
3
- class InitializerGenerator < Rails::Generators::Base
4
- source_root File.expand_path("../templates", __FILE__)
5
-
6
- def create_initializer_file
7
- copy_file "flextures.rb", "config/initializers/flextures.rb"
8
- copy_file "flextures.factory.rb", "config/flextures.factory.rb"
9
- end
10
-
11
- desc <<-MSG
12
- Description:
13
- Creates flextures configuration files.
14
- MSG
15
- end
16
- end
17
- end
1
+ module Flextures
2
+ module Generators
3
+ class InitializerGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ def create_initializer_file
7
+ copy_file "flextures.rb", "config/initializers/flextures.rb"
8
+ copy_file "flextures.factory.rb", "config/flextures.factory.rb"
9
+ end
10
+
11
+ desc <<-MSG
12
+ Description:
13
+ Creates flextures configuration files.
14
+ MSG
15
+ end
16
+ end
17
+ end
@@ -1,8 +1,8 @@
1
- # Flextures::Factory.define :users do |f|
2
- # f.password = "hogehoge"
3
- # f
4
- # end
5
-
6
- # Flextures::DumpFilter.define :users, {
7
- # :encrypted_password => lambda { |v| Base64.encode64(v) }
8
- # }
1
+ # Flextures::Factory.define :users do |f|
2
+ # f.password = "hogehoge"
3
+ # f
4
+ # end
5
+
6
+ # Flextures::DumpFilter.define :users, {
7
+ # :encrypted_password => lambda { |v| Base64.encode64(v) }
8
+ # }