code-box 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,41 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ InstalledFiles
7
+ _yardoc
8
+ coverage
9
+ doc/
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+
17
+ ## MAC OS
18
+ .DS_Store
19
+
20
+ ## TEXTMATE
21
+ *.tmproj
22
+ tmtags
23
+
24
+ ## EMACS
25
+ *~
26
+ \#*
27
+ .\#*
28
+
29
+ ## VIM
30
+ *.swp
31
+
32
+ ## PROJECT::GENERAL
33
+ coverage
34
+ rdoc
35
+ pkg
36
+
37
+ ## PROJECT::SPECIFIC
38
+ tmp
39
+
40
+ ### Rubymine stuff
41
+ .idea
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3-p125
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in code-attr.gemspec
4
+ gemspec
5
+
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ code-box (0.0.1)
5
+ activerecord (~> 3.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (3.2.6)
11
+ activesupport (= 3.2.6)
12
+ builder (~> 3.0.0)
13
+ activerecord (3.2.6)
14
+ activemodel (= 3.2.6)
15
+ activesupport (= 3.2.6)
16
+ arel (~> 3.0.2)
17
+ tzinfo (~> 0.3.29)
18
+ activesupport (3.2.6)
19
+ i18n (~> 0.6)
20
+ multi_json (~> 1.0)
21
+ arel (3.0.2)
22
+ builder (3.0.0)
23
+ i18n (0.6.0)
24
+ multi_json (1.3.6)
25
+ rake (0.9.2.2)
26
+ sqlite3 (1.3.6)
27
+ tzinfo (0.3.33)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ activerecord (~> 3.0)
34
+ code-box!
35
+ rake
36
+ sqlite3
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Martin Schweizer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Code::Attr
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'code-attr'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install code-attr
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rubygems'
5
+ require 'rake'
6
+ require 'bundler'
7
+ Bundler::GemHelper.install_tasks
8
+
9
+
10
+ require 'rake/testtask'
11
+ Rake::TestTask.new(:test) do |test|
12
+ test.libs << 'lib' << 'test'
13
+ test.pattern = 'test/**/test_*.rb'
14
+ test.verbose = true
15
+ end
16
+
17
+
18
+ begin
19
+ require 'rcov/rcovtask'
20
+ Rcov::RcovTask.new do |test|
21
+ test.libs << 'test'
22
+ test.pattern = 'test/**/test_*.rb'
23
+ test.verbose = true
24
+ end
25
+ rescue LoadError
26
+ task :rcov do
27
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
28
+ end
29
+ end
30
+
31
+
32
+ require 'rdoc/task'
33
+ require "code-box/version"
34
+ Rake::RDocTask.new do |rdoc|
35
+ version = CodeBox::VERSION
36
+
37
+ rdoc.rdoc_dir = 'rdoc'
38
+ rdoc.title = "code-box #{version}"
39
+ rdoc.rdoc_files.include('README*')
40
+ rdoc.rdoc_files.include('lib/**/*.rb')
41
+ end
42
+
43
+ task :default => :test
data/code-box.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require File.expand_path('../lib/code-box/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "code-box"
7
+ gem.version = CodeBox::VERSION
8
+
9
+ gem.authors = ["Martin Schweizer"]
10
+ gem.email = ["contact@verticonaut.me"]
11
+ gem.description = %q{Specify attributes as code and provide lookup by I18n-, cache- or associated and support for building code classes.}
12
+ gem.summary = %q{Specify attributes as code and provide lookup by I18n-, cache- or associated and support for building code classes.}
13
+ gem.homepage = %q{http://github.com/verticonaut/code-box}
14
+
15
+ gem.add_dependency "activerecord", "~> 3.0"
16
+
17
+ gem.add_development_dependency "activerecord", "~> 3.0"
18
+ gem.add_development_dependency "sqlite3"
19
+ gem.add_development_dependency "rake"
20
+
21
+ gem.files = `git ls-files`.split($\)
22
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
23
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
24
+ gem.require_paths = ["lib"]
25
+ gem.rdoc_options << '--charset' << 'UTF-8' << '--line-numbers'
26
+ end
27
+
@@ -0,0 +1,74 @@
1
+
2
+ # encoding: utf-8
3
+
4
+ module CodeBox
5
+
6
+ module ActsAsCode
7
+ @opts = {}
8
+
9
+ def self.[](options={})
10
+ @opts = options
11
+ self
12
+ end
13
+
14
+ def self.included(base)
15
+ base.extend(ClassMethods)
16
+ base.acts_as_code(@opts)
17
+ end
18
+
19
+
20
+ module ClassMethods
21
+ DefaultOptions = {
22
+ :code_attribute => :code,
23
+ :polymorphic => false,
24
+ :position_attribute => :position,
25
+ :uniqueness_case_sensitive => false
26
+ }
27
+
28
+ def acts_as_code(options={})
29
+ opts = DefaultOptions.merge(options)
30
+ code_attr = opts[:code_attribute]
31
+ position_attr = opts[:position_attribute]
32
+ case_sensitive = opts[:uniqueness_case_sensitive]
33
+
34
+ order_expression = if position_attr then
35
+ "coalesce(#{position_attr.to_s}, #{code_attr.to_s})"
36
+ else
37
+ code_attr.to_s
38
+ end
39
+
40
+ class_eval <<-CODE
41
+ validates_presence_of :#{code_attr}
42
+ validates_uniqueness_of :#{code_attr}#{opts[:polymorphic] ? ', :scope => :type' : ' '}, :case_sensitive => #{case_sensitive}
43
+
44
+ default_scope order('#{order_expression}')
45
+
46
+ def self.initialize_cache
47
+ all.inject({}) {|hash, obj| hash[obj.#{code_attr}] = obj; hash }
48
+ end
49
+
50
+ def self.for(code)
51
+ code_cache[code]
52
+ end
53
+
54
+ def hash
55
+ (self.class.name + '#' + #{code_attr}).hash
56
+ end
57
+
58
+ def equal?(other)
59
+ other && is_a?(other.class) && #{code_attr} == other.#{code_attr}
60
+ end
61
+ CODE
62
+
63
+ instance_eval <<-CODE
64
+ class << self
65
+ def code_cache
66
+ @code_cache ||= initialize_cache
67
+ end
68
+ end
69
+ CODE
70
+ end
71
+ end
72
+ end
73
+ end
74
+
@@ -0,0 +1,75 @@
1
+ # encoding: utf-8
2
+
3
+ module CodeBox
4
+
5
+ module CodeAttribute
6
+
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+
12
+ module ClassMethods
13
+ DefaultOptions = {
14
+ :foreign_code_attribute => :code,
15
+ :lookup => :i18n,
16
+ :code_attribute_suffix => 'code'
17
+ }
18
+
19
+ def code_attribute(*code_names)
20
+ options = code_names.extract_options!
21
+ opts = DefaultOptions.merge(options)
22
+ lookup_type = opts.delete(:lookup)
23
+ code_attr_suffix = (opts.delete(:code_attribute_suffix) || "code").to_s
24
+ foreign_code_attr_name = opts.delete(:foreign_code_attribute)
25
+
26
+ code_names.each do |code_name|
27
+ opts_copy = opts.dup
28
+ code_attr_name = (opts_copy.delete(:attribute_name) || "#{code_name}_#{code_attr_suffix}").to_s
29
+ code_class_name = opts_copy.delete(:class_name) || "::Codes::#{code_name.to_s.camelize}"
30
+
31
+ case lookup_type
32
+ when :cache
33
+ class_eval <<-RUBY_
34
+ # getter
35
+ def #{code_name}
36
+ code_class_name.constantize.for(value)
37
+ end
38
+
39
+ # setter
40
+ def #{code_name}=(code)
41
+ value = code.#{foreign_code_attr_name}
42
+ #{code_attr_name} = value
43
+ end
44
+ RUBY_
45
+ when :association
46
+ association_options = opts_copy.merge({
47
+ :class_name => "#{code_class_name}",
48
+ :foreign_key => "#{code_attr_name}".to_sym,
49
+ :primary_key => "#{foreign_code_attr_name}"
50
+ })
51
+ belongs_to "#{code_name}".to_sym, association_options
52
+ when :i18n
53
+ class_eval <<-RUBY_
54
+ # getter
55
+ def #{code_name}(locale=I18n.locale)
56
+ value = self.#{code_attr_name}
57
+ value_key = value.nil? ? :null_value : value
58
+ I18n.t("activerecord.\#{self.class.name.underscore}.values.#{code_attr_name}.\#{value_key}", :locale => locale)
59
+ end
60
+
61
+ # setter
62
+ def #{code_name}=(code)
63
+ raise "#{code_name} is a i18n code and can not be set. Use the the correct method '#{code_attr_name}='' instead."
64
+ end
65
+ RUBY_
66
+ else
67
+ raise ArgumentError, "'#{lookup_type}' is not valid. Must be one of [:code_cache, :association]"
68
+ end
69
+ end
70
+ end
71
+
72
+ alias :belongs_to_code :code_attribute
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ module CodeBox
4
+ VERSION = "0.0.1"
5
+ end
data/lib/code-box.rb ADDED
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path('code-box/code_attribute', File.dirname(__FILE__))
4
+ require File.expand_path('code-box/acts_as_code' , File.dirname(__FILE__))
5
+
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ require 'helper'
4
+
5
+ class TestActsAsCode < Test::Unit::TestCase
6
+
7
+ def setup
8
+
9
+ end
10
+
11
+ def test_author_attributes_available
12
+ assert_equal 1,1
13
+ end
14
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ require 'helper'
4
+
5
+ class TestCodeAttribute < Test::Unit::TestCase
6
+
7
+ def setup
8
+ end
9
+
10
+ def test_acts_as_code_i18n_lookup
11
+ obj = Code::SampleClass.new(gender_code: 'f', country_iso: 'de')
12
+
13
+ assert_equal('f', obj.gender_code)
14
+ assert_equal('female', obj.gender)
15
+
16
+ assert_equal('de', obj.country_iso)
17
+ assert_equal('Germany', obj.country)
18
+
19
+ assert_equal('f', obj.gender_code)
20
+ assert_equal('weiblich', obj.gender(:de))
21
+
22
+ assert_equal('de', obj.country_iso)
23
+ assert_equal('Deutschland', obj.country(:de))
24
+
25
+ # I18n.locale = :de
26
+
27
+ # assert_equal(obj.gender_code, 'f')
28
+ # assert_equal(obj.gender, 'weiblich')
29
+
30
+ # assert_equal(obj.country_iso, 'de')
31
+ # assert_equal(obj.country, 'Deutschland')
32
+ end
33
+
34
+
35
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'fileutils'
4
+ require 'active_record'
5
+ require 'logger'
6
+
7
+ # ------------------------------------------------------
8
+ # Setup AR environment
9
+ # ------------------------------------------------------
10
+
11
+ # Define connection info
12
+ ActiveRecord::Base.configurations = {
13
+ "test" => {
14
+ :adapter => 'sqlite3',
15
+ :database => ':memory:'
16
+ }
17
+ }
18
+ ActiveRecord::Base.establish_connection("test")
19
+
20
+ # Setup logger
21
+ tmp = File.expand_path('../../tmp', __FILE__)
22
+ FileUtils.mkdir_p(tmp)
23
+ ActiveRecord::Base.logger = Logger.new("#{tmp}/debug.log")
24
+
25
+ # I18n
26
+ I18n.load_path += Dir[File.expand_path('resources/locale/*.yml', File.dirname(__FILE__))]
27
+ I18n.default_locale = :en
28
+
29
+ puts :i18n_load => I18n.load_path
30
+
31
+ # ------------------------------------------------------
32
+ # Inject audit-trascer
33
+ # and setup test schema
34
+ # and define models used in tests
35
+ # ------------------------------------------------------
36
+ require "code-box"
37
+
38
+ require "resources/schema.rb"
39
+ require "resources/models.rb"
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ ---
3
+ de:
4
+ activerecord:
5
+ code/sample_class:
6
+ values:
7
+ gender_code:
8
+ f: weiblich
9
+ m: männlich
10
+ country_iso:
11
+ de: Deutschland
12
+ ch: Schweiz
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ ---
3
+ en:
4
+ activerecord:
5
+ 'code/sample_class':
6
+ values:
7
+ gender_code:
8
+ f: female
9
+ m: male
10
+ country_iso:
11
+ de: Germany
12
+ ch: Switzerland
@@ -0,0 +1,13 @@
1
+ # ------------------------------------------------------
2
+ # Defined the respective AR Models
3
+ # ------------------------------------------------------
4
+ module Code
5
+ class SampleClass < ActiveRecord::Base
6
+ self.table_name = :code_sample_class
7
+
8
+ include CodeBox::CodeAttribute
9
+
10
+ code_attribute :gender
11
+ code_attribute :country, :lookup => :i18n, :code_attribute_suffix => 'iso'
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ # ------------------------------------------------------
2
+ # Defined the migrations
3
+ # ------------------------------------------------------
4
+ ActiveRecord::Schema.define(:version => 0) do
5
+
6
+ create_table :code_sample_class, :force => true do |t|
7
+ t.string :gender_code
8
+ t.string :country_iso
9
+ end
10
+
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: code-box
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Martin Schweizer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.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'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activerecord
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Specify attributes as code and provide lookup by I18n-, cache- or associated
79
+ and support for building code classes.
80
+ email:
81
+ - contact@verticonaut.me
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .rvmrc
88
+ - Gemfile
89
+ - Gemfile.lock
90
+ - LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - code-box.gemspec
94
+ - lib/code-box.rb
95
+ - lib/code-box/acts_as_code.rb
96
+ - lib/code-box/code_attribute.rb
97
+ - lib/code-box/version.rb
98
+ - test/code-box/test_acts_as_code.rb
99
+ - test/code-box/test_code_attribute.rb
100
+ - test/helper.rb
101
+ - test/resources/locale/de.yml
102
+ - test/resources/locale/en.yml
103
+ - test/resources/models.rb
104
+ - test/resources/schema.rb
105
+ homepage: http://github.com/verticonaut/code-box
106
+ licenses: []
107
+ post_install_message:
108
+ rdoc_options:
109
+ - --charset
110
+ - UTF-8
111
+ - --line-numbers
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 1.8.21
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: Specify attributes as code and provide lookup by I18n-, cache- or associated
132
+ and support for building code classes.
133
+ test_files:
134
+ - test/code-box/test_acts_as_code.rb
135
+ - test/code-box/test_code_attribute.rb
136
+ - test/helper.rb
137
+ - test/resources/locale/de.yml
138
+ - test/resources/locale/en.yml
139
+ - test/resources/models.rb
140
+ - test/resources/schema.rb