activerecord-bixformer 0.2.4 → 0.2.5

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: 18bbd017fde0b78cc6edbcbea84f68cf62088a48
4
- data.tar.gz: 7560282ebbd8bc2474a2b91534a76ec453ff4a53
3
+ metadata.gz: 1372a26753e6dc7dc2d178f00a9d3e54aed1973a
4
+ data.tar.gz: 9089e7d6d4b0344409a6f59a8d5a28e97fa4708d
5
5
  SHA512:
6
- metadata.gz: 546d0e273f0e30697b54b5b30cb200f21ab7d36834db166fb4faa777f2eb3b175bf595fe6a4e997669e3233fef5aad4ac60cecfa980412bbfe6126783ddc47c5
7
- data.tar.gz: fa1063540a8d61e068c74f4af2a7e8b6be2ead2034f02686266fd187339302257d1fc06e241251d850dd2fbb85e7f30f72fa6e6c9f9be6fb8a67044ad4844529
6
+ metadata.gz: 0935d90425fe726f56de126f08671a1807959b6a62f1aa0572aef5ff1356e4b33526c1b903c9f6065ac9f89718053111f77f8df26a5eb50be1efc5a4c4bfcd26
7
+ data.tar.gz: 850006053e33be9a96a827b0c16b88aecafffeea36e126ebe0589612ee93a47cae24c436fbbe3fed7ddf25f0a0ec381c015b6d28b1857091d1416e197c208599
@@ -10,13 +10,19 @@ module ActiveRecord
10
10
  @options = (options.is_a?(::Hash) ? options : {}).with_indifferent_access
11
11
  end
12
12
 
13
- def export(record_attribute_value)
14
- record_attribute_value
13
+ def export(record)
14
+ record_attribute_value(record)
15
15
  end
16
16
 
17
17
  def import(value)
18
18
  value
19
19
  end
20
+
21
+ private
22
+
23
+ def record_attribute_value(record)
24
+ record.__send__(@name)
25
+ end
20
26
  end
21
27
  end
22
28
  end
@@ -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(record_attribute_value)
5
+ def export(record)
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
- record_attribute_value.present? ? true_value : false_value
9
+ record_attribute_value(record).present? ? true_value : false_value
10
10
  end
11
11
 
12
12
  def import(value)
@@ -2,7 +2,9 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Booletania < ::ActiveRecord::Bixformer::Attribute::Base
5
- def export(record_attribute_value)
5
+ def export(record)
6
+ record_attribute_value = record_attribute_value(record)
7
+
6
8
  @model.activerecord_constant.__send__("#{@name}_options").find do |text, bool|
7
9
  bool == record_attribute_value
8
10
  end&.first
@@ -2,7 +2,9 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Date < ::ActiveRecord::Bixformer::Attribute::Base
5
- def export(record_attribute_value)
5
+ def export(record)
6
+ record_attribute_value = record_attribute_value(record)
7
+
6
8
  return nil unless record_attribute_value
7
9
 
8
10
  record_attribute_value.to_s(option_format)
@@ -2,8 +2,8 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Enumerize < ::ActiveRecord::Bixformer::Attribute::Base
5
- def export(record_attribute_value)
6
- record_attribute_value = record_attribute_value.to_s
5
+ def export(record)
6
+ record_attribute_value = record_attribute_value(record).to_s
7
7
 
8
8
  @model.activerecord_constant.__send__(@name).options.find do |text, key|
9
9
  key == record_attribute_value
@@ -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(record_attribute_value)
6
- @model.__send__("override_export_#{@name}", record_attribute_value)
5
+ def export(record)
6
+ @model.__send__("override_export_#{@name}", record_attribute_value(record))
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(record_attribute_value)
6
- record_attribute_value.to_s
5
+ def export(record)
6
+ record_attribute_value(record).to_s
7
7
  end
8
8
 
9
9
  def import(value)
@@ -2,7 +2,9 @@ module ActiveRecord
2
2
  module Bixformer
3
3
  module Attribute
4
4
  class Time < ::ActiveRecord::Bixformer::Attribute::Base
5
- def export(record_attribute_value)
5
+ def export(record)
6
+ record_attribute_value = record_attribute_value(record)
7
+
6
8
  return nil unless record_attribute_value
7
9
 
8
10
  record_attribute_value.to_s(option_format)
@@ -86,24 +86,6 @@ module ActiveRecord
86
86
  activerecord_constant.find_by!(condition)
87
87
  end
88
88
 
89
- def export(record_or_records)
90
- # has_one でしか使わない想定なので record_or_records は ActiveRecord::Base のはず
91
- values = @attributes.map do |attr|
92
- value_reader = attr.options[:reader] || attr.name
93
- attribute_value = record_or_records && record_or_records.__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 = record_or_records && record_or_records.__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
-
107
89
  private
108
90
 
109
91
  def make_each_attribute_import_value(parent_record_id = nil, &block)
@@ -3,6 +3,23 @@ module ActiveRecord
3
3
  module Model
4
4
  module Csv
5
5
  class Base < ::ActiveRecord::Bixformer::Model::Base
6
+ def export(record_or_records)
7
+ # has_one でしか使わない想定なので record_or_records は ActiveRecord::Base のはず
8
+ values = @attributes.map do |attr|
9
+ attribute_value = record_or_records && attr.export(record_or_records)
10
+
11
+ [csv_title(attr.name), attribute_value]
12
+ end.to_h.with_indifferent_access
13
+
14
+ @associations.inject(values) do |each_values, association|
15
+ association_value = record_or_records && record_or_records.__send__(association.name)
16
+
17
+ association_value = association_value.to_a if association_value.is_a?(::ActiveRecord::Relation)
18
+
19
+ each_values.merge(association.export(association_value))
20
+ end
21
+ end
22
+
6
23
  def import(csv_body_row, parent_record_id = nil)
7
24
  values = make_each_attribute_import_value(parent_record_id) do |attr|
8
25
  csv_value = csv_body_row[csv_title(attr.name)]
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Bixformer
3
- VERSION = "0.2.4"
3
+ VERSION = "0.2.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-bixformer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroaki Otsu