property_sets 3.6.0 → 3.7.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
  SHA256:
3
- metadata.gz: 83bde37b6c1a385dc5372f75424eda44a32211093219e2aa793dc6a42e8a40b6
4
- data.tar.gz: 33689e9a2946082c6acf3681de60101cd0c1c8e728500fa891adfa91347c6257
3
+ metadata.gz: d4202c89da527945bdf58692cdd2c99c98090b130b674605ad4d97d6938b48f7
4
+ data.tar.gz: c1ce9e1690953791c8325405ce6fe398b251acc2095868e238146d5e560fc173
5
5
  SHA512:
6
- metadata.gz: d5937f372c44d22aee6647680c98be68a1506720e7115b49be0b29e8bdaf85f3f0e80afa2ee3d246875fe2cf076283255101a5d3bc9e34f6aed8643259f9a725
7
- data.tar.gz: a477ec210da5626275ca6eca932f56d382f68c3f3aca1a74082522cd6614afb48b113cf822948a11c52008f643edca24e71f01c6f34fdc8f5682ed718a8e79de
6
+ metadata.gz: 6f62d70197d3fcb43434d5504187deb5667275f9a0a34d7884f2b57e23ab0e84122ed4f56c68f48a55ac74cd97ef9a7d59a042e9e1fa3e005592f09abe5c9bee
7
+ data.tar.gz: 3225b1bf41f59c63b43dd5ebce79f048b6933fc47345d2089a03bbcbf613b5e1c0877dbc508647a187f2ca999d88375263ec5e3bd9a660b469daf0fad669a027
data/lib/property_sets.rb CHANGED
@@ -10,16 +10,19 @@ end
10
10
  module PropertySets
11
11
  def self.ensure_property_set_class(association, owner_class_name)
12
12
  const_name = "#{owner_class_name}#{association.to_s.singularize.camelcase}".to_sym
13
+
13
14
  unless Object.const_defined?(const_name)
14
- property_class = Object.const_set(const_name, Class.new(ActiveRecord::Base))
15
- property_class.class_eval do
15
+ property_class = Class.new(ActiveRecord::Base) do
16
16
  include PropertySets::PropertySetModel::InstanceMethods
17
17
  extend PropertySets::PropertySetModel::ClassMethods
18
18
  end
19
19
 
20
+ Object.const_set(const_name, property_class)
21
+
20
22
  property_class.owner_class = owner_class_name
21
23
  property_class.owner_assoc = association
22
24
  end
25
+
23
26
  Object.const_get(const_name)
24
27
  end
25
28
  end
@@ -6,6 +6,8 @@ module PropertySets
6
6
  module ActiveRecordExtension
7
7
  module ClassMethods
8
8
 
9
+ RAILS6 = ActiveRecord::VERSION::MAJOR >= 6
10
+
9
11
  def property_set(association, options = {}, &block)
10
12
  unless include?(PropertySets::ActiveRecordExtension::InstanceMethods)
11
13
  self.send(:prepend, PropertySets::ActiveRecordExtension::InstanceMethods)
@@ -14,52 +16,84 @@ module PropertySets
14
16
  end
15
17
 
16
18
  raise "Invalid association name, letters only" unless association.to_s =~ /[a-z]+/
19
+ exists = property_set_index.include?(association)
20
+
17
21
  self.property_set_index << association
18
22
 
23
+ # eg AccountSetting - this IS idempotent
19
24
  property_class = PropertySets.ensure_property_set_class(
20
25
  association,
21
26
  options.delete(:owner_class_name) || self.name
22
27
  )
23
- property_class.instance_eval(&block)
24
28
 
25
- hash_opts = {:class_name => property_class.name, :autosave => true, :dependent => :destroy}.merge(options)
29
+ # eg property :is_awesome
30
+ property_class.instance_eval(&block) if block
31
+
32
+ tb_name = options.delete :table_name
33
+ property_class.table_name = tb_name if tb_name
34
+
35
+ hash_opts = {
36
+ :class_name => property_class.name,
37
+ :autosave => true,
38
+ :dependent => :destroy,
39
+ :inverse_of => self.name.underscore.to_sym,
40
+ }.merge(options)
41
+
42
+ # TODO: should check options are compatible? warn? raise?
43
+ reflection = self.reflections[association.to_s] # => ActiveRecord::Reflection::HasManyReflection
44
+ reflection.options.merge! options if reflection && !options.empty?
26
45
 
27
- silence_warnings do
46
+ unless exists then # makes has_many idempotent...
28
47
  has_many association, hash_opts do
