activerecord-bixformer 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 00e9b24b92407ae36d6e54ec0baea46b42b8d6dd
4
- data.tar.gz: 78ab0a18aa7407a29ec2e367d325cacf38ccb7e2
3
+ metadata.gz: f38f07d98e1c2f686db88cf14e8f0aa440df98d8
4
+ data.tar.gz: a81dfe5886c71ead977d3d66cdde5119cb1cb97b
5
5
  SHA512:
6
- metadata.gz: 9711f06d78d4f34e51f6aa606dcd354ec35ecbf19788cf901510c64c45cfb7de06816bf293cb3ecdfa0cdc17b46f40a1e6c7311a372a6fe9e7d77142345a39d3
7
- data.tar.gz: b60e586431292a13c98ae4c74579552e86eb6d6d6b900cfaebedcaa2f04808aeac7ceba0d25a15f41310b5805eef44f25179183b9797d3f4c2154409e53a4c9d
6
+ metadata.gz: 9642616d5126bd1a85366204e489ee197a1dc3bd9fb505fa7f97130d223524ba97fceb0b0ed7e4ca8ef0702a8d15120297d93f2e300d784869e84dcb09782e34
7
+ data.tar.gz: 8cbfd3443e100e47fc7407d594fb8b47ac876eeb95c1f3a6314bfa331352768d7a1a0c02ac8a0fed09dab5f571d0b4f2d32dadf65e3b9c85edabdb33b14f5402
@@ -7,15 +7,15 @@ module ActiveRecord
7
7
  def initialize(model, attribute_name, options)
8
8
  @model = model
9
9
  @name = attribute_name
10
- @options = options
10
+ @options = (options.is_a?(::Hash) ? options : {}).with_indifferent_access
11
11
  end
12
12
 
13
- def make_export_value(active_record_value)
14
- active_record_value.to_s
13
+ def export(activerecord_value)
14
+ activerecord_value
15
15
  end
16
16
 
17
- def make_import_value(value)
18
- value.presence
17
+ def import(value)
18
+ value
19
19
  end
20
20
  end
21
21
  end
@@ -2,14 +2,14 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Boolean < ::ActiveRecord::Bixformer::Attribute::Base
5
- def make_export_value(active_record_value)
5
+ def export(activerecord_value)
6
6
  true_value = (@options.is_a?(::Hash) && @options[:true]) || 'true'
7
7
  false_value = (@options.is_a?(::Hash) && @options[:false]) || 'false'
8
8
 
9
- active_record_value.present? ? true_value : false_value
9
+ activerecord_value.present? ? true_value : false_value
10
10
  end
11
11
 
12
- def make_import_value(value)
12
+ def import(value)
13
13
  true_value = (@options.is_a?(::Hash) && @options[:true]) || 'true'
14
14
  false_value = (@options.is_a?(::Hash) && @options[:false]) || 'false'
15
15
 
@@ -2,13 +2,13 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Booletania < ::ActiveRecord::Bixformer::Attribute::Base
5
- def make_export_value(active_record_value)
5
+ def export(activerecord_value)
6
6
  @model.activerecord_constant.__send__("#{@name}_options").find do |text, bool|
7
- bool == active_record_value
7
+ bool == activerecord_value
8
8
  end&.first
9
9
  end
10
10
 
11
- def make_import_value(value)
11
+ def import(value)
12
12
  return nil unless value
13
13
 
14
14
  @model.activerecord_constant.__send__("#{@name}_options").to_h[value.strip]
@@ -2,13 +2,13 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Date < ::ActiveRecord::Bixformer::Attribute::Base
5
- def make_export_value(active_record_value)
6
- return nil unless active_record_value
5
+ def export(activerecord_value)
6
+ return nil unless activerecord_value
7
7
 
8
- active_record_value.to_s(option_format)
8
+ activerecord_value.to_s(option_format)
9
9
  end
10
10
 
11
- def make_import_value(value)
11
+ def import(value)
12
12
  return nil if value.blank?
13
13
 
14
14
  begin
