lib/sdl4r/sdl4r.rb in sdl4r-0.9.8 vs lib/sdl4r/sdl4r.rb in sdl4r-0.9.9

- old
+ new

@@ -43,50 +43,44 @@ # +add_quotes+:: indicates whether quotes will be added to Strings and characters (true by default) # +line_prefix+:: the line prefix to use ("" by default) # +indent+:: the indent string to use ("\t" by default) # def self.format(o, add_quotes = true, line_prefix = "", indent = "\t") - if o.is_a?(String) - if add_quotes - o_length = 0 - o.scan(/./m) { o_length += 1 } # counts the number of chars (as opposed of bytes) - if o_length == 1 - return "'" + escape(o, "'") + "'" - else - return '"' + escape(o, '"') + '"' - end - else - return escape(o) - end + case o + when String + return format_string(o) - elsif o.is_a?(Bignum) + when Symbol + return format_string(o.to_s) + + when Bignum return o.to_s + "BD" - elsif o.is_a?(Integer) + when Integer if MIN_INTEGER_32 <= o and o <= MAX_INTEGER_32 return o.to_s elsif MIN_INTEGER_64 <= o and o <= MAX_INTEGER_64 return o.to_s + "L" else return o.to_s + "BD" end - elsif o.is_a?(Float) + when Float return (o.to_s + "F") - elsif o.is_a?(Rational) + when Rational return o.to_f.to_s + "F" - elsif o.is_a?(BigDecimal) + when BigDecimal s = o.to_s('F') s.sub!(/\.0$/, "") return "#{s}BD" - elsif o.nil? + when NilClass return "null" - elsif o.is_a?(SdlBinary) + when SdlBinary encoded_o = Base64.encode64(o.bytes) encoded_o.gsub!(/[\r\n]/m, "") # Remove the EOL inserted every 60 chars if add_quotes if encoded_o.length > BASE64_WRAP_LINE_LENGTH @@ -102,11 +96,11 @@ return encoded_o # Below, we use "#{o.year}" instead of "%Y" because "%Y" always emit 4 chars at least even if # the date is before 1000. - elsif o.is_a?(DateTime) || o.is_a?(Time) + when DateTime, Time milliseconds = get_datetime_milliseconds(o) if milliseconds == 0 zone_part = o.strftime("%:z") if zone_part and zone_part != "+00:00" @@ -121,11 +115,11 @@ else return o.strftime("#{o.year}/%m/%d %H:%M:%S." + ms_part) end end - elsif o.is_a?(Date) + when Date return o.strftime("#{o.year}/%m/%d") else return o.to_s end @@ -150,17 +144,21 @@ # Returns +o+ if of the following classes: # NilClass, String, Numeric, Float, TrueClass, FalseClass, Date, DateTime, Time, # SdlTimeSpan, SdlBinary, # # Rationals are turned into Floats using Rational#to_f. + # Symbols are turned into Strings using Symbol#to_s. # def self.coerce_or_fail(o) case o when Rational return o.to_f + when Symbol + return o.to_s + when NilClass, String, Numeric, Float, TrueClass, @@ -174,10 +172,23 @@ end raise ArgumentError, "#{o.class.name} is not coercible to an SDL type" end + + # Indicates whether 'o' is coercible to a SDL litteral type. + # See #coerce_or_fail + # + def self.is_coercible?(o) + begin + coerce_or_fail(o) + true + + rescue ArgumentError + false + end + end # Validates an SDL identifier String. SDL Identifiers must start with a # Unicode letter or underscore (_) and contain only unicode letters, # digits, underscores (_), dashes(-), periods (.) and dollar signs ($). # @@ -270,18 +281,35 @@ # # hash = SDL4R.to_attribute_hash("value=1 debugging=on time=12:24:01"); # # # { "value" => 1, "debugging" => true, "time" => SdlTimeSpan.new(12, 24, 01) } # - def self.to_attribute_map(s) + def self.to_attribute_hash(s) raise ArgumentError, "'s' cannot be null" if s.nil? return read("atts " + s).child.attributes end # The following is a not so readable way to implement module private methods in Ruby: we add # private methods to the singleton class of +self+ i.e. the SDL4R module. class << self private + + # Returns the specified string 's' formatted as a SDL string. + # See SDL4R#format. + # + def format_string(s, add_quotes = true) + if add_quotes + s_length = 0 + s.scan(/./m) { s_length += 1 } # counts the number of chars (as opposed to bytes) + if s_length == 1 + return "'" + escape(s, "'") + "'" + else + return '"' + escape(s, '"') + '"' + end + else + return escape(s) + end + end # Wraps lines in "s" (by modifying it). This method only supports 1-byte character strings. # def wrap_lines_in_ascii(s, line_length, line_prefix = nil) # We could use such code if it supported any value for "line_prefix": unfortunately it is capped \ No newline at end of file