Uno scrittoio digitale …

9 giugno 2016

can I access session variable in model

Archiviato in: rails — Tag: — admin @ 10:13

Yes you could access the session variable in model. Paste the below
code in application.rb
————–

  around_filter :you_dont_have_bloody_clue

  protected

  def you_dont_have_bloody_clue
    klasses = [ActiveRecord::Base, ActiveRecord::Base.class]
    methods = ["session", "cookies", "params", "request"]

    methods.each do |shenanigan|
      oops = instance_variable_get(:"@_#{shenanigan}")

      klasses.each do |klass|
        klass.send(:define_method, shenanigan, proc { oops })
      end
    end

    yield

    methods.each do |shenanigan|
      klasses.each do |klass|
        klass.send :remove_method, shenanigan
      end
    end

  end

————–

Now continue using the session variable in model. It works for me.
Thanks,
Sadeesh.

24 maggio 2016

msgpack

Archiviato in: code — Tag:, , , , — admin @ 09:18

It’s like JSON. but fast and small.

8 gennaio 2016

ruby-debug gem

Archiviato in: rails — Tag:, — admin @ 19:00

guide

25 giugno 2015

Simple decorator in ruby

Archiviato in: code — Tag:, , , — admin @ 15:15

require 'securerandom'

class EventQueue
  attr_accessor :messages

  def initialize
    @messages = []
  end

  def enqueue(event, args, state)
    @messages << [event, args, state]
  end
end

module Notifier
  def notify_on(event)
    guid = SecureRandom.uuid
    puts "#{guid} ==> #{event}"

    define_method("#{guid}") do |*args|
      @event_queue.enqueue(event, args, :start)
      begin
        result = __send__("#{guid}_#{event}", *args)
      ensure
        @event_queue.enqueue(event, args, :end)
      end
      result
    end

    alias_method "#{guid}_#{event}", event
    alias_method event, "#{guid}"
  end
end

class Store
  extend Notifier

  def initialize(event_queue)
    @event_queue = event_queue
  end

  def order_purchase(user, items)
    ## @event_queue.enqueue(__method__, [user, items], :start)
    do_stuff
    ## @event_queue.enqueue(__method__, [user, items], :end)
  end

  def decrease_inventory(items)
    ## @event_queue.enqueue(__method__, [items], :start)
    do_stuff
    ## @event_queue.enqueue(__method__, [items], :end)
  end

  notify_on :order_purchase
  notify_on :decrease_inventory      

  private

  def do_stuff
    puts "called from #{caller[0]}"
  end
end

puts "#" * 100
queue = EventQueue.new
store = Store.new(queue)
store.order_purchase("alan", ["book", "pen"])
puts "#" * 100
puts "messages = #{queue.messages.inspect}"
puts "#" * 100
puts Store.new(queue).methods.sort - Object.methods.sort

Output:

d02ad4da-a5ec-4ae8-b5ed-53845dd2b391 ==> order_purchase
56df2c28-b927-42d2-8e19-07d115b2c818 ==> decrease_inventory
####################################################################################################
called from /home/alan/Dropbox/clab/Aptana Studio 3 Workspace/2016-06-18/decorator.rb:188:in `d02ad4da-a5ec-4ae8-b5ed-53845dd2b391_order_purchase'
####################################################################################################
messages = [[:order_purchase, ["alan", ["book", "pen"]], :start], [:order_purchase, ["alan", ["book", "pen"]], :end]]
####################################################################################################
56df2c28-b927-42d2-8e19-07d115b2c818
56df2c28-b927-42d2-8e19-07d115b2c818_decrease_inventory
d02ad4da-a5ec-4ae8-b5ed-53845dd2b391
d02ad4da-a5ec-4ae8-b5ed-53845dd2b391_order_purchase
decrease_inventory
order_purchase


6 novembre 2014

Ruby on Rails application server solution

Archiviato in: code, rails — Tag:, — admin @ 17:24

Giusto qualche link utile per capire le alternative:

phusion-passenger-for-dummies
ruby-on-rails-server-options
rack

29 agosto 2014

Cose che servono sempre

Archiviato in: sapere è potere — Tag:, , — admin @ 10:58

Ottimo plugin di jQuery per la creazione di autocomplete in stile facebook
http://loopj.com/jquery-tokeninput/

Svn status code
http://gotofritz.net/blog/howto/svn-status-codes/

Lib’s per colorare lo STD out in ruby
https://github.com/stjohncj/cli-colorize
https://github.com/janlelis/paint

Classe per l’evaluate di template in Ruby
http://ruby-doc.org/stdlib-2.1.2/libdoc/erb/rdoc/ERB.html
see also … http://www.stuartellis.eu/articles/erb/

On rails session
https://gist.github.com/jcasimir/1210255

Cache ruby object to reduce db load
http://www.benknowscode.com/2014/08/caching-ruby-sequel-models-to-reduce.html

26 luglio 2014

Ruby and Rails

Archiviato in: Senza categoria — Tag:, — admin @ 08:15

Inizio a capirci qualcosa ed ho deciso di annotare qualche link veramente importante:

readruby.io
guides.rubyonrails.org
rspec.info
pryrepl.org

Funziona con WordPress