Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
aloon committed Oct 26, 2012
0 parents commit 723c2a7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
=has_dictionary
1 change: 1 addition & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "has_dictionary"
32 changes: 32 additions & 0 deletions lib/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Dictionary
attr_reader :id, :desc

def initialize id, desc
@id = id
@desc = desc
end

def self.add_item(key,value)
@hash ||= {}
@hash[key]=value
end

def self.const_missing(key)
@hash[key]
end

def self.each
@hash.each {|key,value| yield(key,value)}
end

def self.get key=nil
if key.nil?
status=[]
each { |k,v| status << new(v,k[0..-1]) }
status
else
each { |k,v| return new(v,k[0..-1]) if v==key }
end

end
end
26 changes: 26 additions & 0 deletions lib/has_dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module ActiveRecord

module Dictionaries

def self.included( base ) #:nodoc:
base.send( :extend, ActsMethods )
end

module ActsMethods #:nodoc:

def has_dictionary(options = {})
class_attribute :dic_options

self.dic_options = options

define_method(dic_options[:method]) {
eval "#{dic_options[:dic_class]}.get self.#{dic_options[:field]}"
}
end

end

end
end

ActiveRecord::Base.send( :include, ActiveRecord::Dictionaries )

0 comments on commit 723c2a7

Please sign in to comment.