Module: SpreadsheetArchitect::Utils
- Defined in:
- lib/spreadsheet_architect/utils.rb,
lib/spreadsheet_architect/utils/xlsx.rb
Defined Under Namespace
Modules: XLSX
Class Method Summary collapse
- .check_options_types(options = {}) ⇒ Object
- .check_type(options, option_name, type) ⇒ Object
- .convert_styles_to_ods(styles = {}) ⇒ Object
- .deep_clone(x) ⇒ Object
- .get_cell_data(options = {}, klass) ⇒ Object
- .get_options(options = {}, klass) ⇒ Object
- .str_humanize(str, capitalize = true) ⇒ Object
-
.stringify_keys(hash = {}) ⇒ Object
only converts the first 2 levels.
Class Method Details
.check_options_types(options = {}) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/spreadsheet_architect/utils.rb', line 198 def self.(={}) self.check_type(, :spreadsheet_columns, Array) self.check_type(, :instances, Array) self.check_type(, :headers, [TrueClass, FalseClass, Array]) self.check_type(, :sheet_name, String) self.check_type(, :header_style, Hash) self.check_type(, :row_style, Hash) self.check_type(, :column_styles, Array) self.check_type(, :range_styles, Array) self.check_type(, :merges, Array) self.check_type(, :borders, Array) self.check_type(, :column_widths, Array) end |
.check_type(options, option_name, type) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/spreadsheet_architect/utils.rb', line 182 def self.check_type(, option_name, type) unless [option_name].nil? valid = false if type.is_a?(Array) valid = type.any?{|t| [option_name].is_a?(t)} elsif [option_name].is_a?(type) valid = true end if valid raise SpreadsheetArchitect::Exceptions::IncorrectTypeError option_name end end end |
.convert_styles_to_ods(styles = {}) ⇒ Object
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 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/spreadsheet_architect/utils.rb', line 140 def self.convert_styles_to_ods(styles={}) styles = {} unless styles.is_a?(Hash) styles = self.stringify_keys(styles) property_styles = {} text_styles = {} text_styles['font-weight'] = styles.delete('bold') ? 'bold' : styles.delete('font-weight') text_styles['font-size'] = styles.delete('font_size') || styles.delete('font-size') text_styles['font-style'] = styles.delete('italic') ? 'italic' : styles.delete('font-style') if styles['underline'] styles.delete('underline') text_styles['text-underline-style'] = 'solid' text_styles['text-underline-type'] = 'single' end if styles['align'] text_styles['align'] = true end if styles['color'].respond_to?(:sub) && !styles['color'].empty? text_styles['color'] = "##{styles.delete('color').sub('#','')}" end text_styles.delete_if{|_,v| v.nil?} property_styles['text'] = text_styles cell_styles = {} styles['background_color'] ||= styles.delete('background-color') if styles['background_color'].respond_to?(:sub) && !styles['background_color'].empty? cell_styles['background-color'] = "##{styles.delete('background_color').sub('#','')}" end cell_styles.delete_if{|_,v| v.nil?} property_styles['cell'] = cell_styles return property_styles end |
.deep_clone(x) ⇒ Object
178 179 180 |
# File 'lib/spreadsheet_architect/utils.rb', line 178 def self.deep_clone(x) Marshal.load(Marshal.dump(x)) end |
.get_cell_data(options = {}, klass) ⇒ Object
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 |
# File 'lib/spreadsheet_architect/utils.rb', line 11 def self.get_cell_data(={}, klass) self. if klass.name == 'SpreadsheetArchitect' if ![:data] raise SpreadsheetArchitect::Exceptions::NoDataError end if [:headers] && [:headers].is_a?(Array) && ![:headers].empty? headers = [:headers] else headers = false end data = [:data] else has_custom_columns = [:spreadsheet_columns] || klass.instance_methods.include?(:spreadsheet_columns) if ![:instances] && defined?(ActiveRecord) && klass.ancestors.include?(ActiveRecord::Base) [:instances] = klass.where(nil).to_a # triggers the relation call, not sure how this works but it does end if ![:instances] raise SpreadsheetArchitect::Exceptions::NoInstancesError end if [:headers].nil? headers = klass::SPREADSHEET_OPTIONS[:headers] if defined?(klass::SPREADSHEET_OPTIONS) headers ||= SpreadsheetArchitect.[:headers] elsif [:headers].is_a?(Array) headers = [:headers] end if headers == false || headers.is_a?(Array) needs_headers = false else headers = true needs_headers = true end if has_custom_columns headers = [] if needs_headers columns = [] array = [:spreadsheet_columns] || ([:instances].empty? ? [] : [:instances].first.spreadsheet_columns) array.each_with_index do |x,i| if x.is_a?(Array) headers.push(x[0].to_s) if needs_headers columns.push x[1] else headers.push(str_humanize(x.to_s)) if needs_headers columns.push x end end elsif !has_custom_columns && defined?(ActiveRecord) && klass.ancestors.include?(ActiveRecord::Base) ignored_columns = ["id","created_at","updated_at","deleted_at"] the_column_names = (klass.column_names - ignored_columns) headers = the_column_names.map{|x| str_humanize(x)} if needs_headers columns = the_column_names.map{|x| x.to_sym} else raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError, klass end data = [] [:instances].each do |instance| if has_custom_columns && ![:spreadsheet_columns] row_data = [] instance.spreadsheet_columns.each do |x| if x.is_a?(Array) row_data.push(x[1].is_a?(Symbol) ? instance.instance_eval(x[1].to_s) : x[1]) else row_data.push(x.is_a?(Symbol) ? instance.instance_eval(x.to_s) : x) end end data.push row_data else data.push columns.map{|col| col.is_a?(Symbol) ? instance.instance_eval(col.to_s) : col} end end end if headers && !headers[0].is_a?(Array) headers = [headers] end return .merge(headers: headers, data: data, column_types: [:column_types]) end |
.get_options(options = {}, klass) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 |
# File 'lib/spreadsheet_architect/utils.rb', line 98 def self.(={}, klass) if [:headers] if defined?(klass::SPREADSHEET_OPTIONS) header_style = deep_clone(SpreadsheetArchitect.[:header_style]).merge(klass::SPREADSHEET_OPTIONS[:header_style] || {}) else header_style = deep_clone(SpreadsheetArchitect.[:header_style]) end if [:header_style] header_style.merge!([:header_style]) elsif [:header_style] == false header_style = false end else header_style = false end if [:row_style] == false row_style = false else if defined?(klass::SPREADSHEET_OPTIONS) row_style = deep_clone(SpreadsheetArchitect.[:row_style]).merge(klass::SPREADSHEET_OPTIONS[:row_style] || {}) else row_style = deep_clone(SpreadsheetArchitect.[:row_style]) end if [:row_style] row_style.merge!([:row_style]) end end if defined?(klass::SPREADSHEET_OPTIONS) sheet_name = [:sheet_name] || klass::SPREADSHEET_OPTIONS[:sheet_name] || SpreadsheetArchitect.[:sheet_name] else sheet_name = [:sheet_name] || SpreadsheetArchitect.[:sheet_name] end sheet_name ||= (klass.name == 'SpreadsheetArchitect' ? 'Sheet1' : klass.name) return .merge(header_style: header_style, row_style: row_style, sheet_name: sheet_name) end |
.str_humanize(str, capitalize = true) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/spreadsheet_architect/utils.rb', line 3 def self.str_humanize(str, capitalize = true) str = str.sub(/\A_+/, '').gsub(/[_\.]/,' ').sub(' rescue nil','') if capitalize str = str.gsub(/(\A|\ )\w/){|x| x.upcase} end return str end |
.stringify_keys(hash = {}) ⇒ Object
only converts the first 2 levels
213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/spreadsheet_architect/utils.rb', line 213 def self.stringify_keys(hash={}) new_hash = {} hash.each do |k,v| if v.is_a?(Hash) v.each do |k2,v2| new_hash[k2.to_s] = v2 end else new_hash[k.to_s] = v end end return new_hash end |