[MacRuby-devel] Manage array of objects within object's class?

Steve Clarke steve at sclarkes.me.uk
Sat May 18 00:04:44 PDT 2013


Hi Jeff,

I'm no guru but I wouldn't put the methods for dealing with the collection into the model class.

You could indeed have a class array (by using the @@prefix for your variable name) but you could not easily access it in a KVC compliant manner and therefore could not directly bind it to an NSArrayController.

Steve

On 17 May 2013, at 19:52, Jeff Dyck <fsjjeff at gmail.com> wrote:

> Hey,
> 
> I'm working on developing an app in MacRuby that uses HTTParty to consume a Web Service.  I'm hoping someone can help me wrap my head around some of the Model - View - Controller stuff...
> 
> From reading some tutorials on MacRuby (and RubyMotion), it seems that it's best practice to have all the HTTP requests for loading, saving and querying objects as part of the model, rather than in the controller (which is what I've done in the past)...
> 
> I can wrap my head around saving and deleting objects in the Model, but wondering about loading objects - in particular when there are many objects to be loaded... If my code to load the JSON is embedded in the Model as a Class Method, can I also have a Class Array to store them?  And if so can I bind that to an NSArray Controller and use that to feed my UI?
> 
> IE: I have a class of 'sites' which is basically a list of schools in our District, and simplified looks something like: 
> 
> class Site
> 	require 'httparty'
> 	include HTTParty
> 	format :json
> 	base_uri 'http://webservice.address.here'
> 
> 	attr_accessor :siteArray
> 	attr_accessor :siteArrayController
> 
> 	PROPERTIES = [:id, :code, :name, :server, :location, :saved]
> 	PROPERTIES.each { |prop| attr_accessor prop }
> 
> 	def self.getAllSites
> 		siteArray = NSMutableArray.new
>     
> 		get('/sites').parsed_response.each do |site|
> 			siteArrayController.addObject(self.new(site))
> 		end
> 	end
> 
> 	def initialize(attributes = {})
> 		attributes.each { |key, value|
> 			self.send((key.to_s + "=").to_s, value)
> 		}
>         
> 		if @id.nil? then
> 			@saved = false
> 		else
> 			@saved = true
> 		end
> 	end	
> end
> 
> Basically, I would like to call from the main App Delegate something like Site.getAllSites, which would talk to the Web Server, load and parse the json and init the actual objects, adding them to the siteArray, which would in turn populate the UI via Bindings.
> 
> So I guess my questions are:
> 1) Am I crazy trying to do it like this?
> 2) How do I define the siteArray and siteArrayController so that they are Class variables (rather than object variables) and can be bound to the UI?
> 
> Thanks in advance for anyone who can point me in the right direction.
> 
> Jeff
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> https://lists.macosforge.org/mailman/listinfo/macruby-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20130518/1f10c72f/attachment.html>


More information about the MacRuby-devel mailing list