a_a_n 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in a_a_n.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec'
7
+
8
+ group :development do
9
+ gem 'rspec'
10
+ gem 'sqlite3'
11
+ end
data/README ADDED
@@ -0,0 +1,25 @@
1
+ Structure:
2
+
3
+ class UserProfile < ActiveRecord::Base
4
+
5
+ belongs_to :home_country, :class_name => 'Country'
6
+ belongs_to :current_university, :class_name => 'University'
7
+
8
+ acts_as_aan do
9
+ [ [:current_university, 'University', :name],
10
+ [:home_country, 'Country', :name]]
11
+ end
12
+
13
+ validates :home_country_name, :presence => true
14
+ validates :current_university_name, :presence => true
15
+ end
16
+
17
+ Usage:
18
+
19
+ u = UserProfile.new
20
+ u.home_country_name = 'Netherlands'
21
+ u.current_university_name = 'University Of Amsterdam'
22
+ u.save # => true
23
+
24
+ u.home_country # => #<Country id: 12, name: "Netherlands"
25
+ u.home_country_name # => "Netherlands"
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/a_a_n.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "a_a_n/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "a_a_n"
7
+ s.version = AAN::VERSION
8
+ s.authors = ["E-Max"]
9
+ s.email = ["max@studentify.nl"]
10
+ s.homepage = "https://github.com/Studentify/association_as_name"
11
+ s.summary = %q{Association as name}
12
+ s.description = %q{Whenever you need assign an association by its attribute,
13
+ like name, this gem comes to busines.}
14
+
15
+ s.rubyforge_project = "a_a_n"
16
+
17
+ s.files = ["README",
18
+ "a_a_n.gemspec",
19
+ "Gemfile",
20
+ "Rakefile",
21
+ "lib/a_a_n.rb",
22
+ "lib/a_a_n/association_as_name.rb",
23
+ "lib/a_a_n/version.rb"]
24
+ #s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+ s.require_paths = ["lib"]
27
+
28
+ s.add_dependency("activesupport", ">= 3.0.0")
29
+ s.add_dependency("activerecord", ">= 3.0.0")
30
+ end
data/lib/a_a_n.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'a_a_n/version'
2
+
3
+ module AAN
4
+ autoload :AssociationAsName, 'a_a_n/association_as_name'
5
+ end
6
+
7
+ ActiveRecord::Base.send :include, AAN::AssociationAsName
@@ -0,0 +1,64 @@
1
+ ###
2
+ # Module that allows assing attributes by <tt>name</tt>(or any other method)
3
+ #
4
+ # Example:
5
+ #
6
+ # class UserProfile < ActiveRecord::StudetifyBase
7
+
8
+ # acts_as_aan do
9
+ # [ [:current_university, 'University', :name],
10
+ # [:home_country, 'Country', :name]]
11
+ # end
12
+ #
13
+ # attr_accessible :home_country_name, :current_university_name
14
+ #
15
+ # validates :home_country_name, :presence => true
16
+ # validates :current_university_name, :presence => true
17
+ # end
18
+ module AAN
19
+ module AssociationAsName
20
+ def self.included(base)
21
+ base.extend(ClassMethods)
22
+ end
23
+
24
+ module ClassMethods
25
+ def acts_as_aan &block
26
+ aan_structure = block.call
27
+
28
+ aan_structure.each do |aos|
29
+ class_eval <<EOF
30
+ attr_accessor :#{aos.first}_#{aos.last}
31
+ before_validation :aan_set_#{aos.first}
32
+ after_initialize :aan_set_#{aos.first}_#{aos.last}
33
+
34
+ def #{aos.first}_#{aos.last}
35
+ @#{aos.first}_#{aos.last} ||= #{aos.first}.try(:#{aos.last})
36
+ end
37
+
38
+ protected
39
+
40
+ def aan_set_#{aos.first}
41
+ unless #{aos.first}_#{aos.last}.blank?
42
+ obj = #{aos[1]}.find_by_#{aos.last} #{aos.first}_#{aos.last}
43
+ self.#{aos.first} = obj unless obj.nil?
44
+ end
45
+ end
46
+
47
+ def aan_set_#{aos.first}_#{aos.last}
48
+ unless #{aos.first}_#{aos.last}.blank?
49
+ obj = #{aos[1]}.find_by_#{aos.last} #{aos.first}_#{aos.last}
50
+ self.#{aos.first} = obj unless obj.nil?
51
+ end
52
+ end
53
+ EOF
54
+ end
55
+
56
+ class_eval <<EOF
57
+
58
+ cattr_accessor :aan_structure
59
+ @@aan_structure = #{aan_structure}
60
+ EOF
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module AAN
2
+ VERSION = "0.1.2"
3
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: a_a_n
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - E-Max
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-19 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &73043240 !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: *73043240
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &73042980 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *73042980
36
+ description: ! "Whenever you need assign an association by its attribute,\n like name,
37
+ this gem comes to busines."
38
+ email:
39
+ - max@studentify.nl
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - README
45
+ - a_a_n.gemspec
46
+ - Gemfile
47
+ - Rakefile
48
+ - lib/a_a_n.rb
49
+ - lib/a_a_n/association_as_name.rb
50
+ - lib/a_a_n/version.rb
51
+ homepage: https://github.com/Studentify/association_as_name
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project: a_a_n
71
+ rubygems_version: 1.8.10
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Association as name
75
+ test_files: []