activerecord-bixformer 0.2.3 → 0.2.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a3cf0d6744f510047d870eed824bb57d7646fbf8
4
- data.tar.gz: cee67069dc05abc816ca590bfd10ca1ecee7f710
3
+ metadata.gz: 18bbd017fde0b78cc6edbcbea84f68cf62088a48
4
+ data.tar.gz: 7560282ebbd8bc2474a2b91534a76ec453ff4a53
5
5
  SHA512:
6
- metadata.gz: 3d5df8e0c7258482caf4ffbb3e71d5f168f03034eb03459c348e4121722f6b8d1ec1a189c87cc8c27cb443c7d4320b9081612bb2b17514f1d22809c19f491f61
7
- data.tar.gz: 818e84c966120c820eebec7f3838a41f8828f934b5cc30c73c7aefebd00430332e070e632d273d4f3161d16ea841f582d45ddc7c5e7f42c6d5aa1ef2c0fd93cd
6
+ metadata.gz: 546d0e273f0e30697b54b5b30cb200f21ab7d36834db166fb4faa777f2eb3b175bf595fe6a4e997669e3233fef5aad4ac60cecfa980412bbfe6126783ddc47c5
7
+ data.tar.gz: fa1063540a8d61e068c74f4af2a7e8b6be2ead2034f02686266fd187339302257d1fc06e241251d850dd2fbb85e7f30f72fa6e6c9f9be6fb8a67044ad4844529
data/README-ja.md CHANGED
@@ -104,7 +104,7 @@ ActiveRecord::Bixformer::Plan::Base を継承して、以下のメソッドを
104
104
  }
105
105
  ```
106
106
 
107
- ### optional_attributes
107
+ ### preferred_skip_attributes
108
108
 
109
109
  インポート時に、有効な値でない場合に、登録対象としない属性を定義した以下のような配列を返して下さい。
110
110
 
@@ -148,14 +148,14 @@ end
148
148
  ### required_attributes
149
149
 
150
150
  インポート時に、有効な値でない場合に、インポート自体を行わない属性を定義した配列を返して下さい。
151
- データ構成は、 `optional_attributes` と同様です。
151
+ データ構成は、 `preferred_skip_attributes` と同様です。
152
152
 
153
- ### unique_indexes
153
+ ### unique_attributes
154
154
 
155
155
  インポートは、対象の ActiveRecord モデルの `primary_key` に対応するインポートデータの有無によって、
156
156
  追加か更新かを判定しますが、 `primary_key` の属性がインポートデータに含まれていない場合でも、
157
157
  更新処理を行いたい場合に、対象レコードを特定できる属性を定義した配列を返して下さい。
158
- データ構成は、 `optional_attributes` と同様です。
158
+ データ構成は、 `preferred_skip_attributes` と同様です。
159
159
 
160
160
  ```ruby
161
161
  [
@@ -181,7 +181,7 @@ end
181
181
  }