@@ -2,15 +2,15 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Enumerize < ::ActiveRecord::Bixformer::Attribute::Base
5
- def make_export_value(active_record_value)
6
- active_record_value = active_record_value.to_s
5
+ def export(activerecord_value)
6
+ activerecord_value = activerecord_value.to_s
7
7
 
8
8
  @model.activerecord_constant.__send__(@name).options.find do |text, key|
9
- key == active_record_value
9
+ key == activerecord_value
10
10
  end&.first
11
11
  end
12
12
 
13
- def make_import_value(value)
13
+ def import(value)
14
14
  return nil if value.blank?
15
15
 
16
16
  @model.activerecord_constant.__send__(@name).options.to_h[value.strip] or
@@ -2,11 +2,11 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Override < ::ActiveRecord::Bixformer::Attribute::Base
5
- def make_export_value(active_record_value)
6
- @model.__send__("override_export_#{@name}", active_record_value)
5
+ def export(activerecord_value)
6
+ @model.__send__("override_export_#{@name}", activerecord_value)
7
7
  end
8
8
 
9
- def make_import_value(value)
9
+ def import(value)
10
10
  @model.__send__("override_import_#{@name}", value)
11
11
  end
12
12
  end
@@ -0,0 +1,15 @@
1
+ module ActiveRecord
2
+ module Bixformer
3
+ module Attribute
4
+ class String < ::ActiveRecord::Bixformer::Attribute::Base
5
+ def export(activerecord_value)
6
+ activerecord_value.to_s
7
+ end
8
+
9
+ def import(value)
10
+ value.to_s.strip.presence
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -2,13 +2,13 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Time < ::ActiveRecord::Bixformer::Attribute::Base
5
- def make_export_value(active_record_value)
6
- return nil unless active_record_value
5
+ def export(activerecord_value)
6
+ return nil unless activerecord_value
7
7
 
8
- active_record_value.to_s(option_format)
8
+ activerecord_value.to_s(option_format)
9
9
  end
10
10
 
11
- def make_import_value(value)
11
+ def import(value)
12
12
  return nil if value.blank?
13
13
 
14
14
  begin
@@ -13,6 +13,7 @@ module ActiveRecord
13
13
  autoload :Date, 'activerecord-bixformer/attribute/date'
14
14
  autoload :Enumerize, 'activerecord-bixformer/attribute/enumerize'
15
15
  autoload :Override, 'activerecord-bixformer/attribute/override'
16
+ autoload :String, 'activerecord-bixformer/attribute/string'
16
17
  autoload :Time, 'activerecord-bixformer/attribute/time'
17
18
  end
18
19
 
@@ -7,7 +7,7 @@ module ActiveRecord
7
7
  end
8
8
 
9
9
  def assignable_attributes(csv_row)
10
- compile.make_import_value(csv_row)
10
+ compile.import(csv_row)
11
11
  end
12
12
  end
13
13
  end
@@ -17,15 +17,12 @@ module ActiveRecord
17
17
  class Base
18
18
  include ::ActiveRecord::Bixformer::ImportValueValidatable
19
19
 
20
- attr_reader :name, :parent,
21
- :attributes, :associations,
22
- :optional_attributes,
23
- :translator,
24
- :options
20
+ attr_reader :name, :options, :parent, :attributes, :associations,
21
+ :optional_attributes, :translator
25
22
 
26
23
  def initialize(model_or_association_name, options)
27
24
  @name = model_or_association_name.to_s
28
- @options = options
25
+ @options = (options.is_a?(::Hash) ? options : {}).with_indifferent_access
29
26
  @associations = []
30
27
  end
31
28
 
@@ -89,6 +86,24 @@ module ActiveRecord
89
86
  activerecord_constant.find_by!(condition)
90
87
  end
91
88
 
