Class: Diesel::Swagger::Node
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
acronym_regex, acronyms, #camelize, #constantize, #underscore
Constructor Details
#initialize ⇒ Node
Returns a new instance of Node.
80
81
82
|
# File 'lib/diesel/swagger/node.rb', line 80
def initialize
@errors = []
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
78
79
80
|
# File 'lib/diesel/swagger/node.rb', line 78
def errors
@errors
end
|
Class Method Details
.attribute(name, options = {}) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/diesel/swagger/node.rb', line 27
def attribute(name, options = {})
name = name.to_sym
(@attribute_names ||= []) << name
attr_accessor name
if options[:type] == :boolean
define_method("#{name}?".to_sym) do
!!send(name)
end
elsif options[:symbolize]
define_method("#{name}=".to_sym) do |value|
instance_variable_set("@#{name}".to_sym, value.nil? ? nil : value.to_sym)
end
end
(@validates_children ||= []) << name if options[:validate]
end
|
.attribute_names ⇒ Object
15
16
17
|
# File 'lib/diesel/swagger/node.rb', line 15
def attribute_names
@attribute_names || []
end
|
.collect_errors(node, all_errors = []) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/diesel/swagger/node.rb', line 55
def collect_errors(node, all_errors = [])
if @validates_children
@validates_children.each do |name|
if attr_val = node.send(name)
if @hash_names && @hash_names.include?(name)
attr_val.each_pair { |k,v| v.collect_errors(all_errors) }
elsif @list_names && @list_names.include?(name)
attr_val.each { |v| v.collect_errors(all_errors) }
else
unless attr_val.respond_to? :collect_errors
raise "Expecting #{name} to be a node"
end
attr_val.collect_errors(all_errors)
end
end
end
end
all_errors
end
|
.hash(name, options = {}) ⇒ Object
49
50
51
52
53
|
# File 'lib/diesel/swagger/node.rb', line 49
def hash(name, options = {})
attr_accessor name
(@hash_names ||= []) << name
(@validates_children ||= []) << name if options[:validate]
end
|
.hash_names ⇒ Object
23
24
25
|
# File 'lib/diesel/swagger/node.rb', line 23
def hash_names
@hash_names || []
end
|
.inherited(base) ⇒ Object
9
10
11
12
13
|
# File 'lib/diesel/swagger/node.rb', line 9
def inherited(base)
base.instance_variable_set(:@attribute_names, attribute_names.dup)
base.instance_variable_set(:@list_names, list_names.dup)
base.instance_variable_set(:@hash_names, hash_names.dup)
end
|
.list(name, options = {}) ⇒ Object
43
44
45
46
47
|
# File 'lib/diesel/swagger/node.rb', line 43
def list(name, options = {})
attr_accessor name
(@list_names ||= []) << name
(@validates_children ||= []) << name if options[:validate]
end
|
.list_names ⇒ Object
19
20
21
|
# File 'lib/diesel/swagger/node.rb', line 19
def list_names
@list_names || []
end
|
Instance Method Details
#collect_errors(all_errors = []) ⇒ Object
96
97
98
99
|
# File 'lib/diesel/swagger/node.rb', line 96
def collect_errors(all_errors = [])
validate; all_errors.concat(errors)
self.class.collect_errors(self, all_errors)
end
|
#extensions ⇒ Object
84
85
86
|
# File 'lib/diesel/swagger/node.rb', line 84
def extensions
@extensions ||= {}
end
|
#serializable_hash ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/diesel/swagger/node.rb', line 101
def serializable_hash
h = {}
if self.class.attribute_names
self.class.attribute_names.each do |nm|
if v = __send__(nm)
h[camelize(nm, false)] = value_or_serializable_hash(v)
end
end
end
if self.class.list_names
self.class.list_names.each do |nm|
if arr = __send__(nm)
result = arr.map do |v|
value_or_serializable_hash(v)
end
h[camelize(nm, false)] = result if result.any?
end
end
end
if self.class.hash_names
self.class.hash_names.each do |nm|
if value_hash = __send__(nm)
result = value_hash.reduce({}) do |m,(k,v)|
m[k] = value_or_serializable_hash(v); m
end
h[camelize(nm, false)] = result if result.any?
end
end
end
h
end
|
#to_json ⇒ Object
Also known as:
to_s
137
138
139
|
# File 'lib/diesel/swagger/node.rb', line 137
def to_json
MultiJson.dump(serializable_hash)
end
|
#valid? ⇒ Boolean
91
92
93
94
|
# File 'lib/diesel/swagger/node.rb', line 91
def valid?
validate
@errors.any?
end
|
#validate ⇒ Object
88
89
|
# File 'lib/diesel/swagger/node.rb', line 88
def validate
end
|