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
|
# 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) ⇒ Object
54
55
56
|
# 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) ⇒ Object
27
28
29
30
31
|
# 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) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
|
# 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 ⇒ Object
33
34
35
|
# File 'lib/kafo/data_type.rb', line 33
def self.types
@keywords ? @keywords.keys : []
end
|
.unregister_type(keyword) ⇒ Object
37
38
39
|
# File 'lib/kafo/data_type.rb', line 37
def self.unregister_type(keyword)
@keywords.delete(keyword) if @keywords
end
|
Instance Method Details
#condition_value(value) ⇒ Object
60
61
62
|
# File 'lib/kafo/data_type.rb', line 60
def condition_value(value)
value.inspect
end
|
#dump_default(value) ⇒ Object
64
65
66
|
# File 'lib/kafo/data_type.rb', line 64
def dump_default(value)
%{"#{value}"}
end
|
#multivalued? ⇒ Boolean
68
69
70
|
# File 'lib/kafo/data_type.rb', line 68
def multivalued?
false
end
|
#to_s ⇒ Object
72
73
74
|
# File 'lib/kafo/data_type.rb', line 72
def to_s
self.class.name.split('::').last.downcase
end
|
#typecast(value) ⇒ Object
76
77
78
|
# File 'lib/kafo/data_type.rb', line 76
def typecast(value)
value == 'UNDEF' ? nil : value
end
|
#valid?(value, errors = []) ⇒ Boolean
80
81
82
|
# File 'lib/kafo/data_type.rb', line 80
def valid?(value, errors = [])
true
end
|