logical_model 0.5.10 → 0.5.11

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.10
1
+ 0.5.11
@@ -8,75 +8,93 @@ class LogicalModel
8
8
 
9
9
  module ClassMethods
10
10
 
11
- def has_many(key)
11
+ # @param key [String] association name
12
+ # @param options [Hash]
13
+ # @option options [String/Constant] class
14
+ def has_many(key, options = {})
12
15
  @has_many_keys ||= []
13
16
  @has_many_keys << key
14
- has_many_keys = @has_many_keys
17
+ define_association_methods(key,get_attr_class(key,options))
15
18
  end
16
19
 
20
+ # DEPRECATED!!!
21
+ # Use has_many instead
17
22
  def has_many_keys=(keys)
18
23
  @has_many_keys = keys
19
24
  attr_accessor *keys
20
25
 
21
26
  keys.each do |association|
27
+ define_association_methods(association,get_attr_class(association,{}))
28
+ end
29
+ end
22
30
 
23
- # return empty array or @association variable for each association
24
- define_method association do
25
- if instance_variable_get("@#{association}").blank?
26
- instance_variable_set("@#{association}", [])
27
- end
31
+ def has_many_keys
32
+ @has_many_keys
33
+ end
34
+
35
+ protected
28
36
 
29
- instance_variable_get("@#{association}")
30
- end
37
+ def get_attr_class(key, options)
38
+ if options[:class]
39
+ options[:class].is_a?(String) ? options[:class].constantize : options[:class]
40
+ else
41
+ key.to_s.singularize.camelize.constantize
42
+ end
43
+ end
31
44
 
32
- # this method loads the contact attributes recieved by logical model from the service
33
- define_method "#{association}=" do |params|
34
- collection = []
35
- params.each do |attr_params|
36
- if attr_params["_type"].present?
37
- attr_class = attr_params.delete("_type").to_s.constantize
38
- else
39
- attr_class = association.to_s.singularize.camelize.constantize
40
- end
41
- collection << attr_class.new(attr_params)
42
- end
43
- instance_variable_set("@#{association}", collection)
45
+ def define_association_methods(association,attr_class)
46
+
47
+ # Accessor
48
+ # return empty array or @association variable for each association
49
+ define_method association do
50
+ if instance_variable_get("@#{association}").blank?
51
+ instance_variable_set("@#{association}", [])
44
52
  end
45
53
 
46
- define_method "new_#{association.to_s.singularize}" do |attr_params|
54
+ instance_variable_get("@#{association}")
55
+ end
56
+
57
+ # Setter
58
+ # this method loads the contact attributes recieved by logical model from the service
59
+ define_method "#{association}=" do |params|
60
+ collection = []
61
+ params.each do |attr_params|
47
62
  if attr_params["_type"].present?
48
- clazz = attr_params.delete(:_type).constantize
49
- else
50
- clazz = association.to_s.singularize.camelize.constantize
63
+ attr_class = attr_params.delete("_type").to_s.constantize
51
64
  end
65
+ collection << attr_class.new(attr_params)
66
+ end
67
+ instance_variable_set("@#{association}", collection)
68
+ end
52
69
 
53
- return unless clazz
54
-
55
- temp_object = clazz.new(attr_params.merge({"#{self.json_root}_id" => self.id}))
56
- eval(association.to_s) << temp_object
57
- temp_object
70
+ # Initialize instance of associated object
71
+ define_method "new_#{association.to_s.singularize}" do |attr_params|
72
+ if attr_params["_type"].present?
73
+ clazz = attr_params.delete(:_type).constantize
74
+ else
75
+ clazz = attr_class
58
76
  end
59
77
 
60
- # this method loads the contact attributes from the html form (using nested resources conventions)
61
- define_method "#{association}_attributes=" do |key_attributes|
62
- array = []
63
- key_attributes.each do |attr_params|
64
- attr_params.to_hash.symbolize_keys!
65
- if attr_params["_type"].present?
66
- attr_class = attr_params.delete("_type").to_s.constantize
67
- else
68
- attr_class = association.to_s.singularize.camelize.constantize
69
- end
70
- array << attr_class.new(attr_params)
78
+ return unless clazz
79
+
80
+ temp_object = clazz.new(attr_params.merge({"#{self.json_root}_id" => self.id}))
81
+ eval(association.to_s) << temp_object
82
+ temp_object
83
+ end
84
+
85
+ # this method loads the contact attributes from the html form (using nested resources conventions)
86
+ define_method "#{association}_attributes=" do |key_attributes|
87
+ array = []
88
+ key_attributes.each do |attr_params|
89
+ attr_params.to_hash.symbolize_keys!
90
+ if attr_params["_type"].present?
91
+ attr_class = attr_params.delete("_type").to_s.constantize
71
92
  end
72
- instance_variable_set("@#{association}", array)
93
+ array << attr_class.new(attr_params)
73
94
  end
95
+ instance_variable_set("@#{association}", array)
74
96
  end
75
97
  end
76
-
77
- def has_many_keys
78
- @has_many_keys
79
- end
80
98
  end
81
99
  end
82
100
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "logical_model"
8
- s.version = "0.5.10"
8
+ s.version = "0.5.11"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dwayne Macgowan"]
12
- s.date = "2013-11-08"
12
+ s.date = "2014-04-19"
13
13
  s.description = "LogicalModel allows to use a resource as a model. It is based on web presentation http://www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3"
14
14
  s.email = "dwaynemac@gmail.com"
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logical_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.10
4
+ version: 0.5.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-08 00:00:00.000000000 Z
12
+ date: 2014-04-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -373,7 +373,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
373
373
  version: '0'
374
374
  segments:
375
375
  - 0
376
- hash: 449501073
376
+ hash: -888068301
377
377
  required_rubygems_version: !ruby/object:Gem::Requirement
378
378
  none: false
379
379
  requirements: