Module: SpreadsheetArchitect::Utils
- Defined in:
- lib/spreadsheet_architect/utils.rb,
lib/spreadsheet_architect/utils/ods.rb,
lib/spreadsheet_architect/utils/xlsx.rb
Defined Under Namespace
Class Method Summary collapse
- .check_option_type(options, option_name, type) ⇒ Object
- .get_cell_data(options, klass) ⇒ Object
- .get_options(options, klass) ⇒ Object
- .hash_array_symbolize_keys(array) ⇒ Object
- .is_ar_model?(klass) ⇒ Boolean
- .str_titleize(str) ⇒ Object
- .stringify_keys(hash) ⇒ Object
- .symbolize_keys(hash, shallow: false) ⇒ Object
- .verify_option_types(options) ⇒ Object
Class Method Details
.check_option_type(options, option_name, type) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/spreadsheet_architect/utils.rb', line 181 def self.check_option_type(, option_name, type) val = [option_name] if val if type.is_a?(Array) invalid = type.all?{|t| !val.is_a?(t) } elsif !val.is_a?(type) invalid = true end if invalid raise SpreadsheetArchitect::Exceptions::OptionTypeError.new(":#{option_name} option") end end end |
.get_cell_data(options, klass) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/spreadsheet_architect/utils.rb', line 3 def self.get_cell_data(, klass) if [:data] && [:instances] raise SpreadsheetArchitect::Exceptions::MultipleDataSourcesError elsif [:data] data = [:data] end if [:headers] == true headers = [] if [:data] headers = false else needs_headers = true end elsif [:headers].is_a?(Array) headers = [:headers] else headers = false end if [:column_types] column_types = [:column_types] else column_types = [] needs_column_types = true end if !data if ![:instances] if is_ar_model?(klass) [:instances] = klass.where(nil).to_a # triggers the relation call, not sure how this works but it does else raise SpreadsheetArchitect::Exceptions::NoDataError end end if [:spreadsheet_columns] if [String, Symbol].any?{|x| [:spreadsheet_columns].is_a?(x)} cols_method_name = [:spreadsheet_columns] if klass != SpreadsheetArchitect && !klass.instance_methods.include?(cols_method_name) raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(klass, cols_method_name) end end else if klass != SpreadsheetArchitect && !klass.instance_methods.include?(:spreadsheet_columns) if is_ar_model?(klass) the_column_names = klass.column_names headers = the_column_names.map{|x| str_titleize(x)} if needs_headers columns = the_column_names.map{|x| x.to_sym} else raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(klass) end end end data = [] [:instances].each do |instance| if columns data.push columns.map{|col| col.is_a?(Symbol) ? instance.send(col) : col} else row_data = [] if [:spreadsheet_columns] if cols_method_name instance_cols = instance.send(cols_method_name) else instance_cols = [:spreadsheet_columns].call(instance) end else if klass == SpreadsheetArchitect && !instance.respond_to?(:spreadsheet_columns) raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(instance.class) else instance_cols = instance.spreadsheet_columns end end instance_cols.each_with_index do |x,i| if x.is_a?(Array) headers.push(x[0].to_s) if needs_headers row_data.push(x[1].is_a?(Symbol) ? instance.send(x[1]) : x[1]) if needs_column_types column_types[i] = x[2] end else headers.push(str_titleize(x.to_s)) if needs_headers row_data.push(x.is_a?(Symbol) ? instance.send(x) : x) end end data.push row_data needs_headers = false needs_column_types = false end end end if headers && !headers[0].is_a?(Array) headers = [headers] end if column_types.compact.empty? column_types = nil end return .merge(headers: headers, data: data, column_types: column_types) end |
.get_options(options, klass) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/spreadsheet_architect/utils.rb', line 114 def self.(, klass) verify_option_types() if ![:skip_defaults] if defined?(klass::SPREADSHEET_OPTIONS) if klass::SPREADSHEET_OPTIONS.is_a?(Hash) defaults = SpreadsheetArchitect..merge(klass::SPREADSHEET_OPTIONS) else raise SpreadsheetArchitect::Exceptions::OptionTypeError.new("#{klass}::SPREADSHEET_OPTIONS constant") end else defaults = SpreadsheetArchitect. end = defaults.merge() end if ![:headers] .delete(:header_style) end if ![:sheet_name] if klass == SpreadsheetArchitect [:sheet_name] = 'Sheet1' else [:sheet_name] = klass.name if [:sheet_name].respond_to?(:pluralize) [:sheet_name] = [:sheet_name].pluralize end end end if [:freeze] [:freeze] = SpreadsheetArchitect::Utils.symbolize_keys([:freeze]) if [:freeze_headers] raise SpreadsheetArchitect::Exceptions::ArgumentError.new('Cannot use both :freeze and :freeze_headers options at the same time') end end if [:escape_formulas].nil? [:escape_formulas] = true end if [:use_zero_based_row_index].nil? [:use_zero_based_row_index] = false end return end |
.hash_array_symbolize_keys(array) ⇒ Object
239 240 241 242 243 244 245 246 247 |
# File 'lib/spreadsheet_architect/utils.rb', line 239 def self.hash_array_symbolize_keys(array) new_array = [] array.each_with_index do |x,i| new_array[i] = x.is_a?(Hash) ? self.symbolize_keys(x) : x end return new_array end |
.is_ar_model?(klass) ⇒ Boolean
168 169 170 |
# File 'lib/spreadsheet_architect/utils.rb', line 168 def self.is_ar_model?(klass) defined?(ActiveRecord) && klass.ancestors.include?(ActiveRecord::Base) end |
.str_titleize(str) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/spreadsheet_architect/utils.rb', line 172 def self.str_titleize(str) str = str.sub(/\A_+/, '') .gsub(/[_\.]/,' ') .sub(' rescue nil','') .gsub(/(\A|\ )\w/){|x| x.upcase} return str end |
.stringify_keys(hash) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/spreadsheet_architect/utils.rb', line 211 def self.stringify_keys(hash) new_hash = {} hash.each do |k,v| if v.is_a?(Hash) new_hash[k.to_s] = self.stringify_keys(v) else new_hash[k.to_s] = v end end return new_hash end |
.symbolize_keys(hash, shallow: false) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/spreadsheet_architect/utils.rb', line 225 def self.symbolize_keys(hash, shallow: false) new_hash = {} hash.each do |k,v| if v.is_a?(Hash) new_hash[k.to_sym] = shallow ? v : self.symbolize_keys(v) else new_hash[k.to_sym] = v end end return new_hash end |
.verify_option_types(options) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/spreadsheet_architect/utils.rb', line 197 def self.verify_option_types() = self.symbolize_keys(, shallow: true) bad_keys = .keys - ALLOWED_OPTIONS.keys if bad_keys.any? raise SpreadsheetArchitect::Exceptions::ArgumentError.new("Invalid options provided: #{bad_keys}") end ALLOWED_OPTIONS.each do |key, allowed_types| check_option_type(, key, allowed_types) end end |