has_dictionary 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,55 @@
1
+ # has_dictionary Gem
2
+
3
+ ActiveRecord association with a class-based tableless enumerators.
4
+
5
+ ## install
6
+
7
+ in Gemfile add:
8
+
9
+ gem 'has_dictionary', :git=>'https://github.com/aloon/has_dictionary.git'
10
+
11
+ and execute:
12
+
13
+ $ bundle install
14
+
15
+ ## how it works
16
+
17
+ Yoy must create a class with the definitions of Enums:
18
+
19
+ models/status.rb:
20
+ ```ruby
21
+ class Status < Dictionary
22
+ self.add_item :NEW, 0
23
+ self.add_item :GENERATING, 1
24
+ self.add_item :RUN, 2
25
+ self.add_item :DELETING, 3
26
+ end
27
+ ```
28
+
29
+ In your ActiveRecord class
30
+
31
+ ```ruby
32
+ class MyClass < ActiveRecord::Base
33
+ attr_accessible :name, :status_id
34
+
35
+ has_dictionary :dic_class=>Status, :field=>:status_id, :method=>'status'
36
+ end
37
+ ```
38
+
39
+ now you can:
40
+
41
+ ```ruby
42
+ MyClass.find(1).status
43
+ # returns #<Status:0x00 @id=0, @desc="NEW">
44
+
45
+ MyClass.find(1).status.desc
46
+ # returns "NEW"
47
+
48
+ element = MyClass.find(1)
49
+ element.status_id = Status::GENERATING
50
+ elements.save
51
+ # this save element.status_id with the value 1
52
+
53
+ Status.get
54
+ #returns [#<Status:0x00 @id=0, @desc="NEW">, #<Status:0x00 @id=1, @desc="GENERATING">, #<Status:0x00 @id=2, @desc="RUN">, #<Status:0x00 @id=3, @desc="DELETING">]
55
+ ```
@@ -0,0 +1,25 @@
1
+ =begin
2
+ require 'has_dictionary/has_dictionary'
3
+ require 'has_dictionary/dictionary'
4
+
5
+ ActiveRecord::Base.send( :include, ActiveRecord::Dictionaries )
6
+ =end
7
+
8
+
9
+
10
+
11
+ require 'active_record'
12
+ require 'active_support/inflector'
13
+
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+
16
+ module HasDictionary
17
+
18
+ if defined?(ActiveRecord::Base)
19
+ require 'has_dictionary/has_dictionary'
20
+ require 'has_dictionary/dictionary'
21
+ #ActiveRecord::Base.extend ActsAsVotable::Extenders::Votable
22
+ #ActiveRecord::Base.extend HasDictionary::Dictionary
23
+ end
24
+
25
+ end
@@ -0,0 +1,32 @@
1
+ class Dictionary
2
+ attr_reader :id, :description
3
+
4
+ def initialize id, description
5
+ @id = id
6
+ @description = description
7
+ end
8
+
9
+ def self.add_item(key,value)
10
+ @hash ||= {}
11
+ @hash[key]=value
12
+ end
13
+
14
+ def self.const_missing(key)
15
+ @hash[key]
16
+ end
17
+
18
+ def self.each
19
+ @hash.each {|key,value| yield(key,value)}
20
+ end
21
+
22
+ def self.get key=nil
23
+ if key.nil?
24
+ status=[]
25
+ each { |k,v| status << new(v,k[0..-1]) }
26
+ status
27
+ else
28
+ each { |k,v| return new(v,k[0..-1]) if v==key }
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ module HasDictionary
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ end
6
+
7
+ module ClassMethods
8
+ def has_dictionary(options = {})
9
+ class_attribute :dic_options
10
+
11
+ self.dic_options = options
12
+
13
+ define_method(dic_options[:method]) {
14
+ eval "#{dic_options[:dic_class]}.get self.#{dic_options[:field]}"
15
+ }
16
+ end
17
+ end
18
+
19
+ def squawk(string)
20
+ write_attribute(self.class.yaffle_text_field, string.to_squawk)
21
+ end
22
+
23
+ end
24
+
25
+ ActiveRecord::Base.send :include, HasDictionary
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_dictionary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Gonzalez Lacasa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ description: ActiveRecord association with a class-based tableless enumerators
31
+ email:
32
+ - alooncom@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/has_dictionary.rb
38
+ - lib/has_dictionary/dictionary.rb
39
+ - lib/has_dictionary/has_dictionary.rb
40
+ - README.md
41
+ homepage: http://aloon.com
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.23
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: ActiveRecord association with a class-based tableless enumerators
65
+ test_files: []