Class: Ansible::Ruby::Models::Base
- Inherits:
-
Object
- Object
- Ansible::Ruby::Models::Base
show all
- Includes:
- ActiveModel::Model, ActiveModel::Validations
- Defined in:
- lib/ansible/ruby/models/base.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(args = {}) ⇒ Base
13
14
15
16
|
# File 'lib/ansible/ruby/models/base.rb', line 13
def initialize(args = {})
super
@set_vars = args.keys
end
|
Class Method Details
.attr_options(name) ⇒ Object
26
27
28
|
# File 'lib/ansible/ruby/models/base.rb', line 26
def attr_options(name)
@attr_options[name]
end
|
.attribute(name, options = {}) ⇒ Object
19
20
21
22
23
24
|
# File 'lib/ansible/ruby/models/base.rb', line 19
def attribute(name, options = {})
attr_accessor name
@attr_options ||= {}
for_name = @attr_options[name] ||= {}
for_name.merge! options
end
|
.validates(*attributes) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/ansible/ruby/models/base.rb', line 30
def validates(*attributes)
super
hash = attributes.length > 1 && attributes[1]
type_validator = hash && hash[:type]
return unless type_validator
if type_validator.is_a?(TypeGeneric)
name = attributes[0]
for_name = @attr_options[name] ||= {}
for_name[:generic] = type_validator.klass
end
end
|
Instance Method Details
#to_h ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/ansible/ruby/models/base.rb', line 44
def to_h
validate!
Hash[
@set_vars.map do |key|
value = send key
options = self.class.attr_options(key)
value = hashify value
generic_type = options[:generic]
value = convert_generic generic_type, value if generic_type
key = options[:original_name] || key
[key, value]
end.compact]
end
|