Sunday, August 24, 2014

Strategy pattern ruby

In Ruby implementation base class Strategy could be removed.
class Strategy
  def output(data)
    raise "Implement me"
  end
end

class TxtOutput < Strategy
  def output(data)
    puts data.inspect
  end
end

class ConsoleTxtOutput < Strategy
  def output(data)
    puts ">> #{data.inspect}"
  end
end

class SomeLogic
  attr_accessor :output_formatter
  
  def initialize(output_formatter)
    @output_formatter = output_formatter
  end
  
  def do_some_job
    @data = { "some" => "data", "here" => 10 }
  end
  
  def print_somewhere
    @output_formatter.output(@data)
  end
end

txt_output = TxtOutput.new
console_txt_outut = ConsoleTxtOutput.new

# Specify formatter dynamically
logic = SomeLogic.new(txt_output)
logic.do_some_job
logic.print_somewhere
# Specify formatter dynamically
logic.output_formatter = console_txt_outut
logic.print_somewhere

Saturday, August 23, 2014

Template method pattern ruby



class ReportGenerator
  
  def initialize
    @report = ""
  end
  
  def output
    prepare_header
    prepare_body
    prepare_footer
    @report
  end
  
  def prepare_header
    raise "Prepare header should be implemented"
  end
  def prepare_body
    raise "Prepare body should be implemented"
  end
  def prepare_footer
    raise "Prepare footer should be implemented"
  end
end

class TxtReportGenerator < ReportGenerator
  def prepare_header
    @report << "Txt Report header \n"
  end
  def prepare_body
    @report << "body \n"
  end
  def prepare_footer
    @report << "footer \n"
  end
end

class PdfReportGenerator < ReportGenerator
  def prepare_header
    @report << "Pdf Report header \n"
  end
  def prepare_body
    @report << "some encoded body \n"
  end
  def prepare_footer
    @report << "encoded footer \n"
  end
end

txt = TxtReportGenerator.new
pdf = PdfReportGenerator.new

puts txt.output
puts pdf.output

Saturday, March 22, 2014

Mac OS Vagrant interfaces failure

If You see this:
....
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["hostonlyif", "create"]

Stderr: 0%...
Progress state: NS_ERROR_FAILURE
VBoxManage: error: Failed to create the host-only adapter
Just do this:
$ sudo /Library/StartupItems/VirtualBox/VirtualBox restart
It will re-create virtual adapters for VB installation.

Tuesday, September 24, 2013

Git reload remote branche list


Tip: When you delete branches using git branch -d <branchname> or git push origin :<branchname> the reference is only deleted locally. This means that for other team members the deleted branches are still visible when they do a git branch -a. To solve this your team members can prune the deleted branches with git remote prune <repository> , this is typically git remote prune origin.

Tuesday, August 27, 2013

Capistrano postgresql error when running cap deploy:update

....
Can't find the 'libpq-fe.h header
....
Fix:
yum install postgresql-devel # or apt-get install ....

Monday, August 12, 2013

Nginx startup scripts

Create nginx startup script in /etc/init.d/nginx
cd /opt
wget -O init-rpm.sh http://library.linode.com/assets/603-init-rpm.sh
mv /opt/init-rpm.sh /etc/rc.d/init.d/nginx
chmod +x /etc/rc.d/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

via: linode

Mac os mysql localhost / 127.0.0.1 problem

Mac os fix localhost / 127.0.0.1 problem: Add to php.ini:
pdo_mysql.default_socket=/tmp/mysql.sock
mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock
restart apache server:
sudo apachectl restart