Module: Mingle4r::CommonClassMethods

Included in:
Card, Mingle4r::Card::Attachment, Project, PropertyDefinition, User, Wiki
Defined in:
lib/mingle4r/common_class_methods.rb

Overview

ActiveResource makes connecting to rest resources very easy. However it has one problem and a big one at that. If you try setting the authentication credentials or the site or collection name, element name for the class for the second time it doesn’t work. E.g.

class Person < ActiveResource::base

self.site = 'http://localhost:9090/'

end

After sometime you change it to

Person.site = ‘org-server/my_proj/’ Person.user = ‘admin’ Person.password = ‘secret’

Then you do

Person.find(:all) => It bombs

This module provides a mechanism by which you can get rid of this problem. Extend this class in the actual class itself. Do not extend the extended class from ActiveResource::Base.

E.g.

class Person

extend CommonClassMethods

end

set the credentials

Person.site = ‘localhost:8080’ Person.user = ‘foo’ Person.password = ‘bar’

Thats it. Now create some objects

asur = Person.new(:name => ‘Asur’, :job => ‘fooling around’, :status => ‘Single and ready 2 mingle’) asur.save

Now change the class attributes

Person.site = ‘org-server/mingle’ Person.collection_name = ‘boring_people’

Now instantiate an object

rakhshas = Person.new(:name => ‘Rakhshas’, :job => ‘eating people’, :status => ‘just woke up and hungry’) rakhshas.save => Voila !!!!!!! it works

CUSTOMIZATIONS


No amount of wrapping can provide very detailed customizations. Either you have a lot of methods that are not being used or there is hardly anything at all. To oversome this problem this module was written to provide only those methods which are common to most active resource objects. However if you want to have a little more control over your active resource objects its very easy. Here’s how you would do it normally

class Person < ActiveResource::Base

def self.count
  find(:all).size
end

def occupation
  return job if job
  'Unemployed' 
end

end

To do the same thing, here’s how you do it using this library

class Person

module ClassMethods
  def count
    find(:all).size
  end
end

module InstanceMethods
  def occupation
    return job if job
    'Unemployed' 
  end
end
extend CommonClassMethods

end

The instance methods will be available as instance methods in the objects created, class methods will be available as class methods in the class of the object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth_id, *args, &block) ⇒ Object (private)



215
216
217
# File 'lib/mingle4r/common_class_methods.rb', line 215

def method_missing(meth_id, *args, &block)
  @resource_class.send(meth_id, *args, &block)
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



92
93
94
# File 'lib/mingle4r/common_class_methods.rb', line 92

def password
  @password
end

#siteObject

Returns the value of attribute site.



92
93
94
# File 'lib/mingle4r/common_class_methods.rb', line 92

def site
  @site
end

#userObject

Returns the value of attribute user.



92
93
94
# File 'lib/mingle4r/common_class_methods.rb', line 92

def user
  @user
end

Instance Method Details

#collection_nameObject

collection name for the class in which this module is extended



147
148
149
# File 'lib/mingle4r/common_class_methods.rb', line 147

def collection_name
  @collection_name || class_name.underscore.pluralize
end

#collection_name=(collection_name) ⇒ Object

sets the collection name for the class in which this module is extended



131
132
133
134
135
136
# File 'lib/mingle4r/common_class_methods.rb', line 131

def collection_name=(collection_name)
  if collection_name != self.collection_name
    @collection_name = collection_name
  end
  @collection_name
end

#element_nameObject

element name for the class in which this module is extended



152
153
154
# File 'lib/mingle4r/common_class_methods.rb', line 152

def element_name
  @element_name || class_name.underscore
end

#element_name=(element_name) ⇒ Object

sets the elment name for the class in which this module is extended



139
140
141
142
143
144
# File 'lib/mingle4r/common_class_methods.rb', line 139

def element_name=(element_name)
  if element_name != self.element_name
    @element_name = element_name
  end
  @element_name
end

#find(*args) ⇒ Object

routes to active resource find



161
162
163
164
165
166
# File 'lib/mingle4r/common_class_methods.rb', line 161

def find(*args)
  scope = args.slice!(0)
  options = args.slice!(0) || {}
  obj = @resource_class.find(scope, options)
  obj
end

#new(args = {}) ⇒ Object

creates an object of the class in which this module is extended



95
96
97
98
# File 'lib/mingle4r/common_class_methods.rb', line 95

def new(args = {})
  # @resource_class = create_resource_class() # should this be commented
  @resource_class.new(args)
end