Class: Kafo::DataType
- Inherits:
-
Object
show all
- Defined in:
- lib/kafo/data_type.rb
Direct Known Subclasses
Kafo::DataTypes::Any, Kafo::DataTypes::Array, Kafo::DataTypes::Boolean, Kafo::DataTypes::Enum, Kafo::DataTypes::Float, Kafo::DataTypes::Hash, Kafo::DataTypes::Integer, Kafo::DataTypes::NotUndef, Kafo::DataTypes::Numeric, Kafo::DataTypes::Optional, Kafo::DataTypes::Pattern, Kafo::DataTypes::Regexp, Kafo::DataTypes::Scalar, Kafo::DataTypes::String, Kafo::DataTypes::Struct, Kafo::DataTypes::Tuple, Kafo::DataTypes::TypeReference, Kafo::DataTypes::Undef, Kafo::DataTypes::Variant
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.new_from_string(str) ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/kafo/data_type.rb', line 3
def self.new_from_string(str)
keyword_re = /\A([\w:]+)(?:\[(.*)\])?\z/m.match(str)
if keyword_re
if (type = @keywords[keyword_re[1]])
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)
elsif ['Data'].include?(keyword_re[1])
DataTypes::Any.new
elsif keyword_re[1] == 'Default'
DataTypes::Enum.new('default')
elsif @keywords[keyword_re[1].capitalize] DataTypes::Optional.new(keyword_re[1].capitalize)
else
raise ConfigurationException, "unknown data type #{keyword_re[1]}"
end
else
raise ConfigurationException, "data type not recognized #{str}"
end
end
|
.parse_hash(input) ⇒ Object
50
51
52
|
# File 'lib/kafo/data_type.rb', line 50
def self.parse_hash(input)
Hash[input.scan(%r{\s*["'/]?([\w:]+(?:\[[^\]]+\])?|.+?)["'/]?\s*=>\s*["'/]?([\w:]+(?:\[[^\]]+\])?|.+?)["'/]?\s*(?:,|$)}m)]
end
|
.register_type(keyword, type) ⇒ Object
32
33
34
35
36
|
# File 'lib/kafo/data_type.rb', line 32
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) ⇒ Object
46
47
48
|
# File 'lib/kafo/data_type.rb', line 46
def self.split_arguments(input)
input.scan(%r{\s*["'/]?([\w:]+(?:\[.+\])?|.+?)["'/]?\s*(?:,|$)}m).flatten
end
|
.types ⇒ Object
38
39
40
|
# File 'lib/kafo/data_type.rb', line 38
def self.types
@keywords ? @keywords.keys : []
end
|
.unregister_type(keyword) ⇒ Object
42
43
44
|
# File 'lib/kafo/data_type.rb', line 42
def self.unregister_type(keyword)
@keywords.delete(keyword) if @keywords
end
|
Instance Method Details
#condition_value(value) ⇒ Object
56
57
58
|
# File 'lib/kafo/data_type.rb', line 56
def condition_value(value)
value.inspect
end
|
#dump_default(value) ⇒ Object
60
61
62
|
# File 'lib/kafo/data_type.rb', line 60
def dump_default(value)
%{"#{value}"}
end
|
#multivalued? ⇒ Boolean
64
65
66
|
# File 'lib/kafo/data_type.rb', line 64
def multivalued?
false
end
|
#to_s ⇒ Object
68
69
70
|
# File 'lib/kafo/data_type.rb', line 68
def to_s
self.class.name.split('::').last.downcase
end
|
#typecast(value) ⇒ Object
72
73
74
|
# File 'lib/kafo/data_type.rb', line 72
def typecast(value)
value == 'UNDEF' ? nil : value
end
|
#valid?(value, errors = []) ⇒ Boolean
76
77
78
|
# File 'lib/kafo/data_type.rb', line 76
def valid?(value, errors = [])
true
end
|