spreadsheet_model 0.4.2 → 0.4.3
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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/spreadsheet_model.rb +15 -18
- data/lib/spreadsheet_model/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d09161b7b5fcffe4de869aac0423e9ac33c9c076
|
4
|
+
data.tar.gz: 72c3314f35164008ec9a7262f08acd7f29f1fe8b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 981501879576d9d01b6690c7b0467b6faea43b1ed8d59a86f4bde6d2638dc36b3097a4e36ac79c4a3e173ce33b08a1d000c4b115518650d114bb8fe95e85165e
|
7
|
+
data.tar.gz: 5520b8db3c9c8b368b4d3015b4e02c203702f05ba26191d4abc2a64d1779864c3ca71657aec95ef83e8a7639a35d7bc59d525311eab7698f9f4bdde8d759790a
|
data/Gemfile.lock
CHANGED
data/lib/spreadsheet_model.rb
CHANGED
@@ -15,8 +15,7 @@ module SpreadsheetModel
|
|
15
15
|
|
16
16
|
def self.attr_accessor(*args)
|
17
17
|
super
|
18
|
-
@
|
19
|
-
@column_names.concat(args.map(&:to_s))
|
18
|
+
@accessors = args
|
20
19
|
end
|
21
20
|
|
22
21
|
def [](name)
|
@@ -43,11 +42,10 @@ module SpreadsheetModel
|
|
43
42
|
|
44
43
|
keys = sheets.each_with_object([]) do |sheet, keys|
|
45
44
|
rows = sheet.rows.dup
|
46
|
-
|
47
|
-
@column_names ||= header
|
45
|
+
@column_names = rows.shift
|
48
46
|
|
49
47
|
store_hash = rows.each_with_object({}) do |row, store_hash|
|
50
|
-
row_hash = Hash[
|
48
|
+
row_hash = Hash[*@column_names.zip(row).flatten]
|
51
49
|
row_hash = @import_callback.call(row_hash) if @import_callback
|
52
50
|
write_rows = [store_hash[row[0]], row_hash].compact.flatten
|
53
51
|
store_hash[row[0]] = write_rows
|
@@ -60,8 +58,8 @@ module SpreadsheetModel
|
|
60
58
|
end
|
61
59
|
keys
|
62
60
|
end
|
63
|
-
write_cache('
|
64
|
-
write_cache('
|
61
|
+
write_cache('__spreadsheetmodel_keys', keys)
|
62
|
+
write_cache('__spreadsheetmodel_cached', true)
|
65
63
|
end
|
66
64
|
|
67
65
|
# inspired by:
|
@@ -72,12 +70,17 @@ module SpreadsheetModel
|
|
72
70
|
end
|
73
71
|
|
74
72
|
def self.cached?
|
75
|
-
!!read_cache('
|
73
|
+
!!read_cache('__spreadsheetmodel_cached')
|
76
74
|
end
|
77
75
|
|
78
76
|
def self.keys
|
79
77
|
import unless cached?
|
80
|
-
read_cache('
|
78
|
+
read_cache('__spreadsheetmodel_keys')
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.column_names
|
82
|
+
import unless cached?
|
83
|
+
@column_names
|
81
84
|
end
|
82
85
|
|
83
86
|
private
|
@@ -106,8 +109,8 @@ module SpreadsheetModel
|
|
106
109
|
# https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/relation/finder_methods.rb#L432
|
107
110
|
def self.find_one(id)
|
108
111
|
rows = read_cache(id)
|
109
|
-
return nil unless rows
|
110
112
|
|
113
|
+
return nil unless rows
|
111
114
|
row_to_instance(rows[0])
|
112
115
|
end
|
113
116
|
|
@@ -154,9 +157,8 @@ module SpreadsheetModel
|
|
154
157
|
|
155
158
|
def self.row_to_instance(row)
|
156
159
|
return nil unless row
|
157
|
-
attributes = row.select { |key, _| @
|
158
|
-
|
159
|
-
if attributes['type'].to_s.present?
|
160
|
+
attributes = row.select { |key, _| @accessors.try(:include?, key.to_sym) }
|
161
|
+
if row['type'].to_s.present?
|
160
162
|
instance = attributes['type'].constantize.new(attributes)
|
161
163
|
else
|
162
164
|
instance = self.new(attributes)
|
@@ -165,10 +167,5 @@ module SpreadsheetModel
|
|
165
167
|
instance.instance_variable_set(:@__row, row)
|
166
168
|
instance
|
167
169
|
end
|
168
|
-
|
169
|
-
def self.column_names
|
170
|
-
import unless cached?
|
171
|
-
@column_names
|
172
|
-
end
|
173
170
|
end
|
174
171
|
end
|