static_models 0.1.0 → 0.2.0

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: 1ea954799a4013aebd0fd97a706fb65cdd3a04f5
4
- data.tar.gz: 991966ce298336a266fa9b8859d41fa4f2fd2aca
3
+ metadata.gz: ea4da2c5cca0318b880691f4a2bc58709e30ddd9
4
+ data.tar.gz: 2aa14a89d48db484bb8a55b72cbb2816760f1da4
5
5
  SHA512:
6
- metadata.gz: 968c9408edeed32e1f509eb0b117193f9f3235c52f7785c9889d7562f6d0ae14ca520d24df0a181f10d7cc78e948b951da503658defdbfbe86ca8d9894d448a0
7
- data.tar.gz: 482f3bfedef5c81ac5da2a573cadbc3c16f5b55d2361fe88a660f12d08125ef73dd6fc892376a92a82226b8905d8e432adcb3305db77a52f2dc788c764530d4e
6
+ metadata.gz: 4c3e819e42b7fab9be0573ccd8780aba93e58b82963732f63145d3cb62d12e9aeeff7449d13b946f85d07027b8cc338f8595fa193fe3245de1ba65a3ac24f10c
7
+ data.tar.gz: 8e35bebe78a36757e41318a601f788491e749d7b3d8f2c9c70e5bf613e202edba1de1062fcdf3362c18deaedf56fc71dac6c4d788d70e089a5e82f602af0c7a9
data/README.md CHANGED
@@ -75,7 +75,7 @@ Or install it yourself as:
75
75
  attr_accessor :breed_id
76
76
 
77
77
  include StaticModels::BelongsTo
78
- belongs_to_static_model :breed
78
+ belongs_to :breed
79
79
  end
80
80
 
81
81
  Dog.new.tap do |d|
@@ -89,14 +89,38 @@ Or install it yourself as:
89
89
  d.breed.should == Breed.doberman
90
90
  end
91
91
 
92
- # Set your model manually if it can't be inferred from the attribute name
93
-
94
- class WeirdDoggie
95
- attr_accessor :dog_kind_id
92
+ # StaticModels::BelongsTo plays nice with ActiveRecords belongs_to.
93
+ # You can use it in your models transparently, it will know
94
+ # when to use a StaticModel or call out to ActiveRecord's code.
95
+ # You can even set up polymorphic associations that point to either
96
+ # a StaticModel or an ActiveRecord model.
96
97
 
98
+ class StoreDog < ActiveRecord::Base
97
99
  include StaticModels::BelongsTo
98
- belongs_to_static_model :dog_kind, Breed
100
+ belongs_to :breed
101
+ belongs_to :classification, class_name: 'Breed'
102
+ belongs_to :anything, polymorphic: true
103
+ belongs_to :store_dog
104
+ belongs_to :another_dog, class_name: 'StoreDog'
105
+ belongs_to :anydog, polymorphic: true
99
106
  end
107
+
108
+ dog = StoreDog.new
109
+ dog.breed = Breed.corgi
110
+ dog.classification = Breed.collie
111
+ dog.anything = Breed.doberman
112
+ dog.store_dog = dog
113
+ dog.another_dog = dog
114
+ dog.anydog = dog
115
+ dog.save!
116
+ dog.reload
117
+ dog.breed == Breed.corgi
118
+ dog.classification == Breed.collie
119
+ dog.anything == Breed.doberman
120
+ dog.store_dog == dog
121
+ dog.another_dog == dog
122
+ dog.anydog == dog
123
+
100
124
  ```
101
125
 
102
126
  ## Development
@@ -1,3 +1,3 @@
1
1
  module StaticModels
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/static_models.rb CHANGED
@@ -17,17 +17,9 @@ module StaticModels
17
17
  end
18
18
  end
19
19
 
20
- def to_s
21
- code.to_s
22
- end
23
-
24
- def to_i
25
- id
26
- end
27
-
28
- def name
29
- code
30
- end
20
+ def to_s; code.to_s; end
21
+ def to_i; id; end
22
+ def name; code; end
31
23
  end
32
24
 
33
25
  class_methods do
@@ -74,18 +66,49 @@ module StaticModels
74
66
  extend ActiveSupport::Concern
75
67
 
76
68
  class_methods do
77
- def belongs_to_static_model(attr_name, cls=nil)
78
- cls ||= attr_name.to_s.humanize.constantize
69
+ def belongs_to(association, opts = {})
70
+ super(association, opts) if defined?(super)
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
79
78
 
80
- define_method(attr_name) do
81
- cls.find(send("#{attr_name}_id"))
79
+ return nil unless Object.const_defined?(klass_name)
80
+
81
+ klass = klass_name.constantize
82
+
83
+ if klass.include?(Model)
84
+ klass.find(send("#{association}_id"))
85
+ else
86
+ super()
87
+ end
82
88
  end
83
89
 
84
- define_method("#{attr_name}=") do |value|
85
- unless value.nil? || value.is_a?(cls)
86
- raise TypeError.new("Expected #{cls} got #{value.class}")
90
+ define_method("#{association}=") do |value|
91
+ unless opts[:polymorphic]
92
+ expected = [opts[:class_name], association.to_s.camelize].compact
93
+ got = value.class.name
94
+ unless expected.include?(got)
95
+ raise TypeError.new("Expected #{expected.first} got #{got}")
96
+ end
97
+ end
98
+
99
+ if value.class.include?(Model)
100
+ if opts[:polymorphic]
101
+ # This next line resets the old polymorphic association
102
+ # if it was set to an ActiveRecord::Model. Otherwise
103
+ # ActiveRecord will get confused and ask for our StaticModel's
104
+ # table and a bunch of other things that don't apply.
105
+ super(nil) if defined?(super)
106
+ send("#{association}_type=", value.class.name )
107
+ end
108
+ send("#{association}_id=", value && value.id)
109
+ else
110
+ super(value)
87
111
  end
88
- send("#{attr_name}_id=", value && value.id)
89
112
  end
90
113
  end
91
114
  end
@@ -29,4 +29,6 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency "bundler", "~> 1.13"
30
30
  spec.add_development_dependency "rake", "~> 10.0"
31
31
  spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency "activerecord", '~> 4.2', '>= 4.2.0'
33
+ spec.add_development_dependency "sqlite3", "~> 0"
32
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: static_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nubis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-23 00:00:00.000000000 Z
11
+ date: 2017-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -72,6 +72,40 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: activerecord
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '4.2'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 4.2.0
85
+ type: :development
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '4.2'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 4.2.0
95
+ - !ruby/object:Gem::Dependency
96
+ name: sqlite3
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
75
109
  description: "\n Replace your key/value classes with this.\n Define classes
76
110
  with several 'singleton' instances.\n "
77
111
  email: