class Kafo::DataType

Public Class Methods

new_from_string(str) click to toggle source
# File lib/kafo/data_type.rb, line 3
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 54
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 27
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 41
def self.split_arguments(input)
  input.scan(%r{
    \s*
      (?:
        ["'/](.*?)["'/] # quoted string, or regexp argument
        |
        ([\w:]+ (?:\[.+\])?) # bare words, or Type::Name, or Type::Name[args..]
      )
    \s*
    (?:,|$) # match to end of comma-separated arg, or the last arg
  }mx).flatten.compact
end
types() click to toggle source
# File lib/kafo/data_type.rb, line 33
def self.types
  @keywords ? @keywords.keys : []
end
unregister_type(keyword) click to toggle source
# File lib/kafo/data_type.rb, line 37
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 60
def condition_value(value)
  value.inspect
end
dump_default(value) click to toggle source
# File lib/kafo/data_type.rb, line 64
def dump_default(value)
  %Q{"#{value}"}
end
multivalued?() click to toggle source
# File lib/kafo/data_type.rb, line 68
def multivalued?
  false
end
to_s() click to toggle source
# File lib/kafo/data_type.rb, line 72
def to_s
  self.class.name.split('::').last.downcase
end
typecast(value) click to toggle source
# File lib/kafo/data_type.rb, line 76
def typecast(value)
  value == 'UNDEF' ? nil : value
end
valid?(value, errors = []) click to toggle source
# File lib/kafo/data_type.rb, line 80
def valid?(value, errors = [])
  true
end