Search code examples
rubyrakeruby-on-rails-2

Ruby object with object property


I have a class that uses the AWS S3 gem and have several methods in my class that utilise the gem. My issue is, that rather than configuring it in several locations, I'd like to make it a property of my object.

In PHP, we'd do this;

<?php
  class myClass {

    private $obj;

    public function __construct() {
      $this->obj = new Object();
    }
  }
?>

And then I could use $this->obj->method() anywhere in myClass.

I am having a hard time getting similar to work in ruby.

My scenario is similar to this;

require 'aws/s3'

class ProfileVideo < ActiveRecord::Base

  def self.cleanup

    # <snip> YAML load my config etc etc

    AWS::S3::Base.establish_connection!(
      :access_key_id     => @aws_config['aws_key'],
      :secret_access_key => @aws_config['aws_secret']
    )      

  end

  def self.another_method
    # I want to use AWS::S3 here without needing to establish connection again
  end

end

I have also noticed in my class that initialize fails to execute - a simple 'puts "here"' does nothing. Considering this is a rake task and I can 'puts "here"' in the other methods. I'm not sure if maybe rake does not init like running ProfileVideo.new would?

Anyway, thanks in advance.


Solution

  • I'm not familiar with the S3 gem in particular but here are a couple ways you could go about this.

    If you simply want to make establishing the connection easier, you can create a method in your model like so:

    def open_s3
      return if @s3_opened
      AWS::S3::Base.establish_connection!(
        :access_key_id     => @aws_config['aws_key'],
        :secret_access_key => @aws_config['aws_secret']
      )
      @s3_opened = true
    end
    

    then you can call open_s3 at the top of any methods that require it and it will only open once.

    Another route you could take is to place the connection code in a before hook set to fire before any other hooks (IIRC, the order in which you define them sets the order in which they fire) and then make your calls.

    In either case, I would recommend against putting your AWS key and secret into your code. Instead, those should go into a config file is ignored by your version control system and generated on-deploy for remote systems.