Class: Apiture::Swagger::Node

Inherits:
Object
  • Object
show all
Includes:
Utils::Inflections
Defined in:
lib/apiture/swagger/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Inflections

acronym_regex, acronyms, #camelize, #constantize, #underscore

Constructor Details

#initializeNode

Returns a new instance of Node.



89
90
91
# File 'lib/apiture/swagger/node.rb', line 89

def initialize
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



87
88
89
# File 'lib/apiture/swagger/node.rb', line 87

def errors
  @errors
end

Class Method Details

.attribute(name, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/apiture/swagger/node.rb', line 33

def attribute(name, options = {})
  name = name.to_sym
  (@attribute_names ||= []) << name
  if options[:type] == :boolean
    attr_accessor name
    define_method("#{name}?".to_sym) do
      !!send(name)
    end
  elsif options[:symbolize]
    attr_reader name
    define_method("#{name}=".to_sym) do |value|
      instance_variable_set("@#{name}".to_sym, value.nil? ? nil : value.to_sym)
    end
  else
    attr_accessor name
  end
  (@validates_children ||= []) << name if options[:validate]
end

.attribute_namesObject



21
22
23
# File 'lib/apiture/swagger/node.rb', line 21

def attribute_names
  @attribute_names || []
end

.collect_errors(node, all_errors = []) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/apiture/swagger/node.rb', line 64

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



58
59
60
61
62
# File 'lib/apiture/swagger/node.rb', line 58

def hash(name, options = {})
  attr_accessor name
  (@hash_names ||= []) << name
  (@validates_children ||= []) << name if options[:validate]
end

.hash_namesObject



29
30
31
# File 'lib/apiture/swagger/node.rb', line 29

def hash_names
  @hash_names || []
end

.inherited(base) ⇒ Object



14
15
16
17
18
19
# File 'lib/apiture/swagger/node.rb', line 14

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)
  base.instance_variable_set(:@validates_children, nil)
end

.list(name, options = {}) ⇒ Object



52
53
54
55
56
# File 'lib/apiture/swagger/node.rb', line 52

def list(name, options = {})
  attr_accessor name
  (@list_names ||= []) << name
  (@validates_children ||= []) << name if options[:validate]
end

.list_namesObject



25
26
27
# File 'lib/apiture/swagger/node.rb', line 25

def list_names
  @list_names || []
end

Instance Method Details

#collect_errors(all_errors = []) ⇒ Object



105
106
107
108
# File 'lib/apiture/swagger/node.rb', line 105

def collect_errors(all_errors = [])
  validate; all_errors.concat(errors)
  self.class.collect_errors(self, all_errors)
end

#extensionsObject



93
94
95
# File 'lib/apiture/swagger/node.rb', line 93

def extensions
  @extensions ||= {}
end

#serializable_hashObject



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
136
137
138
139
140
141
142
143
144
# File 'lib/apiture/swagger/node.rb', line 110

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_jsonObject Also known as: to_s



146
147
148
# File 'lib/apiture/swagger/node.rb', line 146

def to_json
  MultiJson.dump(serializable_hash)
end

#valid?Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/apiture/swagger/node.rb', line 100

def valid?
  validate
  @errors.any?
end

#validateObject



97
98
# File 'lib/apiture/swagger/node.rb', line 97

def validate
end