Posty

Wyświetlanie postów z sierpień, 2010

Dynamically removing some website content in Rails 3 Way (Prototype, UJS)

<div id="search">
  <div class="results">
    Search results
  </div>
  <%= link_to 'Reset results', '#', :'data-remove' => '#search .results' %>
</div>



document.observe("dom:loaded", function() {
$(document.body).observe("click", function(event) {
element = event.findElement();
resetWhat = element.readAttribute('data-remove')
if(resetWhat) {
resetable = $$(resetWhat);
for(var i=0; i< resetable.size(); i++) {
resetable[i].remove();
}
event.stop();
return false;
}
return true;
});
});



You could implement data-show, data-hide, data-toggle, data-appear etc. in the same easy way :-) I can certainly imagine hundreds of applications for such data-* custom attributes. Hopefully, we will have some libraries which will make good use of this pattern.



You can check it live here: Li…

Module#attr_accessor_with_default by Rails3 (and its speed measured by benchmark)

Did you know that every time you change the value it will recreate reader method?

class Module
# Declare an attribute accessor with an initial default return value.
#
# To give attribute :age the initial value 25:
#
# class Person
# attr_accessor_with_default :age, 25
# end
#
# some_person.age
# => 25
# some_person.age = 26
# some_person.age
# => 26
#
# To give attribute :element_name a dynamic default value, evaluated
# in scope of self:
#
# attr_accessor_with_default(:element_name) { name.underscore }
#
def attr_accessor_with_default(sym, default = nil, &block)
raise 'Default value or block required' unless !default.nil? || block
define_method(sym, block_given? ? block : Proc.new { default })
module_eval(<<-EVAL, __FILE__, __LINE__ + 1)
def #{sym}=(value) # def age=(value)
class << self; attr_reader :#{sym} end # class << self; attr_reader :age end
@#{s…

String#constantize . Can it be faster ?

BigNumber = 100_000
Constants = ["Rails", "ActiveSupport::Autoload", "ActiveSupport::Dependencies::LoadingModule"]

def test
BigNumber.times do
Constants.each do |c|
yield c
end
end
end

Benchmark.bmbm do |x|

x.report("custom ok") do
test do |const|
const = '::' << const unless const.starts_with?("::")
eval(const)
end
end

x.report("custom wrong") do
test do |const|
eval(const)
end
end

x.report("active support implementation") do
test do |const|
names = const.split('::')
names.shift if names.empty? || names.first.empty?

constant = Object
names.each do |name|
constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
end
constant
end
end
end




And the results for Ruby 1.9.2 are:


Rehearsal --------------------------------------------------
custom ok 5.220000 0.020000…