Class: VWorkApp::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, ActiveModel::Validations
Defined in:
lib/vworkapp_ruby/base/base.rb

Direct Known Subclasses

Contact, CustomField, Location, Resource, Step, Telemetry

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



9
10
11
# File 'lib/vworkapp_ruby/base/base.rb', line 9

def initialize(attributes = {})
  self.attributes = attributes
end

Class Method Details

.hattr_accessor(*hattrs) ⇒ Object



31
32
33
34
# File 'lib/vworkapp_ruby/base/base.rb', line 31

def self.hattr_accessor(*hattrs)
  hattr_reader(*hattrs)
  hattr_writer(*hattrs)
end

.hattr_reader(*hattrs) ⇒ Object


Hattr Methods




17
18
19
20
21
22
# File 'lib/vworkapp_ruby/base/base.rb', line 17

def self.hattr_reader(*hattrs)
  hattrs.each do |hattr|
    self.read_hattrs << hattr
    attr_reader(hattr.is_a?(Hash) ? hattr.keys.first : hattr)
  end
end

.hattr_writer(*hattrs) ⇒ Object



24
25
26
27
28
29
# File 'lib/vworkapp_ruby/base/base.rb', line 24

def self.hattr_writer(*hattrs)
  hattrs.each do |hattr|
    self.write_hattrs << hattr
    attr_writer(hattr.is_a?(Hash) ? hattr.keys.first : hattr)
  end
end

.hattrsObject



50
51
52
# File 'lib/vworkapp_ruby/base/base.rb', line 50

def self.hattrs
  read_hattrs + write_hattrs
end

.read_hattrsObject



43
44
45
# File 'lib/vworkapp_ruby/base/base.rb', line 43

def self.read_hattrs
  @read_hattrs ||= Set.new
end

.write_hattrsObject



36
37
38
# File 'lib/vworkapp_ruby/base/base.rb', line 36

def self.write_hattrs
  @write_hattrs ||= Set.new
end

Instance Method Details

#attributesObject


Attributes Methods




61
62
63
# File 'lib/vworkapp_ruby/base/base.rb', line 61

def attributes
  extract_attributes(hattrs)
end

#attributes=(hash) ⇒ Object



73
74
75
# File 'lib/vworkapp_ruby/base/base.rb', line 73

def attributes=(hash)
  inject_attributes(hattrs, hash)
end

#attributes_eql?(other, *test_attrs) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/vworkapp_ruby/base/base.rb', line 93

def attributes_eql?(other, *test_attrs)
  test_attrs.each do |attribute|
    return false unless self.send(attribute.to_sym) == other.send(attribute.to_sym)
  end
  true
end

#hattrsObject



53
54
55
# File 'lib/vworkapp_ruby/base/base.rb', line 53

def hattrs
  self.class.hattrs
end

#read_attributesObject



65
66
67
# File 'lib/vworkapp_ruby/base/base.rb', line 65

def read_attributes
  extract_attributes(read_hattrs)
end

#read_attributes=(hash) ⇒ Object



81
82
83
# File 'lib/vworkapp_ruby/base/base.rb', line 81

def read_attributes=(hash)
  inject_attributes(read_hattrs, hash)
end

#read_hattrsObject



46
47
48
# File 'lib/vworkapp_ruby/base/base.rb', line 46

def read_hattrs
  self.class.read_hattrs
end

#validate_and_raiseObject


Misc Methods


Raises:

  • (Exception)


89
90
91
# File 'lib/vworkapp_ruby/base/base.rb', line 89

def validate_and_raise
  raise Exception.new(self.errors.full_messages.join("\n")) unless valid?
end

#write_attributesObject



69
70
71
# File 'lib/vworkapp_ruby/base/base.rb', line 69

def write_attributes
  extract_attributes(write_hattrs)
end

#write_attributes=(hash) ⇒ Object



77
78
79
# File 'lib/vworkapp_ruby/base/base.rb', line 77

def write_attributes=(hash)
  inject_attributes(write_hattrs, hash)
end

#write_hattrsObject



39
40
41
# File 'lib/vworkapp_ruby/base/base.rb', line 39

def write_hattrs
  self.class.write_hattrs
end