29
- include PropertySets::ActiveRecordExtension::AssociationExtensions
30
-
31
- property_class.keys.each do |key|
32
- raise "Invalid property key #{key}" if self.respond_to?(key)
33
-
34
- # Reports the coerced truth value of the property
35
- define_method "#{key}?" do
36
- type = property_class.type(key)
37
- value = lookup_value(type, key)
38
- ![ "false", "0", "", "off", "n" ].member?(value.to_s.downcase)
39
- end
40
-
41
- # Returns the value of the property
42
- define_method "#{key}" do
43
- type = property_class.type(key)
44
- lookup_value(type, key)
45
- end
46
-
47
- # Assigns a new value to the property
48
- define_method "#{key}=" do |value|
49
- instance = lookup(key)
50
- instance.value = PropertySets::Casting.write(property_class.type(key), value)
51
- instance.value
52
- end
53
-
54
- define_method "#{key}_record" do
55
- lookup(key)
56
- end
48
+ # keep this damn block! -- creates association_module below
49
+ end
50
+ end
51
+
52
+ # eg 5: AccountSettingsAssociationExtension
53
+ # eg 6: Account::SettingsAssociationExtension
54
+
55
+ # stolen/adapted from AR's collection_association.rb #define_extensions
56
+
57
+ module_name = "#{association.to_s.camelize}AssociationExtension"
58
+ module_name = name.demodulize + module_name unless RAILS6
59
+
60
+ target = RAILS6 ? self : self.parent
61
+ association_module = target.const_get module_name
62
+
63
+ association_module.module_eval do
64
+ include PropertySets::ActiveRecordExtension::AssociationExtensions
65
+
66
+ property_class.keys.each do |key|
67
+ raise "Invalid property key #{key}" if self.respond_to?(key)
68
+
69
+ # Reports the coerced truth value of the property
70
+ define_method "#{key}?" do
71
+ type = property_class.type(key)
72
+ value = lookup_value(type, key)
73
+ ![ "false", "0", "", "off", "n" ].member?(value.to_s.downcase)
74
+ end
75
+
76
+ # Returns the value of the property
77
+ define_method "#{key}" do
78
+ type = property_class.type(key)
79
+ lookup_value(type, key)
80
+ end
81
+
82
+ # Assigns a new value to the property
83
+ define_method "#{key}=" do |value|
84
+ instance = lookup(key)
85
+ instance.value = PropertySets::Casting.write(property_class.type(key), value)
86
+ instance.value
57
87
  end
58
88
 
59
- define_method :property_serialized? do |key|
60
- property_class.type(key) == :serialized
89
+ define_method "#{key}_record" do
90
+ lookup(key)
61
91
  end
62
92
  end
93
+
94
+ define_method :property_serialized? do |key|
95
+ property_class.type(key) == :serialized
96
+ end
63
97
  end
64
98
  end
65
99
  end
@@ -108,13 +108,16 @@ module PropertySets
108
108
  base.attr_accessible :name, :value if defined?(ProtectedAttributes)
109
109
  end
110
110
 
111
- def property(key, options = nil)
111
+ def properties
112
112
  @properties ||= HashWithIndifferentAccess.new
113
- @properties[key] = options
113
+ end
114
+
115
+ def property(key, options = nil)
116
+ properties[key] = options
114
117
  end
115
118
 
116
119
  def keys
117
- @properties.keys
120
+ properties.keys
118
121
  end
119
122
 
120
123
  def default(key)
@@ -122,15 +125,15 @@ module PropertySets
122
125
  end
123
126
 
124
127
  def raw_default(key)
125
- @properties[key].try(:[], :default)
128
+ properties[key].try(:[], :default)
126
129
  end
127
130
 
128
131
  def type(key)
129
- @properties[key].try(:[], :type) || :string
132
+ properties[key].try(:[], :type) || :string
130
133
  end
131
134
 
132
135
  def protected?(key)
133
- @properties[key].try(:[], :protected) || false
136
+ properties[key].try(:[], :protected) || false
134
137
  end
135
138
 
136
139
  def owner_class=(owner_class_name)
@@ -1,3 +1,3 @@
1
1
  module PropertySets
2
- VERSION = "3.6.0"
2
+ VERSION = "3.7.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: property_sets
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.0
4
+ version: 3.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Morten Primdahl
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-14 00:00:00.000000000 Z
11
+ date: 2021-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -132,7 +132,7 @@ homepage: http://github.com/zendesk/property_sets
132
132
  licenses:
133
133
  - MIT
134
134
  metadata: {}
135
- post_install_message:
135
+ post_install_message:
136
136
  rdoc_options: []
137
137
  require_paths:
138
138
  - lib
@@ -147,8 +147,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
147
  - !ruby/object:Gem::Version
148
148
  version: '0'
149
149
  requirements: []
150
- rubygems_version: 3.1.4
151
- signing_key:
150
+ rubygems_version: 3.0.3
151
+ signing_key:
152
152
  specification_version: 4
153
153
  summary: Property sets for ActiveRecord.
154
154
  test_files: []