úterý 22. dubna 2008

nové nádobíčko

Podarilo se mi usetrit nejakou tu korunu, a tak jsem se rozhodl udelat si radost a koupit si nejaky novy objektiv. Zlakan popisem vyhodnosti nakupu v USA a klesajicim kurzem dolaru, jsem neodolal a domluvil se s dalsimi kamarady na objednavce z B&H photo. Samotny nakup na B&H photo byl naprosto v pohode, platba internetovou kartou eBanky sla jak po masle, doruceni bylo taktez rychle. Tezkosti nastaly az v CR - jelikoz jsme toho koupili hodne, zadrzeli nas balik na celnici, abychom deklarovali, zda se jedna o zbozi obchodniho charakteru nebo pro soukrome vyuziti. Cestou na celnici jsem zustal viset v kolone pred Prahou kvuli horicimu autobusu a do Prahy prijel samozrejme pozde. Nastesti se mi podarilo odchytnout posledniho odchaziciho celnika, ktery si ode me vzal doklad o zaplaceni, prohlaseni a slibil, ze to v pondeli vyridi a poslou do Brna. Mel jsem malicko obavy, jak dopadne zasilka v rukou Ceske Posty, ale prisla uplne v poradku. Stacilo zaplatit clo a vymerene DPH a i presto uspora oproti nakupu v CR byla znacna (priblizne 40%), takze nakup v USA nemohu nez doporucit.

úterý 1. dubna 2008

Apríl

V dnešní snůšce blábolů jsem narazil na opravdovou perlu. Článek navazující na velmi odbornou diskuzi. Doporučuji!

čtvrtek 20. března 2008

PhotoExtract

Je tomu par dni, co jsem si objednal nejake nove prislusenstvi ke svemu milackovi (DSLR - Nikon D40) a hledal jsem mistecko (komunitni server :-D), kde bych se mohl svymi dily pochlubit.
Nasel jsem velmi povedene dilo: PhotoExtract.
Navic je tento server velmi zivy a kvalita prispevovatelu je tez pomerne vysoka. Zaroven zde prave publikuji serial pro zacatecniky ve stylu HEAD First knih a je to velmi prijmne poctenicko.

Vrele doporucuji.

středa 23. ledna 2008

ar_fixture: BigDecimal and YAML problem

When we used ar_fixture plugin, we found bug with BigDecimal. When we called Amount.to_fixture, this record was generated:

amount_03246:
deleted_at:
id: 3246
value: !ruby/object:BigDecimal {}
currency: 203


classic to_yaml on Amount class works fine. So I think that problem was violated by merging hash or some other magic ;-) in to_fixture method

Solution is easy (similiar as BigDecimal and YAML don't get along): in ar_fixture.rb override BigDecimal.to_yaml method like this:

class BigDecimal
alias :_original_to_yaml :to_yaml
def to_yaml (opts={},&block)
to_s.to_yaml(opts,&block)
end
end

Removing diacritic (windows 1250) in Ruby

We received data in windows 1250 (cp1250) encoding. It's problem in Rails that works with utf8.
When you save these data in YAML, you will get accented signs as \xe1 for á (in cp1250), but strange sign in UTF8. So we need remove this encoding and replace it by ASCII signs.

You can use following code for removing diacritic:

TABLE1250 = {"e1" => "a", "e4" => "a", "e8" => "c", "ef" => "d", "e9" => "e", "ec" => "e", "ed" => "i", "be" => "l", "e5" => "l", "f2" => "n", "f3" => "o", "f6" => "o", "f5" => "o", "f4" => "o", "f8" => "r", "e0" => "r", "9a" => "s", "9d" => "t", "fa" => "u", "f9" => "u", "fc" => "u", "fb" => "u", "fd" => "y", "9e" => "z", "c1" => "A", "c4" => "A", "c8" => "C", "cf" => "D", "c9" => "E", "cc" => "E", "cd" => "I", "bc" => "L", "c5" => "L", "d2" => "N", "d3" => "O", "d6" => "O", "d5" => "O", "d4" => "O", "d8" => "R", "c0" => "R", "8a" => "S", "8d" => "T", "da" => "U", "d9" => "U", "dc" => "U", "db" => "U", "dd" => "Y", "8e" => "Z"}

def remove_diacritic str
while !str.index("\\x").nil?
idx = str.index("\\x")
str[idx, 4] = "#{TABLE1250[str[idx+2, 2].downcase]}"
end
str
end

úterý 22. ledna 2008

ar_fixture: Database to YAML

We faced problem with storing data from our application to fixtures that we want use to testing.
I found perfect plugin ar_fixtures, but installation process described on official page was broken. Maybe it could be caused by our proxy etc. So better solution is to install this plugin from gems.

This plugin allows download all data in one table to fixture (yaml file with "serialization" of each item).

We need some updates:
1.) select records that fulfil specific conditions
we change original method to_fixture

def to_fixture(limit=nil)
opts = {}
opts[:limit] = limit if limit

by new

def to_fixture(opts = {}, limit=nil)
opts[:limit] = limit if limit

and now, you can use e.g. Amount.to_fixture({:conditions => "value > 500"}) for saving amounts greater than 500 to YAML file

2.) remove bad diacritic from data (see tommorow's post)

3.) export all data
I created this simple method that loads all table names and call to_fixture method over all entity classes

def export_all
tables = ActiveRecord::Base.find_by_sql "select TABLE_NAME as name from INFORMATION_SCHEMA.TABLES"

tables.each do |t|
begin
className = t.name.classify
eval(className).to_fixture
puts "#{className} done"
rescue
puts "#{t.name} is not Entity class" # any relation
end
end
end

úterý 20. listopadu 2007

ActiveRecord and BigDecimal problem with decimal point

We are using database with Czech Collation (Locales) and Czech language uses comma as decimal point. ActiveRecord (and BigDecimal) are not written for using comma. So, when another character than digit or point appears on input, input is stopped and previous parsed text is returned as number.
We have to compute with prices that are decimal. In database is stored something like 123,45 but after calling

> Amount.find(1).value

we received 123 instead of 123.45 (#<BigDecimal:4b5850c,'0.12345e3',8(12)>)
I traced ActiveRecord and found that method find (its internal implementation) uses method instantiate and it calls object.instance_variable_set("@attributes", record) and this method is just stub for C runtime libraries.
So I created solution - override method instance_variable_set in model class Amount like this:


def instance_variable_set symbol, obj
super
if symbol == "@attributes"
input = obj["value"]
unless input.nil? or input.index(",").nil?
input[input.index(",")] = "."
@value = BigDecimal.new(input)
end
end
end

Any better solution?