89
+ def export(activerecord_or_activerecords)
90
+ # has_one でしか使わない想定なので activerecord_or_activerecords は ActiveRecord::Base のはず
91
+ values = @attributes.map do |attr|
92
+ value_reader = attr.options[:reader] || attr.name
93
+ attribute_value = activerecord_or_activerecords && activerecord_or_activerecords.__send__(value_reader)
94
+
95
+ [csv_title(attr.name), attr.export(attribute_value)]
96
+ end.to_h.with_indifferent_access
97
+
98
+ @associations.inject(values) do |each_values, association|
99
+ association_value = activerecord_or_activerecords && activerecord_or_activerecords.__send__(association.name)
100
+
101
+ association_value = association_value.to_a if association_value.is_a?(::ActiveRecord::Relation)
102
+
103
+ each_values.merge(association.export(association_value))
104
+ end
105
+ end
106
+
92
107
  private
93
108
 
94
109
  def make_each_attribute_import_value(parent_activerecord_id = nil, &block)
@@ -3,32 +3,15 @@ module ActiveRecord
3
3
  module Model
4
4
  module Csv
5
5
  class Base < ::ActiveRecord::Bixformer::Model::Base
6
- def make_export_value(activerecord_or_activerecords)
7
- # has_one でしか使わない想定なので activerecord_or_activerecords は ActiveRecord::Base のはず
8
- values = @attributes.map do |attr|
9
- attribute_value = activerecord_or_activerecords && activerecord_or_activerecords.__send__(attr.name)
10
-
11
- [csv_title(attr.name), attr.make_export_value(attribute_value)]
12
- end.to_h.with_indifferent_access
13
-
14
- @associations.inject(values) do |each_values, association|
15
- association_value = activerecord_or_activerecords && activerecord_or_activerecords.__send__(association.name)
16
-
17
- association_value = association_value.to_a if association_value.is_a?(::ActiveRecord::Relation)
18
-
19
- each_values.merge(association.make_export_value(association_value))
20
- end
21
- end
22
-
23
- def make_import_value(csv_row, parent_activerecord_id = nil)
6
+ def import(csv_row, parent_activerecord_id = nil)
24
7
  values = make_each_attribute_import_value(parent_activerecord_id) do |attr|
25
8
  csv_value = csv_row[csv_title(attr.name)]
26
9
 
27
- attr.make_import_value(csv_value)
10
+ attr.import(csv_value)
28
11
  end
29
12
 
30
13
  make_each_association_import_value(values) do |association, self_activerecord_id|
31
- association.make_import_value(csv_row, self_activerecord_id)
14
+ association.import(csv_row, self_activerecord_id)
32
15
  end
33
16
  end
34
17
 
@@ -6,12 +6,10 @@ module ActiveRecord
6
6
  def initialize(model_or_association_name, options)
7
7
  super
8
8
 
9
- @options = options.is_a?(::Hash) ? options : {}
10
-
11
9
  @options[:size] ||= 1
12
10
  end
13
11
 
14
- def make_export_value(activerecord_or_activerecords)
12
+ def export(activerecord_or_activerecords)
15
13
  activerecord_or_activerecords ||= []
16
14
 
17
15
  # has_many でしか使わない想定なので activerecord_or_activerecords は Array のはず
@@ -22,7 +20,7 @@ module ActiveRecord
22
20
  end
23
21
  end
24
22
 
25
- def make_import_value(csv_row, parent_activerecord_id = nil)
23
+ def import(csv_row, parent_activerecord_id = nil)
26
24
  # has_many でしか使わない想定なので Array を返却
27
25
  (1..options[:size]).map do |index|
28
26
  update_translator(index)
@@ -7,8 +7,22 @@ module ActiveRecord
7
7
  attr_accessor :__bixformer_format
8
8
 
9
9
  class_attribute :__bixformer_model
10
-
11
10
  class_attribute :__bixformer_namespace
11
+ class_attribute :__bixformer_entry
12
+ class_attribute :__bixformer_optional_attributes
13
+ class_attribute :__bixformer_required_attributes
14
+ class_attribute :__bixformer_unique_indexes
15
+ class_attribute :__bixformer_required_condition
16
+ class_attribute :__bixformer_default_values
17
+ class_attribute :__bixformer_translation_config
18
+
19
+ self.__bixformer_entry = {}
20
+ self.__bixformer_optional_attributes = []
21
+ self.__bixformer_required_attributes = []
22
+ self.__bixformer_unique_indexes = []
23
+ self.__bixformer_required_condition = {}
24
+ self.__bixformer_default_values = {}
25
+ self.__bixformer_translation_config = { scope: :bixformer, extend_scopes: [] }
12
26
  end
13
27
 
14
28
  module ClassMethods
@@ -19,37 +33,15 @@ module ActiveRecord
19
33
  def bixformer_load_namespace(namespace)
20
34
  self.__bixformer_namespace = namespace
21
35
  end
22
- end
23
-
24
- def entry
25
- {}
26
- end
27
-
28
- def optional_attributes
29
- []
30
- end
31
-
32
- def required_attributes
33
- []
34
- end
35
36
 
36
- def unique_indexes
37
- []
38
- end
39
-
40
- def required_condition
41
- {}
42
- end
43
-
44
- def default_values
45
- {}
46
- end
47
-
48
- def translation_config
49
- {
50
- scope: :bixformer,
51
- extend_scopes: []
52
- }
37
+ [
38
+ :entry, :optional_attributes, :required_attributes, :unique_indexes,
39
+ :required_condition, :default_values, :translation_config
40
+ ].each do |config_name|
41
+ define_method "bixformer_#{config_name}" do |v|
42
+ self.__send__("__bixformer_#{config_name}=", v)
43
+ end
44
+ end
53
45
  end
54
46
  end
55
47
  end
@@ -12,14 +12,14 @@ module ActiveRecord
12
12
  end
13
13
 
14
14
  def value_of(config_name)
15
- @plan.__send__(config_name).dup
15
+ compile_config_value(config_name).dup
16
16
  end
17
17
 
18
18
  def pickup_value_for(model, config_name, default_value = nil)
19
19
  model_names_without_root = (model.parents.map(&:name) + [model.name]).drop(1)
20
20
 
21
21
  # 指定された設定の全設定値を取得
22
- entire_config_value = @plan.__send__(config_name)
22
+ entire_config_value = compile_config_value(config_name)
23
23
 
24
24
  if entire_config_value.is_a?(::Hash)
25
25
  # Hashなら、with_indifferent_accessしておく
@@ -40,7 +40,7 @@ module ActiveRecord
40
40
 
41
41
  if config_value.is_a?(::Array)
42
42
  # Arrayで最後の要素がHashの場合、それは子要素の設定値なので、結果に含めない
43
- config_value.pop if config_value.last.is_a?(::Hash)
43
+ config_value.extract_options!
44
44
 
45
45
  # Arrayなら、要素は文字列化しておく
46
46
  config_value = config_value.map { |v| v.to_s }
@@ -98,6 +98,18 @@ module ActiveRecord
98
98
 
99
99
  private
100
100
 
101
+ def compile_config_value(config_name)
102
+ v = @plan.class.__send__("__bixformer_#{config_name}")
103
+
104
+ if v.is_a?(::Proc)
105
+ v.call
106
+ elsif v.is_a?(::Symbol) || v.is_a?(::String)
107
+ @plan.__send__(v)
108
+ else
109
+ v
110
+ end
111
+ end
112
+
101
113
  def find_nested_config_value(config, keys)
102
114
  return config ? config.dup : nil if keys.empty?
103
115
 
@@ -12,7 +12,7 @@ module ActiveRecord
12
12
 
13
13
  def csv_body_row(activerecord)
14
14
  model = compile
15
- body_map = model.make_export_value(activerecord)
15
+ body_map = model.export(activerecord)
16
16
 
17
17
  model.csv_titles.map { |title| body_map[title] }
18
18
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Bixformer
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-bixformer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroaki Otsu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-16 00:00:00.000000000 Z
11
+ date: 2016-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -229,6 +229,7 @@ files:
229
229
  - lib/activerecord-bixformer/attribute/date.rb
230
230
  - lib/activerecord-bixformer/attribute/enumerize.rb
231
231
  - lib/activerecord-bixformer/attribute/override.rb
232
+ - lib/activerecord-bixformer/attribute/string.rb
232
233
  - lib/activerecord-bixformer/attribute/time.rb
233
234
  - lib/activerecord-bixformer/autoload.rb
234
235
  - lib/activerecord-bixformer/compiler.rb