{"id":31,"date":"2009-05-04T08:45:27","date_gmt":"2009-05-04T08:45:27","guid":{"rendered":"http:\/\/nicholshayes.myzen.co.uk\/blog\/?p=31"},"modified":"2009-05-04T08:45:51","modified_gmt":"2009-05-04T08:45:51","slug":"temp-object","status":"publish","type":"post","link":"http:\/\/nicholshayes.co.uk\/blog\/?p=31","title":{"rendered":"Temp Object"},"content":{"rendered":"<p>I found creating search forms in Rails a little awkward because most of the built in Rails form elements expect to work with attributes of an object, whereas for a search form I just needed to pass a set of attributes. The solution was simple &#8211; create a temporary object to hold search attributes. <\/p>\n<p>When I first create the class I now use to solve this issue, I just intended to use it for search forms, but I now find I&#8217;m using it for other things such as organising the data returned by custom SQL calls.<\/p>\n<p>Here is the class definition. I&#8217;ll add a couple of other blog entries to describe how I use it.<\/p>\n<pre>\r\n# An object used to store data\r\n# Attributes can be generated both on initiation and later\r\n\r\n# Usage\r\n# t = TempObject.new('one')\r\n# t.one = \"Hi!\"\r\n# puts t.one     ---> \"Hi!\"\r\n# t.create_attribute('two')\r\n# t.two = \"Hello again\"\r\n# puts t.two     ---> \"Hello again\"\r\nclass TempObject\r\n  \r\n  def initialize(*attribute_names)\r\n    @attribute_names = Array.new\r\n    attribute_names.flatten.each do |a| \r\n      create_attribute(a)\r\n    end\r\n  end\r\n  \r\n  # Creates a new attribute with a name defined by name\r\n  # The example below generates two methods: the setter 'two=' and the getter two\r\n  #\r\n  # t = TempObject.new\r\n  # t.create_attribute('two')\r\n  # t.two = \"Hello\"\r\n  # puts t.two   ---> \"Hello\"\r\n  def create_attribute(name)\r\n    name = name.to_sym\r\n    self.class.send(:attr_accessor, name)\r\n    @attribute_names << name\r\n  end\r\n  \r\n  # Returns a list of the attributes added to this object\r\n  def attribute_names\r\n    @attribute_names\r\n  end\r\n  \r\n  # Allows data to be loaded into the attributes from a Hash.\r\n  # t = TempObject.new(:one, :two)\r\n  # t.load_data({:one => 1, 'two' => 2, :three => 3})\r\n  # puts t.one     ---> 1\r\n  # puts t.three   ---> error no method\r\n  def load_data(data = nil)\r\n    if data\r\n      @attribute_names.each do |a|\r\n        if value = data[a.to_sym] or value = data[a.to_s]\r\n          instance_variable_set(\"@#{a}\", value)\r\n        end\r\n      end\r\n    end\r\n  end\r\n  \r\n  # Allows a new object to be created from a hash in one step\r\n  # All data in hash will be used to populate new object\r\n  # t = TempObject.new_and_load_all({:one => 1, :two => 2})\r\n  # puts t.one   ---> 1\r\n  def self.new_and_load_all(data)\r\n    if data.kind_of?(Hash) and data.length > 0\r\n      object = self.new(data.keys)\r\n      object.load_data(data)\r\n    end\r\n    return object\r\n  end\r\n\r\n  # Takes an array of hashes (for example from a connection.select_all\r\n  # and returns an array of TempOjects created from the hashes\r\n  def self.create_from_array_of_hashes(array)\r\n    array.collect{|hash| TempObject.new_and_load_all(hash)}\r\n  end\r\n  \r\n  def report\r\n    report_data = Array.new\r\n    for attribute in attribute_names\r\n      value = send(attribute)\r\n      value = value.strftime(\"%a %d-%b-%y %H:%M\") if value.kind_of? Time\r\n      report_data << \"#{attribute.to_s.capitalize} : #{value}\" if value\r\n    end\r\n    return report_data\r\n  end\r\n\r\nend\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I found creating search forms in Rails a little awkward because most of the built in Rails form elements expect to work with attributes of an object, whereas for a search form I just needed to pass a set of &hellip; <a href=\"http:\/\/nicholshayes.co.uk\/blog\/?p=31\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/31"}],"collection":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=31"}],"version-history":[{"count":3,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":34,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/31\/revisions\/34"}],"wp:attachment":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}