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::WrappedDataType
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.new_from_string(str) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# 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) ⇒ Object
82
83
84
|
# 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) ⇒ Object
29
30
31
32
33
|
# 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) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# 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)) scanner.getch quoted = scanner.scan_until(/(?:^|[^\\])#{quote}/) or raise ConfigurationException, "missing end quote in argument #{args.count + 1} in data type #{input}"
args << quoted[0..-2]
else type = scanner.scan(/[\w:-]+/) or raise ConfigurationException, "missing argument #{args.count + 1} to data type #{input}"
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..]
when '['
bracket_count += 1
when ']'
bracket_count -= 1
end
type << next_bracket
end
end
args << type
end
scanner.skip(/\s*,?/)
end
args
end
|
.types ⇒ Object
35
36
37
|
# File 'lib/kafo/data_type.rb', line 35
def self.types
@keywords ? @keywords.keys : []
end
|
.unregister_type(keyword) ⇒ Object
39
40
41
|
# File 'lib/kafo/data_type.rb', line 39
def self.unregister_type(keyword)
@keywords.delete(keyword) if @keywords
end
|
Instance Method Details
#condition_value(value) ⇒ Object
88
89
90
|
# File 'lib/kafo/data_type.rb', line 88
def condition_value(value)
value.inspect
end
|
#dump_default(value) ⇒ Object
92
93
94
|
# File 'lib/kafo/data_type.rb', line 92
def dump_default(value)
%{"#{value}"}
end
|
#multivalued? ⇒ Boolean
96
97
98
|
# File 'lib/kafo/data_type.rb', line 96
def multivalued?
false
end
|
#to_s ⇒ Object
100
101
102
|
# File 'lib/kafo/data_type.rb', line 100
def to_s
self.class.name.split('::').last.downcase
end
|
#typecast(value) ⇒ Object
104
105
106
|
# File 'lib/kafo/data_type.rb', line 104
def typecast(value)
(value == 'UNDEF') ? nil : value
end
|
#valid?(value, errors = []) ⇒ Boolean
108
109
110
|
# File 'lib/kafo/data_type.rb', line 108
def valid?(value, errors = [])
true
end
|