a_a_n 0.1.2 → 0.2.3
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/Gemfile +3 -2
- data/README +8 -4
- data/a_a_n.gemspec +1 -0
- data/lib/a_a_n.rb +1 -0
- data/lib/a_a_n/association_as_name.rb +26 -21
- data/lib/a_a_n/keeper.rb +48 -0
- data/lib/a_a_n/version.rb +1 -1
- metadata +7 -6
data/Gemfile
CHANGED
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
|
-
|
10
|
-
[:
|
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 :
|
14
|
-
validates :
|
17
|
+
validates :home_country, :presence => true
|
18
|
+
validates :current_university, :presence => true
|
15
19
|
end
|
16
20
|
|
17
21
|
Usage:
|
data/a_a_n.gemspec
CHANGED
@@ -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) }
|
data/lib/a_a_n.rb
CHANGED
@@ -23,41 +23,46 @@ module AAN
|
|
23
23
|
|
24
24
|
module ClassMethods
|
25
25
|
def acts_as_aan &block
|
26
|
-
|
26
|
+
AAN::Keeper.associations(self, &block)
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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 #{
|
35
|
-
@#{
|
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_#{
|
41
|
-
unless #{
|
42
|
-
obj =
|
43
|
-
self.#{
|
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_#{
|
48
|
-
unless #{
|
49
|
-
obj =
|
50
|
-
self.#{
|
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
|
data/lib/a_a_n/keeper.rb
ADDED
@@ -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
|
data/lib/a_a_n/version.rb
CHANGED
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.
|
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-
|
12
|
+
date: 2012-01-30 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
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: *
|
24
|
+
version_requirements: *74437120
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activerecord
|
27
|
-
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: *
|
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: []
|