Posts Tagged ‘ruby’

default ordering with datamapper

Thanks to Adam French for this tip on the datamapper mailinglist.

You’d essentially be update’ing the default query object that all
calls to all() and first() start off with before applying the user’s
parameters

class Post
 include DataMapper::Resource
 #... other properties here
 default_scope(:default).update(:order => [:created_at.desc])
end

datamapper stand alone script (for bugreports)

#!/usr/bin/env ruby

require 'rubygems'

gem 'dm-core', '~>0.9.8'
require 'dm-core'

DataMapper::Logger.new(STDOUT, :debug)
# DataObjects::Sqlite3.logger = DataObjects::Logger.new(STDOUT, :debug)

DataMapper.setup(:default, 'sqlite3::memory:')

# here come the demo class(es)

DataMapper.auto_migrate!

# here comes initial creation of records if necessary

puts '-' * 80

# here comes the demo code demonstrating the error or whatever

merb_resource_controller now supports nested (singleton) resources

merb_resource_controller is a merb plugin that provides the default CRUD actions for controllers and allows for easy customization of the generated actions. You might have already heard of or used one of its counterparts in rails world, like make_resourceful, James Golick’s resource_controller, Ian White’s resources_controller or other projects on github. Thx a lot for the inspiration guys!

As it so happens, today merb_resource_controller learned to work with arbitrarily nested (singleton) resources AND got a massive README update :-)

So if you are a  merbivore you should really check it out and give me feedback!

What’s left to say? I say let the code do the talking …

class Articles < Application
  controlling :articles
end

running system commands with ruby

# notice the backticks
`git config --global user.email`

support for speccing datamapper models with rspec

# Put this in spec_helper.rb
module SpecSupport

  def self.unload_class(*classes)
    classes.each do |c|
      Object.send(:remove_const, c) if Object.const_defined?(c)
    end
  end

  def self.fresh_class(name, superclass = nil, &block)
    unload_class(name)
    args = superclass ? [ superclass ] : []
    Object.const_set(name, Class.new(*args))
    Object.const_get(name).class_eval(&block)
  end

  def self.fresh_model(name)
    fresh_class(name) do
      include DataMapper::Resource
      property :id,         DataMapper::Types::Serial
      property :created_at, DateTime
      property :updated_at, DateTime
    end
  end

end

dm-is-protectable

I just released dm-is-protectable which is an attempt to make it easier for datamapper users on merb or on rails to implement permission checking in their models, instead of in their controller.s Yes, I know that sounds weird! but too many permission checks are done solely in controllers these days …

debugging the call stack using puts and caller in ruby


# call this anywhere inside a method
# it will print out the call stack for the current execution
# play with the numbers to narrow down the area of interest

def print_call_stack(method_name, from, to)
  from.upto(to) { |i| p "#{method_name}[#{i}]: #{caller[i]}" }
end

ruby aliasing

irb(main):001:0> class Foo
irb(main):002:1>   alias model class
irb(main):003:1>   def bar
irb(main):004:2>     p self.model
irb(main):005:2>   end
irb(main):006:1>   def baz
irb(main):007:2>     p model
irb(main):008:2>   end
irb(main):009:1>   def self.bam
irb(main):010:2>     p model
irb(main):011:2>   end
irb(main):012:1> end
=> nil
irb(main):013:0> f = Foo.new
=> #
irb(main):014:0> f.bar
Foo
=> nil
irb(main):015:0> f.baz
Foo
=> nil
irb(main):016:0> Foo.bam
NameError: undefined local variable or method `model’ for Foo:Class
        from (irb):10:in `bam’
        from (irb):16
        from :0

irb(main):017:0> Foo.model
NoMethodError: undefined method `model’ for Foo:Class
        from (irb):17
        from :0

Thread local variables in ruby

The following is taken from the Pickaxe

As we described in the previous section, a thread can normally access any variables that are in scope when the thread is created. Variables local to the block of a thread are local to the thread, and are not shared.But what if you need per-thread variables that can be accessed by other threads — including the main thread? Thread features a special facility that allows thread-local variables to be created and accessed by name. You simply treat the thread object as if it were a Hash, writing to elements using []= and reading them back using [].

Thread.current[:thread_local_var] = :foo
Thread.current[:thread_local_var] # prints foo

class and module order relation in ruby

irb(main):001:0> class Stone
irb(main):002:1> end
=> nil
irb(main):003:0>
irb(main):004:0* module Alive
irb(main):005:1>   # superset
irb(main):006:1* end
=> nil
irb(main):007:0>
irb(main):008:0* class Reptile
irb(main):009:1>   # subset of alive creatures
irb(main):010:1*   include Alive
irb(main):011:1> end
=> Reptile
irb(main):012:0>
irb(main):013:0* class Mammal
irb(main):014:1>   # subset of alive creatures
irb(main):015:1*   include Alive
irb(main):016:1> end
=> Mammal
irb(main):017:0>
irb(main):018:0* class Dolphin < Mammal
irb(main):019:1>   # subset of alive creatures
irb(main):020:1* end
=> nil
irb(main):021:0>
irb(main):022:0* Alive > Stone
=> nil
irb(main):023:0> Alive > Reptile
=> true
irb(main):024:0> Alive > Mammal
=> true
irb(main):025:0> Alive > Dolphin
=> true
irb(main):026:0>
irb(main):027:0* Stone   < Alive
=> nil
irb(main):028:0> Reptile < Alive
=> true
irb(main):029:0> Mammal  < Alive
=> true
irb(main):030:0> Dolphin < Alive
=> true
irb(main):031:0>
irb(main):032:0* Dolphin < Mammal
=> true
irb(main):033:0> Mammal > Dolphin
=> true
irb(main):034:0>
irb(main):035:0* Dolphin > Mammal
=> false
irb(main):036:0> Mammal < Dolphin
=> false
irb(main):037:0>
irb(main):038:0* Reptile > Mammal # unrelated creatures
=> nil
irb(main):039:0> Reptile < Mammal # unrelated creatures
=> nil
irb(main):040:0> Mammal < Reptile # unrelated creatures
=> nil
irb(main):041:0> Mammal > Reptile # unrelated creatures
=> nil

« Older Entries