Class: ITGlue::Asset::Attributes

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/itglue/asset/base/attributes.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Attributes

Returns a new instance of Attributes.



4
5
6
7
# File 'lib/itglue/asset/base/attributes.rb', line 4

def initialize(*args)
  @changed_attribute_keys = []
  super
end

Instance Method Details

#assign_attribute(key, value) ⇒ Object



9
10
11
12
# File 'lib/itglue/asset/base/attributes.rb', line 9

def assign_attribute(key, value)
  @changed_attribute_keys << key.to_sym
  self[key] = value
end

#attributes_hashObject



23
24
25
# File 'lib/itglue/asset/base/attributes.rb', line 23

def attributes_hash
  self.to_h
end

#changesObject



36
37
38
39
40
41
# File 'lib/itglue/asset/base/attributes.rb', line 36

def changes
  attributes_hash = self.attributes_hash
  @changed_attribute_keys.each_with_object({}) do |key, changes|
    changes[key] = attributes_hash[key]
  end
end

#inspect_field(field) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/itglue/asset/base/attributes.rb', line 27

def inspect_field(field)
  value = self[field]
  if value.is_a?(String)
    value.length > 100 ? "\"#{value[0..100]}...\"" : "\"#{value}\""
  else
    value.inspect
  end
end

#keysObject



19
20
21
# File 'lib/itglue/asset/base/attributes.rb', line 19

def keys
  self.to_h.keys
end

#remove_attribute(key) ⇒ Object



14
15
16
17
# File 'lib/itglue/asset/base/attributes.rb', line 14

def remove_attribute(key)
  @changed_attribute_keys.delete(key)
  self.delete_field(key)
end