a_a_n 0.1.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,9 +3,10 @@ source "http://rubygems.org"
3
3
  # Specify your gem's dependencies in a_a_n.gemspec
4
4
  gemspec
5
5
 
6
- gem 'rspec'
7
-
8
6
  group :development do
9
7
  gem 'rspec'
8
+ end
9
+
10
+ group :development, :test do
10
11
  gem 'sqlite3'
11
12
  end
data/README CHANGED
@@ -6,12 +6,16 @@ Structure:
6
6
  belongs_to :current_university, :class_name => 'University'
7
7
 
8
8
  acts_as_aan do
9
- [ [:current_university, 'University', :name],
10
- [:home_country, 'Country', :name]]
9
+ association :current_university do
10
+ [:name]
11
+ end
12
+ association :home_country do
13
+ [:name, { :iso_3166_a2 => :country_code}]
14
+ end
11
15
  end
12
16
 
13
- validates :home_country_name, :presence => true
14
- validates :current_university_name, :presence => true
17
+ validates :home_country, :presence => true
18
+ validates :current_university, :presence => true
15
19
  end
16
20
 
17
21
  Usage:
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
20
20
  "Rakefile",
21
21
  "lib/a_a_n.rb",
22
22
  "lib/a_a_n/association_as_name.rb",
23
+ "lib/a_a_n/keeper.rb",
23
24
  "lib/a_a_n/version.rb"]
24
25
  #s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
26
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -2,6 +2,7 @@ require 'a_a_n/version'
2
2
 
3
3
  module AAN
4
4
  autoload :AssociationAsName, 'a_a_n/association_as_name'
5
+ autoload :Keeper, 'a_a_n/keeper'
5
6
  end
6
7
 
7
8
  ActiveRecord::Base.send :include, AAN::AssociationAsName
@@ -23,41 +23,46 @@ module AAN
23
23
 
24
24
  module ClassMethods
25
25
  def acts_as_aan &block
26
- aan_structure = block.call
26
+ AAN::Keeper.associations(self, &block)
27
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}
28
+ AAN::Keeper.structure[self].each_pair do |association, assoc_attrs|
29
+ assoc_attrs.each do |structure|
30
+ attribute = structure.first
31
+ aliased_method = structure.last
32
+ class_eval <<EOF
33
+ attr_accessor :#{aliased_method}
34
+ before_validation :aan_set_#{association}
35
+ after_initialize :aan_set_#{aliased_method}
33
36
 
34
- def #{aos.first}_#{aos.last}
35
- @#{aos.first}_#{aos.last} ||= #{aos.first}.try(:#{aos.last})
37
+ def #{aliased_method}
38
+ @#{aliased_method} ||= #{association}.try(:#{attribute})
36
39
  end
37
40
 
41
+ def #{association}_with_aan_assigment=(new_object)
42
+ #{AAN::Keeper.nullify_aliased_methods_for self, association}
43
+ association(:#{association}).replace(new_object)
44
+ end
45
+ alias_method_chain :#{association}=, :aan_assigment
46
+
38
47
  protected
39
48
 
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?
49
+ def aan_set_#{association}
50
+ unless #{aliased_method}.blank?
51
+ obj = association(:#{association}).klass.find_by_#{attribute} #{aliased_method}
52
+ self.#{association} = obj unless obj.nil?
44
53
  end
45
54
  end
46
55
 
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?
56
+ def aan_set_#{aliased_method}
57
+ unless #{aliased_method}.blank?
58
+ obj = association(:#{association}).klass.find_by_#{attribute} #{aliased_method}
59
+ self.#{association} = obj unless obj.nil?
51
60
  end
52
61
  end
53
62
  EOF
63
+ end
54
64
  end
55
65
 
56
- class_eval <<EOF
57
-
58
- cattr_accessor :aan_structure
59
- @@aan_structure = #{aan_structure}
60
- EOF
61
66
  end
62
67
  end
63
68
  end
@@ -0,0 +1,48 @@
1
+ module AAN
2
+ class Keeper
3
+ cattr_reader :current_model
4
+
5
+ def self.nullify_aliased_methods_for model, assoc
6
+ aliased_methods = []
7
+ AAN::Keeper.aliases_for(model, assoc).each do |aliased_method|
8
+ aliased_methods << "@#{aliased_method} = nil"
9
+ end
10
+ aliased_methods.join("\n")
11
+ end
12
+
13
+ def self.structure
14
+ @@structures ||= {}
15
+ end
16
+
17
+ def self.[](*args)
18
+ (structure[args.first] ||= {})
19
+ end
20
+
21
+ def self.associations model, &block
22
+ @@current_model = model
23
+ instance_eval &block
24
+ end
25
+
26
+ def self.association(name, &block)
27
+ sub_structure = block.call
28
+ sub_structure.each do |element|
29
+ if element.is_a? Hash
30
+ element.symbolize_keys!
31
+ element = element.to_a.flatten
32
+ elsif
33
+ element = [element.to_sym, "#{name}_#{element}".to_sym]
34
+ end
35
+ (AAN::Keeper[current_model][name] ||= []) << element
36
+ end
37
+ end
38
+
39
+ protected
40
+
41
+ def self.aliases_for model, assoc
42
+ structure[model][assoc].collect do |params|
43
+ params.last
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -1,3 +1,3 @@
1
1
  module AAN
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: a_a_n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-19 00:00:00.000000000Z
12
+ date: 2012-01-30 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &73043240 !ruby/object:Gem::Requirement
16
+ requirement: &74437120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *73043240
24
+ version_requirements: *74437120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activerecord
27
- requirement: &73042980 !ruby/object:Gem::Requirement
27
+ requirement: &74436820 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 3.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *73042980
35
+ version_requirements: *74436820
36
36
  description: ! "Whenever you need assign an association by its attribute,\n like name,
37
37
  this gem comes to busines."
38
38
  email:
@@ -47,6 +47,7 @@ files:
47
47
  - Rakefile
48
48
  - lib/a_a_n.rb
49
49
  - lib/a_a_n/association_as_name.rb
50
+ - lib/a_a_n/keeper.rb
50
51
  - lib/a_a_n/version.rb
51
52
  homepage: https://github.com/Studentify/association_as_name
52
53
  licenses: []