class Kafo::DataType
Public Class Methods
new_from_string(str)
click to toggle source
# File lib/kafo/data_type.rb, line 5 def self.new_from_string(str) keyword_re = /\A([\w:]+)(?:\[(.*)\])?\z/m.match(str) raise ConfigurationException, "data type not recognized #{str}" unless keyword_re type = @keywords[keyword_re[1]] raise ConfigurationException, "unknown data type #{keyword_re[1]}" unless type if type.is_a?(String) new_from_string(type) else args = if keyword_re[2] hash_re = keyword_re[2].match(/\A\s*\{(.*)\}\s*\z/m) if hash_re [parse_hash(hash_re[1])] else split_arguments(keyword_re[2]) end else [] end type.new(*args) end end
parse_hash(input)
click to toggle source
# File lib/kafo/data_type.rb, line 82 def self.parse_hash(input) Hash[input.scan(%r{\s*["'/]?([\w:]+(?:\[[^\]]+\])?|.+?)["'/]?\s*=>\s*["'/]?([\w:]+(?:\[[^\]]+\])?|.+?)["'/]?\s*(?:,|$)}m)] end
register_type(keyword, type)
click to toggle source
# File lib/kafo/data_type.rb, line 29 def self.register_type(keyword, type) @keywords ||= {} raise ArgumentError, "Data type #{keyword} is already registered, cannot be re-registered" if @keywords.has_key?(keyword) @keywords[keyword] = type end
split_arguments(input)
click to toggle source
# File lib/kafo/data_type.rb, line 43 def self.split_arguments(input) scanner = StringScanner.new(input) args = [] until scanner.eos? scanner.skip(/\s*/) if %w(' " /).include?(quote = scanner.peek(1)) # quoted string, or regexp argument scanner.getch # skip first quote quoted = scanner.scan_until(/(?:^|[^\\])#{quote}/) or raise ConfigurationException, "missing end quote in argument #{args.count + 1} in data type #{input}" args << quoted[0..-2] # store unquoted value else # bare words, or Type::Name, or Type::Name[args..] type = scanner.scan(/[\w:-]+/) or raise ConfigurationException, "missing argument #{args.count + 1} to data type #{input}"# # store inner arguments as a continuation of the type string if scanner.peek(1) == '[' type << scanner.getch bracket_count = 1 until bracket_count.zero? next_bracket = scanner.scan_until(/[\[\]]/) or raise ConfigurationException, "missing close bracket in argument #{args.count + 1} in data type #{input}" case next_bracket[-1..-1] when '[' bracket_count += 1 when ']' bracket_count -= 1 end type << next_bracket end end args << type end scanner.skip(/\s*,?/) end args end
types()
click to toggle source
# File lib/kafo/data_type.rb, line 35 def self.types @keywords ? @keywords.keys : [] end
unregister_type(keyword)
click to toggle source
# File lib/kafo/data_type.rb, line 39 def self.unregister_type(keyword) @keywords.delete(keyword) if @keywords end
Public Instance Methods
condition_value(value)
click to toggle source
public interface
# File lib/kafo/data_type.rb, line 88 def condition_value(value) value.inspect end
dump_default(value)
click to toggle source
# File lib/kafo/data_type.rb, line 92 def dump_default(value) %{"#{value}"} end
multivalued?()
click to toggle source
# File lib/kafo/data_type.rb, line 96 def multivalued? false end
to_s()
click to toggle source
# File lib/kafo/data_type.rb, line 100 def to_s self.class.name.split('::').last.downcase end
typecast(value)
click to toggle source
# File lib/kafo/data_type.rb, line 104 def typecast(value) value == 'UNDEF' ? nil : value end
valid?(value, errors = [])
click to toggle source
# File lib/kafo/data_type.rb, line 108 def valid?(value, errors = []) true end