Class: Yammer::Resources::Base

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

Direct Known Subclasses

Group, GroupMembership, Message, Thread, 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:


100
101
102
103
104
105
106
107
108
109
110
# File 'lib/yammer/resources/base.rb', line 100

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


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

def attrs
  @attrs
end

#idObject

Returns the value of attribute id


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

def id
  @id
end

Class Method Details

.attr_accessor_deffered(*symbols) ⇒ Object (protected)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/yammer/resources/base.rb', line 73

def attr_accessor_deffered(*symbols)
  symbols.each do |key|
    # track attributes that should trigger a fetch
    model_attributes[key] = false

    # getter
    define_method(key.to_s) do
      load_deferred_attribute!(key)
      instance_variable_get("@#{key}")
    end

    # setter
    define_method("#{key}=") do |value|
      load_deferred_attribute!(key)
      if persisted? && loaded?
        @modified_attributes[key] = value
      else
        @attrs[key] = value
      end
      instance_variable_set("@#{key}", value)
    end
  end
end

.base_nameObject

Returns the non-qualified class name


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/yammer/resources/base.rb', line 23

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


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/yammer/resources/base.rb', line 47

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)

40
41
42
43
# File 'lib/yammer/resources/base.rb', line 40

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

.identity_mapObject


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

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

.model_attributesObject

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


67
68
69
# File 'lib/yammer/resources/base.rb', line 67

def model_attributes
  @model_attributes ||= {}
end

Instance Method Details

#api_handlerObject


112
113
114
# File 'lib/yammer/resources/base.rb', line 112

def api_handler
  @klass.api_handler
end

#base_nameObject


116
117
118
# File 'lib/yammer/resources/base.rb', line 116

def base_name
  @klass.base_name
end

#changesObject


128
129
130
# File 'lib/yammer/resources/base.rb', line 128

def changes
  @modified_attributes
end

#delete!Object


164
165
166
167
168
# File 'lib/yammer/resources/base.rb', line 164

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

#load!Object


140
141
142
143
144
145
# File 'lib/yammer/resources/base.rb', line 140

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

#load_deferred_attribute!(key) ⇒ Object (protected)

loads model


188
189
190
191
192
193
194
195
# File 'lib/yammer/resources/base.rb', line 188

def load_deferred_attribute!(key)
  if @attrs.empty? && persisted? && !loaded?
    load!
    if !@attrs.has_key?(key)
      raise "The key: #{key} appears not to be supported for model: #{self.base_name} \n #{@attrs.keys.inspect}"
    end
  end
end

#loaded?Boolean

Returns:

  • (Boolean)

136
137
138
# File 'lib/yammer/resources/base.rb', line 136

def loaded?
  @loaded
end

#modified?Boolean

Returns:

  • (Boolean)

132
133
134
# File 'lib/yammer/resources/base.rb', line 132

def modified?
  !changes.empty?
end

#new_record?Boolean

Returns:

  • (Boolean)

120
121
122
# File 'lib/yammer/resources/base.rb', line 120

def new_record?
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)

124
125
126
# File 'lib/yammer/resources/base.rb', line 124

def persisted?
  !new_record?
end

#reload!Object


147
148
149
150
# File 'lib/yammer/resources/base.rb', line 147

def reload!
  reset!
  load!
end

#saveObject


152
153
154
155
156
157
158
159
160
161
162
# File 'lib/yammer/resources/base.rb', line 152

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

#update(attrs = {}) ⇒ Object (protected)

set all fetchable attributes


198
199
200
201
202
203
204
205
206
207
# File 'lib/yammer/resources/base.rb', line 198

def update(attrs={})
  attrs.each do |key, value|
    send("#{key}=", value) if self.respond_to?("#{key}=")
  end
  if persisted? && !loaded?
    @loaded = @klass.model_attributes.keys.inject(true) do |result, key|
      result && @attrs.has_key?(key)
    end
  end
end