Class: Plangrade::Resources::Base

Inherits:
Object
  • Object
show all
Extended by:
ApiHandler
Defined in:
lib/plangrade/resources/base.rb

Direct Known Subclasses

Activity, Company, Notice, Participant, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiHandler

establish_api_handler

Constructor Details

#initialize(props = {}) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/plangrade/resources/base.rb', line 86

def initialize(props={})
  @klass               = self.class
  @modified_attributes = {}
  @new_record          = true
  @loaded              = false
  @attrs               = props
  self.id              = @attrs.delete(:id)
  self.update(@attrs)

  yield self if block_given?
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



84
85
86
# File 'lib/plangrade/resources/base.rb', line 84

def attrs
  @attrs
end

#idObject

Returns the value of attribute id.



84
85
86
# File 'lib/plangrade/resources/base.rb', line 84

def id
  @id
end

Class Method Details

.base_nameObject

Returns the non-qualified class name



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/plangrade/resources/base.rb', line 9

def base_name
  @base_name ||= begin
    word = "#{name.split(/::/).last}"
    word.gsub!(/::/, '/')
    word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
    word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
    word.tr!("-", "_")
    word.downcase!
    word
  end
end

.fetch(id) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/plangrade/resources/base.rb', line 33

def fetch(id)
  return unless identity_map
  attributes = identity_map.get("#{base_name}_#{id}")
  unless attributes
    result = api_handler.send("get_#{base_name}", id)
    attributes = result.empty? ? nil : result.body
    unless attributes.empty?
      identity_map.put("#{base_name}_#{id}", attributes)
    end
  end
  attributes
end

.get(id) ⇒ Yammer::Base

Fetches JSON reprsentation for object model with provided ‘id` and returns a model instance with attributes

Parameters:

  • id (Integer)

Returns:

  • (Yammer::Base)


26
27
28
29
# File 'lib/plangrade/resources/base.rb', line 26

def get(id)
  attrs = fetch(id)
  attrs ? new(attrs) : nil
end

.identity_mapObject



47
48
49
# File 'lib/plangrade/resources/base.rb', line 47

def identity_map
  @identity_map ||= Plangrade::Resources::IdentityMap.new
end

.model_attributesObject

Returns a hash of all attributes that are meant to trigger an HTTP request



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

def model_attributes
  @model_attributes ||= {}
end

Instance Method Details

#api_handlerObject



98
99
100
# File 'lib/plangrade/resources/base.rb', line 98

def api_handler
  @klass.api_handler
end

#base_nameObject



102
103
104
# File 'lib/plangrade/resources/base.rb', line 102

def base_name
  @klass.base_name
end

#changesObject



114
115
116
# File 'lib/plangrade/resources/base.rb', line 114

def changes
  @modified_attributes
end

#delete!Object



150
151
152
153
154
# File 'lib/plangrade/resources/base.rb', line 150

def delete!
  return if new_record?
  result = api_handler.send("delete_#{base_name}", @id)
  result.success?
end

#load!Object



126
127
128
129
130
131
# File 'lib/plangrade/resources/base.rb', line 126

def load!
  @attrs = @klass.fetch(@id)
  @loaded = true
  update(@attrs)
  self
end

#loaded?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/plangrade/resources/base.rb', line 122

def loaded?
  @loaded
end

#modified?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/plangrade/resources/base.rb', line 118

def modified?
  !changes.empty?
end

#new_record?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/plangrade/resources/base.rb', line 106

def new_record?
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/plangrade/resources/base.rb', line 110

def persisted?
  !new_record?
end

#reload!Object



133
134
135
136
# File 'lib/plangrade/resources/base.rb', line 133

def reload!
  reset!
  load!
end

#saveObject



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/plangrade/resources/base.rb', line 138

def save
  return self if ((persisted? && @modified_attributes.empty?) || @attrs.empty?)

  result = if new_record?
    api_handler.send("create_#{base_name}", @attrs)
  else
    api_handler.send("update_#{base_name}", @id, @modified_attributes)
  end
  @modified_attributes = {}
  self
end