Module: Spreadsheet::Datatypes

Includes:
Compatibility
Included in:
Column, Font, Format, Row, Worksheet
Defined in:
lib/spreadsheet/datatypes.rb

Overview

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

Class Method Summary collapse

Methods included from Compatibility

#ivar_name, #method_name

Class Method Details

.append_features(mod) ⇒ Object



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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/spreadsheet/datatypes.rb', line 9

def Datatypes.append_features mod
  super
  mod.module_eval do
class << self
  ##
  # Valid colors for color attributes.
  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,
         :xls_color_0,
         :xls_color_1,
         :xls_color_2,
         :xls_color_3,
         :xls_color_4,
         :xls_color_5,
         :xls_color_6,
         :xls_color_7,
         :xls_color_8,
         :xls_color_9,
         :xls_color_10,
         :xls_color_11,
         :xls_color_12,
         :xls_color_13,
         :xls_color_14,
         :xls_color_15,
         :xls_color_16,
         :xls_color_17,
         :xls_color_18,
         :xls_color_19,
         :xls_color_20,
         :xls_color_21,
         :xls_color_22,
         :xls_color_23,
         :xls_color_24,
         :xls_color_25,
         :xls_color_26,
         :xls_color_27,
         :xls_color_28,
         :xls_color_29,
         :xls_color_30,
         :xls_color_31,
         :xls_color_32,
         :xls_color_33,
         :xls_color_34,
         :xls_color_35,
         :xls_color_36,
         :xls_color_37,
         :xls_color_38,
         :xls_color_39,
         :xls_color_40,
         :xls_color_41,
         :xls_color_42,
         :xls_color_43,
         :xls_color_44,
         :xls_color_45,
         :xls_color_46,
         :xls_color_47,
         :xls_color_48,
         :xls_color_49,
         :xls_color_50,
         :xls_color_51,
         :xls_color_52,
         :xls_color_53,
         :xls_color_54,
         :xls_color_55 ]
  ##
  # Define instance methods to read and write boolean attributes.
  def boolean *args
args.each do |key|
  define_method key do
    name = ivar_name key
    !!(instance_variable_get(name) if instance_variables.include?(name))
  end
  define_method "#{key}?" do
    send key
  end
  define_method "#{key}=" do |arg|
    arg = false if arg == 0
    instance_variable_set(ivar_name(key), !!arg)
  end
  define_method "#{key}!" do
    send "#{key}=", true
  end
end
  end
  ##
  # Define instance methods to read and write color attributes.
  # For valid colors see COLORS
  def colors *args
args.each do |key|
  attr_reader key
  define_method "#{key}=" do |name|
    name = name.to_s.downcase.to_sym
    if COLORS.include?(name)
      instance_variable_set ivar_name(key), name
    else
      raise ArgumentError, "unknown color '#{name}'"
    end
  end
end
  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.
  def enum key, *values
aliases = {}
if values.last.is_a? Hash
  values.pop.each do |value, synonyms|
    if synonyms.is_a? Array
      synonyms.each do |synonym| aliases.store synonym, value end
    else
      aliases.store synonyms, value
    end
  end
end
values.each do |value|
  aliases.store value, value
end
define_method key do
  name = ivar_name key
  value = instance_variable_get(name) if instance_variables.include? name
  value || values.first
end
define_method "#{key}=" do |arg|
  if arg
    arg = aliases.fetch arg do
      aliases.fetch arg.to_s.downcase.gsub(/[ \-]/, '_').to_sym, arg
    end
    if values.any? do |val| val === arg end
      instance_variable_set(ivar_name(key), arg)
    else
      valid = values.collect do |val| val.inspect end.join ', '
      raise ArgumentError,
        "Invalid value '#{arg.inspect}' for #{key}. Valid values are: #{valid}"
    end
  else
    instance_variable_set ivar_name(key), values.first
  end
end
  end
end
  end
end