static_models 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36852f849aa037a3e75ddd732e77a2f9eef48b74
4
- data.tar.gz: d064389a9fe474c8237f3809ca08403784fb3f95
3
+ metadata.gz: 14b52ac6a5f6d2ea3a9cd809e96dc9a5d3a9bd84
4
+ data.tar.gz: 0fe26ca5989a19dbdf39805a156e4dd4a16509dc
5
5
  SHA512:
6
- metadata.gz: 9f92c0dcdcba6cd964a29e6bbf98de5cf7f196f9309e882a79871ff5aff2031af662302daba15629d66a35c727a3335cc94b33309a6b7c6771eb6b1fd3869b19
7
- data.tar.gz: e384551d6fee147c63c2602627ad47027a07f31e99587a078f947c78ed4d5b80ea5a10dfc1a92e5d1731c997e911791b05f8a9eb6f4154048ece1126045a7e7c
6
+ metadata.gz: 8d2a70e05c48350c0a4c48b3924403a511358dab5f09c9fd9bd71a2cfe84835c938b09324ac06cdf4610c11878e4218cb49848bb29311220f3bd8148d982ec27
7
+ data.tar.gz: 0559dd452091ec6fd0954d10eb81d70a1450a2ce9e2efd4baf0f8b78890dacb90ad6e536ada21cadbcce89db90b6f1c4b5f7aded2ac559e71572158f8141607b
@@ -1,3 +1,3 @@
1
1
  module StaticModels
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
data/lib/static_models.rb CHANGED
@@ -69,14 +69,16 @@ module StaticModels
69
69
  def belongs_to(association, opts = {})
70
70
  super(association, opts) if defined?(super)
71
71
 
72
- define_method("#{association}") do
73
- klass_name = if opts[:polymorphic]
74
- send("#{association}_type")
75
- else
76
- (opts[:class_name] || association.to_s.camelize)
77
- end
72
+ expected_class = unless opts[:polymorphic]
73
+ module_name = self.class.to_s.split("::")[0..-2].join("::")
74
+ [ opts[:class_name],
75
+ "#{module_name}::#{association.to_s.camelize}",
76
+ association.to_s.camelize,
77
+ ].compact.collect(&:safe_constantize).compact.first
78
+ end
78
79
 
79
- klass = klass_name && klass_name.safe_constantize
80
+ define_method("#{association}") do
81
+ klass = expected_class || send("#{association}_type").to_s.safe_constantize
80
82
 
81
83
  if klass && klass.include?(Model)
82
84
  klass.find(send("#{association}_id"))
@@ -86,12 +88,8 @@ module StaticModels
86
88
  end
87
89
 
88
90
  define_method("#{association}=") do |value|
89
- unless opts[:polymorphic] || value.nil?
90
- expected = [opts[:class_name], association.to_s.camelize].compact
91
- got = value.class.name
92
- unless expected.include?(got)
93
- raise TypeError.new("Expected #{expected.first} got #{got}")
94
- end
91
+ if expected_class && !value.nil? && value.class != expected_class
92
+ raise TypeError.new("Expected #{expected_class} got #{value.class}")
95
93
  end
96
94
 
97
95
  if value.nil? || value.class.include?(Model)
@@ -31,4 +31,5 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency "rspec", "~> 3.0"
32
32
  spec.add_development_dependency "activerecord", '~> 4.2', '>= 4.2.0'
33
33
  spec.add_development_dependency "sqlite3", "~> 1.0", ">= 1.0.0"
34
+ spec.add_development_dependency "byebug", "~> 1.0", ">= 1.0.0"
34
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: static_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - nubis
@@ -112,6 +112,26 @@ dependencies:
112
112
  - - ">="
113
113
  - !ruby/object:Gem::Version
114
114
  version: 1.0.0
115
+ - !ruby/object:Gem::Dependency
116
+ name: byebug
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - "~>"
120
+ - !ruby/object:Gem::Version
121
+ version: '1.0'
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 1.0.0
125
+ type: :development
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.0'
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: 1.0.0
115
135
  description: "\n Replace your key/value classes with this.\n Define classes
116
136
  with several 'singleton' instances.\n "
117
137
  email: