Module: Spreadsheet::Datatypes

Includes:
Compatibility
Included in:
Column, Font, Format, Row
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
# 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,
         :black, :white, :olive, :teal, :violet, :burgundy, :cream,
         :light_cyan, :dark_purple, :dark_blue, :lavender, :red, :lime,
         :blue, :yellow, :magenta, :cyan, :brown, :green, :navy, :silver,
         :gray, :orange, :purple, :sky_blue, :sea_green, :canary_yellow,
         :cornflower, :pink, :fuchsia, :peach, :medium_blue, :turquoise,
         :yellow_green, :yellow_orange, :burnt_orange, :red_orange,
         :dark_violet, :dark_gray, :midnight_blue, :border, :pattern_bg,
         :dialog_bg, :chart_text, :chart_bg, :chart_border, :tooltip_bg,
         :tooltip_text, :text, :aqua, :grey]

  ##
  # 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