attribute_localizer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Michael Deering
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,44 @@
1
+ h1. Attribute Localizer
2
+
3
+ p. I like to keep my Active Record models as strict as possible but I also like the further layer of protection/restriction setting database columns to not allow NULL adds. Normalizing to nil helps enforce this better by not letting '' slip through the cracks and I can still prevent those who insist on direct DB access from entering in shitty data as much as possible.
4
+
5
+ h2. Install as a Ruby gem
6
+
7
+ p. The "attribute_localizer gem":http://gemcutter.org/gems/attribute_localizer is hosted over at "Gemcutter":http://gemcutter.org
8
+
9
+ h3. Install the Attribute Localizer gem
10
+
11
+ <pre><code>sudo gem install attribute_localizer</code></pre>
12
+
13
+ h2. Install for Ruby on Rails
14
+
15
+ <pre><code lang="ruby"># config/environement.rb
16
+ config.gem 'attribute_localizer'</code></pre>
17
+
18
+ h2. Usage
19
+
20
+ This is eager loaded into Active Record. It is usable inside of other ruby classes outside of ActiveRecord by just including the module AttributeLocalizer.
21
+
22
+ <pre><code>
23
+ class Klass < ActiveRecord::Base
24
+
25
+ # Can take an array of attributes if you want
26
+ localize_attributes :name, :description
27
+
28
+ end
29
+
30
+ object = Klass.new
31
+ object.locale = :fr
32
+ object.update_attributes(:name => 'french_name', :description => 'french_description')
33
+ object.locale = :en
34
+ object.update_attributes(:name => 'english_name', :description => 'english_description')
35
+ object.reload
36
+
37
+ object.locale = nil
38
+ object.name # => Will us the Rails I18n.local that has been set at the application level in your controller or I18n.default_locale
39
+ object.locale = :fr
40
+ object.name # => Will return the french name translation pulled from 'config/locales/klasses/fr.yml' if all defaults are used.
41
+
42
+ h2. Copyright
43
+
44
+ Copyright (c) 2010 "Michael Deering(Edmonton Ruby on Rails)":http://mdeering.com See MIT-LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+ require 'spec/rake/spectask'
4
+
5
+ begin
6
+ AUTHOR = "Michael Deering"
7
+ EMAIL = "mdeering@mdeering.com"
8
+ GEM = "attribute_localizer"
9
+ HOMEPAGE = "http://github.com/mdeering/attribute_localizer"
10
+ SUMMARY = "Attribute serialization and recall for multilingual model attributes."
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |s|
14
+ s.author = AUTHOR
15
+ s.email = EMAIL
16
+ s.files = %w(install.rb install.txt MIT-LICENSE README.textile Rakefile) + Dir.glob("{rails,lib,spec}/**/*")
17
+ s.homepage = HOMEPAGE
18
+ s.name = GEM
19
+ s.require_path = 'lib'
20
+ s.summary = SUMMARY
21
+ end
22
+ Jeweler::GemcutterTasks.new
23
+ rescue LoadError
24
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
25
+ end
26
+
27
+ desc 'Default: spec tests.'
28
+ task :default => :spec
29
+
30
+ desc 'Test the attribute_localizer plugin.'
31
+ Spec::Rake::SpecTask.new('spec') do |t|
32
+ t.spec_files = FileList['spec/**/*_spec.rb']
33
+ t.spec_opts = ["-c"]
34
+ end
35
+
36
+ desc "Run all examples with RCov"
37
+ Spec::Rake::SpecTask.new('examples_with_rcov') do |t|
38
+ t.spec_files = FileList['spec/**/*_spec.rb']
39
+ t.rcov = true
40
+ t.rcov_opts = ['--exclude', '/opt,spec,Library']
41
+ end
42
+
43
+ desc 'Generate documentation for the attribute_localizer plugin.'
44
+ Rake::RDocTask.new(:rdoc) do |rdoc|
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = 'AttributeLocalizer'
47
+ rdoc.options << '--line-numbers' << '--inline-source'
48
+ rdoc.rdoc_files.include('README.textile')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ puts IO.read(File.join(File.dirname(__FILE__), 'install.txt'))
data/install.txt ADDED
@@ -0,0 +1,3 @@
1
+ -----------------------------------------------------------------------
2
+ Attribute Localizer Installed:
3
+ -----------------------------------------------------------------------
@@ -0,0 +1,98 @@
1
+ module AttributeLocalizer
2
+
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def localize_attributes(*attributes)
10
+
11
+ klass = class << self; self end
12
+
13
+ class_eval 'after_save :serialize_localizations'
14
+ class_eval 'attr_accessor :locale'
15
+ attributes.each { |attribute| class_eval "attr_accessor :#{attribute}" }
16
+
17
+ src = attributes.map do |attribute|
18
+ str = "def #{attribute}\n"
19
+ str << "@#{attribute} || I18n.t(\"\#{self.class.send :localization_namespace}.\#{localization_hash_key}.#{attribute}\", :locale => locale, :default => '')\n"
20
+ str << "end"
21
+ str
22
+ end.join("\n")
23
+
24
+ module_eval src, __FILE__, __LINE__
25
+
26
+ src = <<-end_src
27
+ def locale
28
+ @locale || I18n.locale
29
+ end
30
+ end_src
31
+
32
+ module_eval src, __FILE__, __LINE__
33
+
34
+ private
35
+
36
+ klass.send :define_method, 'i18n_file' do |locale|
37
+ "#{RAILS_ROOT}/config/locales/#{localization_namespace}/#{locale}.yml"
38
+ end
39
+
40
+ klass.send :private, 'i18n_file'
41
+
42
+ src = <<-end_src
43
+ def load_locale_data(locale)
44
+ localized_hash = YAML::load_file(i18n_file(locale))
45
+ localized_hash[locale][localization_namespace]
46
+ rescue
47
+ {}
48
+ end
49
+ end_src
50
+
51
+ klass.class_eval src, __FILE__, __LINE__
52
+
53
+ klass.send :private, 'load_locale_data'
54
+
55
+ klass.send :define_method, 'localization_namespace' do
56
+ to_s.underscore.pluralize
57
+ end
58
+
59
+ klass.send :private, 'localization_namespace'
60
+
61
+ klass.send :define_method, 'write_locale_data' do |locale, localized_hash|
62
+ File.open( i18n_file(locale), File::WRONLY|File::TRUNC|File::CREAT ) do |out|
63
+ YAML.dump( { locale => {localization_namespace => localized_hash} }, out )
64
+ end
65
+ end
66
+
67
+ klass.send :private, 'write_locale_data'
68
+
69
+ src = <<-end_src
70
+ private
71
+ def localization_hash_key
72
+ "_\#{id}".to_sym
73
+ end
74
+
75
+ def serialize_localizations
76
+ localized_hash = self.class.send :load_locale_data, locale
77
+ localized_hash[localization_hash_key] = localized_yaml
78
+ self.class.send :write_locale_data, locale, localized_hash
79
+ end
80
+
81
+ def localized_yaml
82
+ {
83
+ #{attributes.map { |attribute| ":#{attribute} => #{attribute}"}.join(',')}
84
+ }
85
+ end
86
+ end_src
87
+
88
+ module_eval src, __FILE__, __LINE__
89
+
90
+ end
91
+
92
+ alias :localize_attribute :localize_attributes
93
+
94
+ end
95
+
96
+ end
97
+
98
+ ActiveRecord::Base.send(:include, AttributeLocalizer)
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. lib attribute_localizer])
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attribute_localizer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Michael Deering
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-13 00:00:00 -06:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: mdeering@mdeering.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.textile
29
+ files:
30
+ - MIT-LICENSE
31
+ - README.textile
32
+ - Rakefile
33
+ - install.rb
34
+ - install.txt
35
+ - lib/attribute_localizer.rb
36
+ - rails/init.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/mdeering/attribute_localizer
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.6
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Attribute serialization and recall for multilingual model attributes.
67
+ test_files: []
68
+