Class: Ficus
- Inherits:
-
Object
show all
- Defined in:
- lib/ficus.rb
Defined Under Namespace
Classes: Error, MissingValidation
Constant Summary
collapse
- TYPES =
{
string: [String],
number: [Fixnum, Float],
boolean: [TrueClass, FalseClass],
array: [Array],
section: [Hash],
}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(config, opts = {}) ⇒ Ficus
Returns a new instance of Ficus.
26
27
28
29
30
31
|
# File 'lib/ficus.rb', line 26
def initialize(config, opts = {})
self.config = config
self.opts = opts
self.templates = {}
self.errors = []
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
16
17
18
|
# File 'lib/ficus.rb', line 16
def config
@config
end
|
#errors ⇒ Object
Returns the value of attribute errors.
16
17
18
|
# File 'lib/ficus.rb', line 16
def errors
@errors
end
|
#opts ⇒ Object
Returns the value of attribute opts.
16
17
18
|
# File 'lib/ficus.rb', line 16
def opts
@opts
end
|
#schema ⇒ Object
Returns the value of attribute schema.
16
17
18
|
# File 'lib/ficus.rb', line 16
def schema
@schema
end
|
#templates ⇒ Object
Returns the value of attribute templates.
16
17
18
|
# File 'lib/ficus.rb', line 16
def templates
@templates
end
|
Class Method Details
.load(data) ⇒ Object
11
12
13
|
# File 'lib/ficus.rb', line 11
def load(data)
Ficus.new data
end
|
.load_file(filename) ⇒ Object
7
8
9
|
# File 'lib/ficus.rb', line 7
def load_file(filename)
Ficus.load(YAML.load_file(filename))
end
|
Instance Method Details
#optional(name, default, type = nil) ⇒ Object
92
93
94
95
96
97
98
99
|
# File 'lib/ficus.rb', line 92
def optional(name, default, type = nil)
value = exists?(name) ? get(name) : default
if !valid_type?(value, type)
error heritage(name), "must be #{type}"
else
self.config[name] = value
end
end
|
#required(name, type = nil) ⇒ Object
101
102
103
104
105
106
107
|
# File 'lib/ficus.rb', line 101
def required(name, type = nil)
if self.config.key?(name)
error heritage(name), "must be #{type}" unless self.valid_type? get(name), type
else
error heritage(name), "undefined"
end
end
|
#section(name, section_opts = {}, &block) ⇒ Object
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
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/ficus.rb', line 53
def section(name, section_opts = {}, &block)
if sections(name).nil?
return if section_opts.fetch(:optional, false)
msg = (name.class == Regexp) ? "no matches in #{heritage}" : "undefined"
error heritage(name), msg
return
end
sections(name).each do |key|
if !self.valid_type?(self.get(key), :section)
error heritage(key), "must be section"
next
end
validation_block = block if block_given?
template_name = section_opts.fetch(:template, false)
if template_name
if !self.templates.key?(template_name)
error heritage(key), "undefined template #{template_name}"
next
end
validation_block = self.templates[template_name]
end
leaf = Ficus.new self.get(key), heritage: heritage(key)
leaf.validation(&validation_block) unless validation_block.nil?
if leaf.valid?
self.config[key] = leaf.config
else
self.errors += leaf.errors
end
end
end
|
#template(name, &block) ⇒ Object
49
50
51
|
# File 'lib/ficus.rb', line 49
def template(name, &block)
self.templates[name] = block
end
|
#valid? ⇒ Boolean
38
39
40
41
42
43
|
# File 'lib/ficus.rb', line 38
def valid?
raise MissingValidation.new('no validation block specified') if self.schema.nil?
self.errors = []
instance_eval(&self.schema)
return self.errors.empty?
end
|
#validation(&block) ⇒ Object
33
34
35
36
|
# File 'lib/ficus.rb', line 33
def validation(&block)
self.schema = block
return self
end
|