Module Spreadsheet::Datatypes
In: lib/spreadsheet/datatypes.rb
Column Format Row Font Enumerable Worksheet\n[lib/spreadsheet/excel.rb\nlib/spreadsheet/worksheet.rb] Link Workbook Workbook\n[lib/spreadsheet/excel.rb\nlib/spreadsheet/excel/workbook.rb] SstEntry String Row Array Worksheet ExcelCompatibleWorkbook Format Worksheet Workbook DelegateClassSpreadsheet::Format Writer Compatibility Formula Reader\n[lib/parseexcel/parseexcel.rb\nlib/spreadsheet/excel/reader.rb\nlib/spreadsheet/excel/reader/biff5.rb\nlib/spreadsheet/excel/reader/biff8.rb] Error lib/spreadsheet/excel.rb lib/spreadsheet/link.rb lib/spreadsheet/formula.rb lib/spreadsheet/workbook.rb lib/spreadsheet/font.rb lib/spreadsheet/column.rb lib/spreadsheet/format.rb lib/spreadsheet/writer.rb lib/spreadsheet/row.rb Encodings lib/spreadsheet/excel.rb lib/spreadsheet/excel/row.rb lib/spreadsheet/excel/reader/biff5.rb lib/spreadsheet/excel/worksheet.rb lib/spreadsheet/excel/sst_entry.rb lib/spreadsheet/excel/error.rb lib/spreadsheet/excel/workbook.rb lib/spreadsheet/excel/writer/format.rb lib/spreadsheet/excel/writer/workbook.rb lib/spreadsheet/excel/writer/worksheet.rb Biff8 Writer Biff5 Biff8 Internals Offset Excel Datatypes ParseExcel Compatibility Spreadsheet dot/m_34_0.png

This module defines convenience-methods for the definition of Spreadsheet attributes (boolean, colors and enumerations)

Methods

Included Modules

Compatibility

Constants

COLORS = [ :builtin_black, :builtin_white, :builtin_red, :builtin_green, :builtin_blue, :builtin_yellow, :builtin_magenta, :builtin_cyan, :text, :border, :pattern_bg, :dialog_bg, :chart_text, :chart_bg, :chart_border, :tooltip_bg, :tooltip_text, :aqua, :black, :blue, :cyan, :brown, :fuchsia, :gray, :grey, :green, :lime, :magenta, :navy, :orange, :purple, :red, :silver, :white, :yellow ]   Valid colors for color attributes.

Public Class methods

[Source]

     # File lib/spreadsheet/datatypes.rb, line 9
  9:     def Datatypes.append_features mod
 10:       super
 11:       mod.module_eval do
 12: class << self
 13:   ##
 14:   # Valid colors for color attributes.
 15:   COLORS = [ :builtin_black, :builtin_white, :builtin_red, :builtin_green,
 16:              :builtin_blue, :builtin_yellow, :builtin_magenta, :builtin_cyan,
 17:              :text, :border, :pattern_bg, :dialog_bg, :chart_text, :chart_bg,
 18:              :chart_border, :tooltip_bg, :tooltip_text, :aqua,
 19:              :black, :blue, :cyan, :brown, :fuchsia, :gray, :grey, :green,
 20:              :lime, :magenta, :navy, :orange, :purple, :red, :silver, :white,
 21:              :yellow ]
 22:   ##
 23:   # Define instance methods to read and write boolean attributes.
 24:   def boolean *args
 25:     args.each do |key|
 26:       define_method key do
 27:         name = ivar_name key
 28:         !!(instance_variable_get(name) if instance_variables.include?(name))
 29:       end
 30:       define_method "#{key}?" do
 31:         send key
 32:       end
 33:       define_method "#{key}=" do |arg|
 34:         arg = false if arg == 0
 35:         instance_variable_set(ivar_name(key), !!arg)
 36:       end
 37:       define_method "#{key}!" do
 38:         send "#{key}=", true
 39:       end
 40:     end
 41:   end
 42:   ##
 43:   # Define instance methods to read and write color attributes.
 44:   # For valid colors see COLORS
 45:   def colors *args
 46:     args.each do |key|
 47:       attr_reader key
 48:       define_method "#{key}=" do |name|
 49:         name = name.to_s.downcase.to_sym
 50:         if COLORS.include?(name)
 51:           instance_variable_set ivar_name(key), name
 52:         else
 53:           raise ArgumentError, "unknown color '#{name}'"
 54:         end
 55:       end
 56:     end
 57:   end
 58:   ##
 59:   # Define instance methods to read and write enumeration attributes.
 60:   # * The first argument designates the attribute name.
 61:   # * The second argument designates the default value.
 62:   # * All subsequent attributes are possible values.
 63:   # * If the last attribute is a Hash, each value in the Hash designates
 64:   #   aliases for the corresponding key.
 65:   def enum key, *values
 66:     aliases = {}
 67:     if values.last.is_a? Hash
 68:       values.pop.each do |value, synonyms|
 69:         if synonyms.is_a? Array
 70:           synonyms.each do |synonym| aliases.store synonym, value end
 71:         else
 72:           aliases.store synonyms, value
 73:         end
 74:       end
 75:     end
 76:     values.each do |value|
 77:       aliases.store value, value
 78:     end
 79:     define_method key do
 80:       name = ivar_name key
 81:       value = instance_variable_get(name) if instance_variables.include? name
 82:       value || values.first
 83:     end
 84:     define_method "#{key}=" do |arg|
 85:       if arg
 86:         arg = aliases.fetch arg do
 87:           aliases.fetch arg.to_s.downcase.gsub(/[ \-]/, '_').to_sym, arg
 88:         end
 89:         if values.any? do |val| val === arg end
 90:           instance_variable_set(ivar_name(key), arg)
 91:         else
 92:           valid = values.collect do |val| val.inspect end.join ', '
 93:           raise ArgumentError,
 94:             "Invalid value '#{arg.inspect}' for #{key}. Valid values are: #{valid}"
 95:         end
 96:       else
 97:         instance_variable_set ivar_name(key), values.first
 98:       end
 99:     end
