2008年10月31日 星期五

jquery,keypress,enter,keycode


$('.serchStockInput').keypress(function (e) {
if(e.keyCode == 13) {
track();
return false;
}
});

2008年10月30日 星期四

composed_of,model,data object

http://hideto.javaeye.com/blog/77459
http://rorataspire.wordpress.com/2008/07/10/rails-composed_of/
http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html#M001663
http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful


composed_of 感覺就是用來把妳一些 field 在弄成一個 object, 當然也就可以拿這個 value object 產生出新的 object, 也可以用在find 裡面當條件用

1. class Person < ActiveRecord::Base
2. composed_of :address,
3. :class_name => "Address",
4. :mapping => [%w(address_street street),
5. %w(address_city city),
6. %w(address_state state),
7. %w(address_country country)]
8.
9. class Address
10. attr_accessor :street, :city, :state, :country
11. def initialize(street, city, state, country)
12. @street = street
13. @city = city
14. @state = state
15. @country = country
16. end
17. end

  1. person.address = Address.new(...)
  2. person.save
  1. person.address.city = "LA"
  2. person.save
這不會更新person的city字段,因為composed_of()的值對象被認為是immutable的,而只有new一個Address對象才能保存



class UserData
attr_accessor :name

def initialize(name)
@name = name
end
end

class User < ActiveRecord::Base
composed_of :user_data, :mapping =>%w(name name) <= 其實是這樣 ["name", "name"]
end

現在就可以用這樣了
>> u = UserData.new('ilake')
=> #<UserData:0xb7197f28 @name="ilake">
>> User.find(:all, :conditions => {:user_data => u})
=> [#<User id: 5, name: "ilake"]


2008年10月29日 星期三

module_eval,class_eval,singleton

http://onestepback.org/articles/10things/page027.html
http://my.donews.com/idlecat/2006/08/20/IpzenftEYkWGFjJpxPTDTJTCDOGKqTPzCaPZ/llllll
http://blackanger.blog.51cto.com/140924/84057
http://fcamel.twbbs.org/archives/2008/04/07/474/
http://tw.myblog.yahoo.com/weijenlu/article?mid=99&prev=109&next=-1

class Module
def trace_attr(sym)
self.module_eval <<-EOS <= 也可以說是動態產生 method 的方法
def #{sym}
printf "Accessing %s with value %s\n",
"#{sym}", @#{sym}.inspect
@#{sym}

end
EOS
end
end

class Dog
trace_attr :name
def initialize(string)
@name = string
end
end
p Dog.new("Fido").name


class Test
end

code =<<-EOF
def hello_module
'hello world, use module'
end
EOF Test.module_eval(code) <= 動態塞段 code 進去, method 就生了, 太神啦
p Test.new.hello_module()


Test.class_eval <<-END <= 有直接塞進class的 fu
def hello_class
' hello world, use class'
end
END
p Test.new.hello_class()

t = Test.new
class << t <= 可以直接塞method 進 t 這個 object
def hello_singleton
'hello singleton'
end
end

class << Test <= Test class也是 oject 當然也可以塞
def class_singleton
'class singleton'
end
end



class Announce < ActiveRecord::Base
require_dependency 'forum/forum_model_mixin'
include ForumModelMixin
end

module ForumModelMixin
def self.included(klass) <= klass可以知道是哪個 class include了這個lib
klass.class_eval do

has_many :forum_comments, :as => :forum
alias_method :old_destroy, :destroy
validates_presence_of :content, :subject, :user_id

def destroy(r)
if r
old_destroy
else
update_attribute(:removed_at, Time.now) if !removed_at
end
end
end
end
end

Dir,entries,glob

http://www.ruby-doc.org/core/classes/Dir.html

Dir.entries("#{RAILS_ROOT}/public/images/score/1")
=> ["victory_32.png", "..", "victory_128.png", "victory_64.png", "."]

Dir.glob("#{RAILS_ROOT}/public/images/score/1")
=> ["/home/lake/rails_app/iwakela/public/images/score/1"]

2008年10月28日 星期二

instance_variable_get,delegate

http://ihower.idv.tw/blog/archives/1719
http://blog.gugl.org/archives/41
http://www.ruby-doc.org/core/classes/Object.html#M000380
http://www.ruby-doc.org/core/classes/Module.html#M001677
http://www.ruby-doc.org/core/classes/Hash.html#M002868


KEYS_MAPPINGS.each do |key, value|
define_method key, lambda { <= 用來生 method 的 method
instance_variable_get('@hash')[value] <= 可以直接拿 @hash instance variable 的 值
}
end



KEYS_MAPPINGS = {
:ask => 'BSELL' , :bid => 'BBUY' ,
:change => 'CHANGE', :high => 'HIGH',
:target_id => 'STKID' , :target_name => 'STKNAME'
}

delegate_opts = MarketData::KEYS_MAPPINGS.keys.reject do |e|
e == :target_id || e== :target_name
end.push(:to => :market_data) <= 呼叫 名為 market_data的 instance _function, 一般常是 has_one :thing 的 thing, 不過一樣意思, 就是叫後面這個 function
delegate *delegate_opts <= delegate_opts = [:ask, :bid, :change, :high, {:to => :market_data}]
<= delegate *delegate_opts 就是 delegate :ask, :bid, :change, :high, :to => :market_data
<= delegate 可以一次傳入多個 parameter
def market_data(key=nil)
@market_data ||= MarketData.target(target_id)
key && @market_data ? @market_data[key] : @market_data <= 有傳 key 跟沒傳key return的東西不同
end



[User model]
has_one :profile

field = Profile.content_columns.inject([]) do |result, column| <= content_columns, 濾掉 _id, _count的那些
result << column.name
end.push(:to => :profile)

delegate *field

User.first.address <= 現在可以直接這樣用了

has_many,through,source,source_type,foregin_key,class_name


#我發表過forum的comments
has_many :forum_comments, :through => :forums, :source => :comments


#forum 裡有我的 comments的 forum, 找出 forum array
has_many :forums_has_comments,
:through => :comments,
:source => :record,
:source_type => 'Forum'

#我在forum 裡所有的comments, 找出 comments array
has_many :comments_in_forums,
:foreign_key => :user_id,
:class_name => 'Comment',
:conditions => {:record_type => 'Forum'}

2008年10月27日 星期一

rails,version,uninstall

rails _2.0.2_ your_old_rails_project <= 指定用哪個版本rails 去安裝
sudo gem uninstall rails -v 2.1.0 <= 移除某個版本的rails