1
# lists.rb : Different Types to deal with types where
2
# you can choose among several possibilities
3
# Copyright (C) 2006, 2009 Vincent Fourmond
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
require 'ctioga2/utils'
23
Version::register_svn_info('$Revision: 222 $', '$Date: 2011-01-11 00:52:31 +0100 (Tue, 11 Jan 2011) $')
28
# The module Types should be used for all subclasses of
29
# Type, to keep the place clean and tidy.
32
# A regular expression matching true
33
TRUE_RE = /^\s*(true|yes|on)\s*$/i
35
# A regular expression matching false
36
FALSE_RE = /^\s*(false|no(ne)?|off)\s*$/i
40
class BooleanParameter < Type
48
# Yes, this *really* is a boolean !
53
def string_to_type_internal(str)
54
if str == true or str =~ TRUE_RE
61
def type_to_string_internal(val)
69
# Booleans are a special case for option parser, as they
70
# are handled completely differently
71
def option_parser_long_option(name, biniou = nil)
72
return "--[no-]#{name}"
76
# A list of symbols. A hash :list must be provided that states
77
# the correspondance between the legal symbols that can be
78
# accepted by this parameter and their "english" name.
79
# This parameter can typically be used to prompt the user
80
# for different choices.
81
class ListParameter < Type
87
raise "type must have a :list key" unless type.has_key?(:list)
88
# We make a copy for our own purposes.
89
@hash = type[:list].dup
96
def string_to_type_internal(str)
97
if @hash.has_key?(str.to_sym)
100
raise IncorrectInput, "Invalid input: #{str} should be one of " +
101
@hash.keys.map {|s| s.to_s}.join(',')
105
def type_to_string_internal(val)
111
# A choice between different symbols based on regular expressions.
112
class REListParameter < Type
118
raise "type must have a :list key" unless type.has_key?(:list)
119
# We make a copy for our own purposes.
120
@re_hash = type[:list].dup
127
def string_to_type_internal(str)
129
if str =~ /^\s*#{k}\s*$/
133
raise IncorrectInput, "Invalid input: #{str} should match " +
134
@re_hash.keys.map {|s| s.to_s}.join(',')
137
def type_to_string_internal(val)
142
# An array of identical elements of type specified by :subtype. Defaults
144
class ArrayParameter < Type
149
# We make a copy for our own purposes.
150
subtype = type[:subtype] || {:type => :string}
151
@subtype = Type.get_type(subtype)
152
@separator = /\s*,\s*/
160
def string_to_type_internal(str)
161
ary = str.split(@separator)
162
return ary.map do |a|
163
@subtype.string_to_type(a)
167
def type_to_string_internal(val)
168
return val.map do |a|
169
@subtype.type_to_string(a)
170
end.join(@separator_out)
174
# A Type used for sets for Graphics::Styles::CircularArray
177
# \todo write a gradient stuff !!!
178
class SetParameter < ArrayParameter
183
@separator = /\s*\|\s*/
191
def string_to_type_internal(str)
193
if str =~ /(.*)\*\s*(\d+)\s*$/
197
if str =~ /^\s*gradient:(.+)--(.+),(\d+)\s*$/
198
s,e,nb = $1, $2, $3.to_i
199
s,e = @subtype.string_to_type(s),@subtype.string_to_type(e)
201
1.0/(nb - 1) # The famous off-by one...
203
warn { "Incorrect gradient number: #{nb}" }
208
array << Utils::mix_objects(e,s, i * fact)
214
# Seems that I've finally managed to understand what zip
216
array = array.zip(*([array]*(multiply-1))).flatten(1)