182
182
  ```
183
183
 
184
- * `primary_key` や `unique_indexes` が指定されている場合のデータベース検索の条件に追加されます
184
+ * `primary_key` や `unique_attributes` が指定されている場合のデータベース検索の条件に追加されます
185
185
  * 関連モデルの場合は、親レコードの foreign_key が代わりに使用されます
186
186
  * `primary_key` が指定されているのに、データベース検索に失敗した場合には、 `ActiveRecord::RecordNotFound` 例外が raise されます
187
187
 
@@ -3,10 +3,10 @@ module ActiveRecord
3
3
  class AssignableAttributesNormalizer
4
4
  include ::ActiveRecord::Bixformer::ImportValueValidatable
5
5
 
6
- def initialize(plan, model, parent_activerecord_id)
6
+ def initialize(plan, model, parent_record_id)
7
7
  @plan = ActiveRecord::Bixformer::PlanAccessor.new(plan)
8
8
  @model = model
9
- @parent_activerecord_id = parent_activerecord_id
9
+ @parent_record_id = parent_record_id
10
10
  @identified_column_name = @model.activerecord_constant.primary_key
11
11
  end
12
12
 
@@ -47,9 +47,9 @@ module ActiveRecord
47
47
  # 設定するのは親がいる場合のみ
48
48
  return unless @model.parent
49
49
 
50
- if @parent_activerecord_id
50
+ if @parent_record_id
51
51
  # 親のレコードが見つかっているなら、それも結果ハッシュに追加する
52
- @model_attributes[@model.parent_foreign_key] = @parent_activerecord_id
52
+ @model_attributes[@model.parent_foreign_key] = @parent_record_id
53
53
  else
54
54
  # 見つかっていないなら、間違った値が指定されている可能性があるので、キー自体を削除
55
55
  @model_attributes.delete(@model.parent_foreign_key)
@@ -58,7 +58,7 @@ module ActiveRecord
58
58
 
59
59
  def set_identified_attribute
60
60
  # 更新の場合は、インポートデータを元にデータベースから対象のレコードを検索してIDを取得
61
- verified_id = verified_activerecord_id
61
+ verified_id = verified_record_id
62
62
 
63
63
  if verified_id
64
64
  # 更新なら、ID属性を改めて設定
@@ -69,7 +69,7 @@ module ActiveRecord
69
69
  end
70
70
  end
71
71
 
72
- def verified_activerecord_id
72
+ def verified_record_id
73
73
  # 更新対象のレコードを特定できるかチェック
74
74
  identified_value = @model_attributes[@identified_column_name]
75
75
 
@@ -99,19 +99,19 @@ module ActiveRecord
99
99
  # 検証条件とマージして、データベースに登録されているか確認する
100
100
  verified_condition = uniqueness_condition.merge(required_condition)
101
101
 
102
- @model.find_activerecord_by!(verified_condition).__send__(@identified_column_name)
102
+ @model.find_record_by!(verified_condition).__send__(@identified_column_name)
103
103
  rescue ::ActiveRecord::RecordNotFound => e
104
104
  # ID属性が指定されているのに、データベースに見つからない場合はエラーにする
105
105
  raise e if identified_value
106
106
  end
107
107
 
108
108
  def find_unique_condition
109
- unique_indexes = @plan.pickup_value_for(@model, :unique_indexes, [])
109
+ unique_attributes = @plan.pickup_value_for(@model, :unique_attributes, [])
110
110
 
111
111
  # ユニーク条件が指定されていないなら終了
112
- return nil if unique_indexes.empty?
112
+ return nil if unique_attributes.empty?
113
113
 
114
- unique_condition = unique_indexes.map do |key|
114
+ unique_condition = unique_attributes.map do |key|
115
115
  [key, @model_attributes[key]]
116
116
  end.to_h
117
117
 
@@ -10,8 +10,8 @@ module ActiveRecord
10
10
  @options = (options.is_a?(::Hash) ? options : {}).with_indifferent_access
11
11
  end
12
12
 
13
- def export(activerecord_value)
14
- activerecord_value
13
+ def export(record_attribute_value)
14
+ record_attribute_value
15
15
  end
16
16
 
17
17
  def import(value)
@@ -2,11 +2,11 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Boolean < ::ActiveRecord::Bixformer::Attribute::Base
5
- def export(activerecord_value)
5
+ def export(record_attribute_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
- activerecord_value.present? ? true_value : false_value
9
+ record_attribute_value.present? ? true_value : false_value
10
10
  end
11
11
 
12
12
  def import(value)
@@ -2,9 +2,9 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Booletania < ::ActiveRecord::Bixformer::Attribute::Base
5
- def export(activerecord_value)
5
+ def export(record_attribute_value)
6
6
  @model.activerecord_constant.__send__("#{@name}_options").find do |text, bool|
7
- bool == activerecord_value
7
+ bool == record_attribute_value
8
8
  end&.first
9
9
  end
10
10
 
@@ -2,10 +2,10 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Date < ::ActiveRecord::Bixformer::Attribute::Base
5
- def export(activerecord_value)
6
- return nil unless activerecord_value
5
+ def export(record_attribute_value)
6
+ return nil unless record_attribute_value
7
7
 
8
- activerecord_value.to_s(option_format)
8
+ record_attribute_value.to_s(option_format)
9
9
  end
10
10
 
11
11
  def import(value)
@@ -2,11 +2,11 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Enumerize < ::ActiveRecord::Bixformer::Attribute::Base
5
- def export(activerecord_value)
6
- activerecord_value = activerecord_value.to_s
5
+ def export(record_attribute_value)
6
+ record_attribute_value = record_attribute_value.to_s
7
7
 
8
8
  @model.activerecord_constant.__send__(@name).options.find do |text, key|
9
- key == activerecord_value
9
+ key == record_attribute_value
10
10
  end&.first
11
11
  end
12
12
 
@@ -2,8 +2,8 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Override < ::ActiveRecord::Bixformer::Attribute::Base
5
- def export(activerecord_value)
6
- @model.__send__("override_export_#{@name}", activerecord_value)
5
+ def export(record_attribute_value)
6
+ @model.__send__("override_export_#{@name}", record_attribute_value)
7
7
  end
8
8
 
9
9
  def import(value)
@@ -2,8 +2,8 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class String < ::ActiveRecord::Bixformer::Attribute::Base
5
- def export(activerecord_value)
6
- activerecord_value.to_s
5
+ def export(record_attribute_value)
6
+ record_attribute_value.to_s
7
7
  end
8
8
 
9
9
  def import(value)
@@ -2,10 +2,10 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Time < ::ActiveRecord::Bixformer::Attribute::Base
5
- def export(activerecord_value)
6
- return nil unless activerecord_value
5
+ def export(record_attribute_value)
6
+ return nil unless record_attribute_value
7
7
 
8
- activerecord_value.to_s(option_format)
8
+ record_attribute_value.to_s(option_format)
9
9
  end
10
10
 
11
11
  def import(value)
@@ -6,8 +6,12 @@ module ActiveRecord
6
6
  super(:csv, plan)
7
7
  end
8
8
 
9
- def assignable_attributes(csv_row)
10
- compile.import(csv_row)
9
+ def verify_csv_titles(csv_title_row)
10
+ compile.verify_csv_titles(csv_title_row)
11
+ end
12
+
13
+ def assignable_attributes(csv_body_row)
14
+ compile.import(csv_body_row)
11
15
  end
12
16
  end
13
17
  end
@@ -7,7 +7,7 @@ module ActiveRecord
7
7
  # the instance has parent association.
8
8
  # @attr_reader [Hash<String, ActiveRecord::Bixformer::Attribute::Base>] attributes
9
9
  # the import/export target attribute names and its instance.
10
- # @attr_reader [Array<String>] optional_attributes
10
+ # @attr_reader [Array<String>] preferred_skip_attributes
11
11
  # the list of attribute name to not make key if its value is blank.
12
12
  # @attr_reader [Hash<String, ActiveRecord::Bixformer::Model::Base>] associations
13
13
  # the import/export target association names and its instance.
@@ -18,7 +18,7 @@ module ActiveRecord
18
18
  include ::ActiveRecord::Bixformer::ImportValueValidatable
19
19
 
20
20
  attr_reader :name, :options, :parent, :attributes, :associations,
21
- :optional_attributes, :translator
21
+ :preferred_skip_attributes, :translator
22
22
 
23
23
  def initialize(model_or_association_name, options)
24
24
  @name = model_or_association_name.to_s
@@ -37,7 +37,7 @@ module ActiveRecord
37
37
  @plan.new_module_instance(:attribute, attribute_type, self, attribute_name, attribute_options)
38
38
  end
39
39
 
40
- @optional_attributes = @plan.pickup_value_for(self, :optional_attributes, [])
40
+ @preferred_skip_attributes = @plan.pickup_value_for(self, :preferred_skip_attributes, [])
41
41
  @default_values = @plan.pickup_value_for(self, :default_values, {})
42
42
 
43
43
  # At present, translation function is only i18n
@@ -82,21 +82,21 @@ module ActiveRecord
82
82
  end
83
83
  end
84
84
 
85
- def find_activerecord_by!(condition)
85
+ def find_record_by!(condition)
86
86
  activerecord_constant.find_by!(condition)
87
87
  end
88
88
 
89
- def export(activerecord_or_activerecords)
90
- # has_one でしか使わない想定なので activerecord_or_activerecords は ActiveRecord::Base のはず
89
+ def export(record_or_records)
90
+ # has_one でしか使わない想定なので record_or_records は ActiveRecord::Base のはず
91
91
  values = @attributes.map do |attr|
92
92
  value_reader = attr.options[:reader] || attr.name
93
- attribute_value = activerecord_or_activerecords && activerecord_or_activerecords.__send__(value_reader)
93
+ attribute_value = record_or_records && record_or_records.__send__(value_reader)
94
94
 
95
95
  [csv_title(attr.name), attr.export(attribute_value)]
96
96
  end.to_h.with_indifferent_access
97
97
 
98
98
  @associations.inject(values) do |each_values, association|
99
- association_value = activerecord_or_activerecords && activerecord_or_activerecords.__send__(association.name)
99
+ association_value = record_or_records && record_or_records.__send__(association.name)
100
100
 
101
101
  association_value = association_value.to_a if association_value.is_a?(::ActiveRecord::Relation)
102
102
 
@@ -106,9 +106,9 @@ module ActiveRecord
106
106
 
107
107
  private
108
108
 
109
- def make_each_attribute_import_value(parent_activerecord_id = nil, &block)
109
+ def make_each_attribute_import_value(parent_record_id = nil, &block)
110
110
  values = {}.with_indifferent_access
111
- normalizer = ::ActiveRecord::Bixformer::AssignableAttributesNormalizer.new(plan, self, parent_activerecord_id)
111
+ normalizer = ::ActiveRecord::Bixformer::AssignableAttributesNormalizer.new(plan, self, parent_record_id)
112
112
 
113
113
  @attributes.each do |attr|
114
114
  attribute_value = block.call(attr)
@@ -117,7 +117,7 @@ module ActiveRecord
117
117
 
118
118
  # 取り込み時は、オプショナルな属性では、空と思われる値は取り込まない
119
119
  next if ! presence_value?(attribute_value) &&
120
- @optional_attributes.include?(attr.name.to_s)
120
+ @preferred_skip_attributes.include?(attr.name.to_s)
121
121
 
122
122
  values[attr.name] = attribute_value
123
123
  end
@@ -126,10 +126,10 @@ module ActiveRecord
126
126
  end
127
127
 
128
128
  def make_each_association_import_value(values, &block)
129
- self_activerecord_id = values[activerecord_constant.primary_key]
129
+ self_record_id = values[activerecord_constant.primary_key]
130
130
 
131
131
  @associations.each do |association|
132
- association_value = block.call(association, self_activerecord_id)
132
+ association_value = block.call(association, self_record_id)
133
133
 
134
134
  if association_value.is_a?(::Array)
135
135
  # has_many な場合は、配列が返ってくるが、空と思われる要素は結果に含めない
@@ -138,7 +138,7 @@ module ActiveRecord
138
138
 
139
139
  # 取り込み時は、オプショナルな関連では、空と思われる値は取り込まない
140
140
  next if ! presence_value?(association_value) &&
141
- @optional_attributes.include?(association.name.to_s)
141
+ @preferred_skip_attributes.include?(association.name.to_s)
142
142
 
143
143
  values["#{association.name}_attributes".to_sym] = association_value
144
144
  end
@@ -3,18 +3,23 @@ module ActiveRecord
3
3
  module Model
4
4
  module Csv
5
5
  class Base < ::ActiveRecord::Bixformer::Model::Base
6
- def import(csv_row, parent_activerecord_id = nil)
7
- values = make_each_attribute_import_value(parent_activerecord_id) do |attr|
8
- csv_value = csv_row[csv_title(attr.name)]
6
+ def import(csv_body_row, parent_record_id = nil)
7
+ values = make_each_attribute_import_value(parent_record_id) do |attr|
8
+ csv_value = csv_body_row[csv_title(attr.name)]
9
9
 
10
10
  attr.import(csv_value)
11
11
  end
12
12
 
13
- make_each_association_import_value(values) do |association, self_activerecord_id|
14
- association.import(csv_row, self_activerecord_id)
13
+ make_each_association_import_value(values) do |association, self_record_id|
14
+ association.import(csv_body_row, self_record_id)
15
15
  end
16
16
  end
17
17
 
18
+ def verify_csv_titles(csv_title_row)
19
+ @attributes.map { |attr| csv_title(attr.name) }.all? { |title| csv_title_row.include?(title) } &&
20
+ @associations.all? { |ass| ass.verify_csv_titles(csv_title_row) }
21
+ end
22
+
18
23
  def csv_titles
19
24
  [
20
25
  *@attributes.map { |attr| csv_title(attr.name) },
@@ -9,18 +9,18 @@ module ActiveRecord
9
9
  @options[:size] ||= 1
10
10
  end
11
11
 
12
- def export(activerecord_or_activerecords)
13
- activerecord_or_activerecords ||= []
12
+ def export(record_or_records)
13
+ record_or_records ||= []
14
14
 
15
- # has_many でしか使わない想定なので activerecord_or_activerecords は Array のはず
15
+ # has_many でしか使わない想定なので record_or_records は Array のはず
16
16
  (1..options[:size]).inject({}) do |values, index|
17
17
  update_translator(index)
18
18
 
19
- values.merge(super(activerecord_or_activerecords[index-1]))
19
+ values.merge(super(record_or_records[index-1]))
20
20
  end
21
21
  end
22
22
 
23
- def import(csv_row, parent_activerecord_id = nil)
23
+ def import(csv_body_row, parent_record_id = nil)
24
24
  # has_many でしか使わない想定なので Array を返却
25
25
  (1..options[:size]).map do |index|
26
26
  update_translator(index)
@@ -29,6 +29,13 @@ module ActiveRecord
29
29
  end
30
30
  end
31
31
 
32
+ def verify_csv_titles(csv_title_row)
33
+ # size は可変長なので、'1'だけ検証する
34
+ update_translator(1)
35
+
36
+ super
37
+ end
38
+
32
39
  def csv_titles
33
40
  (1..options[:size]).flat_map do |index|
34
41
  update_translator(index)
@@ -9,20 +9,20 @@ module ActiveRecord
9
9
  class_attribute :__bixformer_model
10
10
  class_attribute :__bixformer_namespace
11
11
  class_attribute :__bixformer_entry
12
- class_attribute :__bixformer_optional_attributes
12
+ class_attribute :__bixformer_preferred_skip_attributes
13
13
  class_attribute :__bixformer_required_attributes
14
- class_attribute :__bixformer_unique_indexes
14
+ class_attribute :__bixformer_unique_attributes
15
15
  class_attribute :__bixformer_required_condition
16
16
  class_attribute :__bixformer_default_values
17
17
  class_attribute :__bixformer_translation_config
18
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: [] }
19
+ self.__bixformer_entry = {}
20
+ self.__bixformer_preferred_skip_attributes = []
21
+ self.__bixformer_required_attributes = []
22
+ self.__bixformer_unique_attributes = []
23
+ self.__bixformer_required_condition = {}
24
+ self.__bixformer_default_values = {}
25
+ self.__bixformer_translation_config = { scope: :bixformer, extend_scopes: [] }
26
26
  end
27
27
 
28
28
  module ClassMethods
@@ -35,7 +35,7 @@ module ActiveRecord
35
35
  end
36
36
 
37
37
  [
38
- :entry, :optional_attributes, :required_attributes, :unique_indexes,
38
+ :entry, :preferred_skip_attributes, :required_attributes, :unique_attributes,
39
39
  :required_condition, :default_values, :translation_config
40
40
  ].each do |config_name|
41
41
  define_method "bixformer_#{config_name}" do |v|
@@ -11,6 +11,10 @@ module ActiveRecord
11
11
  @plan
12
12
  end
13
13
 
14
+ def for
15
+ @plan.class.__bixformer_model
16
+ end
17
+
14
18
  def value_of(config_name)
15
19
  compile_config_value(config_name).dup
16
20
  end
@@ -16,40 +16,6 @@ module ActiveRecord
16
16
 
17
17
  model.csv_titles.map { |title| body_map[title] }
18
18
  end
19
-
20
- # private
21
-
22
- # def csv_body_map_by_model(model, activerecord_or_activerecords)
23
- # activerecord_or_activerecords = if activerecord_or_activerecords.is_a?(::ActiveRecord::Relation)
24
- # activerecord_or_activerecords.to_a
25
- # else
26
- # activerecord_or_activerecords
27
- # end
28
-
29
- # model.generate_export_value(activerecord_or_activerecords).merge(
30
- # csv_body_map_by_association(model, activerecord_or_activerecords)
31
- # )
32
- # end
33
-
34
- # def csv_body_map_by_association(model, activerecord_or_activerecords)
35
- # model.associations.inject({}) do |body_map, association_model|
36
- # activerecords = if activerecord_or_activerecords.is_a?(::Array)
37
- # activerecord_or_activerecords
38
- # else
39
- # [activerecord_or_activerecords]
40
- # end
41
-
42
- # body_map.merge(
43
- # activerecords.inject({}) do |body_each_map, activerecord|
44
- # association_value = activerecord.__send__(association_model.name)
45
-
46
- # body_each_map.merge(
47
- # csv_body_map_by_model(association_model, association_value)
48
- # )
49
- # end
50
- # )
51
- # end
52
- # end
53
19
  end
54
20
  end
55
21
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Bixformer
3
- VERSION = "0.2.3"
3
+ VERSION = "0.2.4"
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.3
4
+ version: 0.2.4
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-19 00:00:00.000000000 Z
11
+ date: 2016-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord