Class: ITGlue::Asset::Base
- Inherits:
-
Object
- Object
- ITGlue::Asset::Base
show all
- Extended by:
- Relatable
- Defined in:
- lib/itglue/asset/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Relatable
children, parent
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
82
83
84
85
|
# File 'lib/itglue/asset/base.rb', line 82
def initialize(attributes = {})
raise ITGlueAssetError.new('cannot instantiate base') if self == Base
@attributes = Attributes.new(attributes)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/itglue/asset/base.rb', line 138
def method_missing(method, *args)
method_name = method.to_s
arg_count = args.length
if method_name.chomp!('=')
raise ArgumentError.new("wrong number of arguments (#{arg_count} for 1)") if arg_count != 1
@attributes.assign_attribute(method_name, args[0])
elsif arg_count == 0
@attributes[method]
else
super
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
80
81
82
|
# File 'lib/itglue/asset/base.rb', line 80
def attributes
@attributes
end
|
#id ⇒ Object
Returns the value of attribute id.
80
81
82
|
# File 'lib/itglue/asset/base.rb', line 80
def id
@id
end
|
#type ⇒ Object
Returns the value of attribute type.
80
81
82
|
# File 'lib/itglue/asset/base.rb', line 80
def type
@type
end
|
Class Method Details
.asset_type ⇒ Object
Override in subclasses if required
12
13
14
15
|
# File 'lib/itglue/asset/base.rb', line 12
def asset_type
raise ITGlueAssetError.new('no asset_type for base') if self == Base
self.name.demodulize.pluralize.underscore.to_sym
end
|
.client ⇒ Object
68
69
70
|
# File 'lib/itglue/asset/base.rb', line 68
def client
@@client ||= Client.new
end
|
Executes a get request through the top-level path, with a filter query E.g. GET ‘/configurations?filter=HP-01’
53
54
55
56
57
|
# File 'lib/itglue/asset/base.rb', line 53
def filter(filter)
raise_method_not_available(__method__, 'is nested asset') if nested_asset?
assets = client.get(asset_type, {}, { query: { filter: filter } })
assets.map { |data| self.new_from_payload(data) }
end
|
Executes a get request through the top-level path for a specific asset E.g. GET ‘/configurations/1’
63
64
65
66
|
# File 'lib/itglue/asset/base.rb', line 63
def find(id)
data = client.get(asset_type, id: id )
self.new_from_payload(data)
end
|
Executes a get request through the top-level path E.g. GET ‘/configurations’
32
33
34
35
36
|
# File 'lib/itglue/asset/base.rb', line 32
def get
raise_method_not_available(__method__, 'is nested asset') if nested_asset?
assets = client.get(asset_type)
assets.map { |data| self.new_from_payload(data) }
end
|
.get_nested(parent) ⇒ Array<ITGlue::Asset>
Executes a get request through the nested asset path E.g. GET ‘organizations/:organization_id/relationships/configurations’
42
43
44
45
46
47
|
# File 'lib/itglue/asset/base.rb', line 42
def get_nested(parent)
raise_method_not_available(__method__, 'is top-level asset') unless parent_type
path_options = { parent: parent }
assets = client.get(asset_type, path_options)
assets.map { |data| self.new_from_payload(data) }
end
|
Instantiates a record from data payload
21
22
23
24
25
26
27
|
# File 'lib/itglue/asset/base.rb', line 21
def new_from_payload(data)
raise_method_not_available(__method__, 'not available for Base') if self == Base
asset = self.new(data[:attributes])
asset.id = data[:id]
asset.type = data[:type]
asset
end
|
Instance Method Details
#[](attribute) ⇒ Object
130
131
132
|
# File 'lib/itglue/asset/base.rb', line 130
def [](attribute)
@attributes[attribute]
end
|
#[]=(attribute, value) ⇒ Object
134
135
136
|
# File 'lib/itglue/asset/base.rb', line 134
def []=(attribute, value)
@attributes.assign_attribute(attribute, value)
end
|
#asset_type ⇒ Object
87
88
89
|
# File 'lib/itglue/asset/base.rb', line 87
def asset_type
self.class.asset_type
end
|
#assign_attributes(attributes) ⇒ Object
103
104
105
106
107
108
|
# File 'lib/itglue/asset/base.rb', line 103
def assign_attributes(attributes)
raise ArgumentError.new('attributtes must be a Hash') unless attributes.is_a?(Hash)
attributes.each do |attribute, value|
@attributes[attribute] = value
end
end
|
#changed? ⇒ Boolean
126
127
128
|
# File 'lib/itglue/asset/base.rb', line 126
def changed?
!changed_attributes.empty?
end
|
#changed_attributes ⇒ Object
110
111
112
|
# File 'lib/itglue/asset/base.rb', line 110
def changed_attributes
@attributes.changes
end
|
#dup ⇒ Object
97
98
99
100
101
|
# File 'lib/itglue/asset/base.rb', line 97
def dup
dup = self.class.new(self.attributes)
dup.type = self.type
dup
end
|
#inspect ⇒ Object
91
92
93
94
95
|
# File 'lib/itglue/asset/base.rb', line 91
def inspect
string = "#<#{self.class.name} id: #{self.id || 'nil'} "
fields = @attributes.keys.map { |field| "#{field}: #{@attributes.inspect_field(field)}" }
string << fields.join(", ") << ">"
end
|
#new_asset? ⇒ Boolean
118
119
120
|
# File 'lib/itglue/asset/base.rb', line 118
def new_asset?
!self.id
end
|
#remove_attribute(key) ⇒ Object
114
115
116
|
# File 'lib/itglue/asset/base.rb', line 114
def remove_attribute(key)
@attributes.remove_attribute(key)
end
|
#save ⇒ Object
122
123
124
|
# File 'lib/itglue/asset/base.rb', line 122
def save
new_asset? ? create : update
end
|