100:   end
101: end
102:       end
103:     end

Define instance methods to read and write boolean attributes.

[Source]

    # File lib/spreadsheet/datatypes.rb, line 24
24:   def boolean *args
25:     args.each do |key|
26:       define_method key do
27:         name = ivar_name key
28:         !!(instance_variable_get(name) if instance_variables.include?(name))
29:       end
30:       define_method "#{key}?" do
31:         send key
32:       end
33:       define_method "#{key}=" do |arg|
34:         arg = false if arg == 0
35:         instance_variable_set(ivar_name(key), !!arg)
36:       end
37:       define_method "#{key}!" do
38:         send "#{key}=", true
39:       end
40:     end
41:   end

Define instance methods to read and write color attributes. For valid colors see COLORS

[Source]

    # File lib/spreadsheet/datatypes.rb, line 45
45:   def colors *args
46:     args.each do |key|
47:       attr_reader key
48:       define_method "#{key}=" do |name|
49:         name = name.to_s.downcase.to_sym
50:         if COLORS.include?(name)
51:           instance_variable_set ivar_name(key), name
52:         else
53:           raise ArgumentError, "unknown color '#{name}'"
54:         end
55:       end
56:     end
57:   end

Define instance methods to read and write enumeration attributes.

  • The first argument designates the attribute name.
  • The second argument designates the default value.
  • All subsequent attributes are possible values.
  • If the last attribute is a Hash, each value in the Hash designates aliases for the corresponding key.

[Source]

     # File lib/spreadsheet/datatypes.rb, line 65
 65:   def enum key, *values
 66:     aliases = {}
 67:     if values.last.is_a? Hash
 68:       values.pop.each do |value, synonyms|
 69:         if synonyms.is_a? Array
 70:           synonyms.each do |synonym| aliases.store synonym, value end
 71:         else
 72:           aliases.store synonyms, value
 73:         end
 74:       end
 75:     end
 76:     values.each do |value|
 77:       aliases.store value, value
 78:     end
 79:     define_method key do
 80:       name = ivar_name key
 81:       value = instance_variable_get(name) if instance_variables.include? name
 82:       value || values.first
 83:     end
 84:     define_method "#{key}=" do |arg|
 85:       if arg
 86:         arg = aliases.fetch arg do
 87:           aliases.fetch arg.to_s.downcase.gsub(/[ \-]/, '_').to_sym, arg
 88:         end
 89:         if values.any? do |val| val === arg end
 90:           instance_variable_set(ivar_name(key), arg)
 91:         else
 92:           valid = values.collect do |val| val.inspect end.join ', '
 93:           raise ArgumentError,
 94:             "Invalid value '#{arg.inspect}' for #{key}. Valid values are: #{valid}"
 95:         end
 96:       else
 97:         instance_variable_set ivar_name(key), values.first
 98:       end
 99:     end
100:   end

[Validate]