has_metadata 1.6.0 → 1.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  h1. has_metadata -- Keep your tables narrow
2
2
 
3
3
  | *Author* | Tim Morgan |
4
- | *Version* | 1.6 (Jan 23, 2012) |
4
+ | *Version* | 1.6.1 (Jan 24, 2012) |
5
5
  | *License* | Released under the MIT License. |
6
6
 
7
7
  h2. Important note for those upgrading from 1.0 to 1.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "has_metadata"
8
- s.version = "1.6.0"
8
+ s.version = "1.6.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tim Morgan"]
12
- s.date = "2012-01-24"
12
+ s.date = "2012-01-25"
13
13
  s.description = "has_metadata lets you move non-indexed and weighty columns off of your big tables by creating a separate metadata table to store all this extra information. Works with Ruby 1.9. and Rails 3.0."
14
14
  s.email = "git@timothymorgan.info"
15
15
  s.extra_rdoc_files = [
@@ -73,6 +73,9 @@ module HasMetadata
73
73
 
74
74
  define_method(:save_metadata) { metadata.save! }
75
75
  define_method(:metadata_changed?) { metadata.try :changed? }
76
+
77
+ alias_method_chain :changed_attributes, :metadata
78
+ alias_method_chain :attribute_will_change!, :metadata
76
79
  else
77
80
  raise "Cannot redefine existing metadata fields: #{(fields.keys & self.metadata_fields.keys).to_sentence}" unless (fields.keys & self.metadata_fields.keys).empty?
78
81
  self.metadata_fields = self.metadata_fields.merge(fields)
@@ -142,15 +145,33 @@ module HasMetadata
142
145
  # saved one if none was found.
143
146
 
144
147
  def metadata!
145
- if instance_variables.include?(:@metadata) then
146
- metadata.set_fields self.class.metadata_fields
148
+ if instance_variables.include?(:@metadata) && metadata then
149
+ metadata
147
150
  else
148
- (metadata || Metadata.transaction { metadata || create_metadata }).set_fields self.class.metadata_fields
149
- end
151
+ if new_record? then
152
+ metadata || build_metadata
153
+ else
154
+ metadata || Metadata.transaction { metadata || create_metadata }
155
+ end
156
+ end.set_fields self.class.metadata_fields
150
157
  end
151
158
 
152
159
  # @private
153
160
  def inspect
154
161
  "#<#{self.class.to_s} #{attributes.merge(metadata.try(:data).try(:stringify_keys) || {}).map { |k,v| "#{k}: #{v.inspect}" }.join(', ')}>"
155
162
  end
163
+
164
+ # @private
165
+ def changed_attributes_with_metadata
166
+ changed_attributes_without_metadata.merge(metadata.try(:changed_metadata) || {})
167
+ end
168
+
169
+ # @private
170
+ def attribute_will_change_with_metadata!(attr)
171
+ if attribute_names.include?(attr) then
172
+ attribute_will_change_without_metadata! attr
173
+ else
174
+ metadata!.send :attribute_will_change!, attr
175
+ end
176
+ end
156
177
  end
@@ -56,12 +56,19 @@ module HasMetadata
56
56
 
57
57
  options = fields[name] || {}
58
58
  data_will_change!
59
+ attribute_will_change! name.to_s
59
60
  data[name] = HasMetadata.metadata_typecast(value, options[:type])
60
61
  end
61
62
 
62
63
  self
63
64
  end
64
65
 
66
+ # @return [Hash<String, Object>] A hash of metadata fields that have been
67
+ # altered.
68
+ def changed_metadata
69
+ changed_attributes.except(*attribute_names)
70
+ end
71
+
65
72
  private
66
73
 
67
74
  def initialize_data
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_metadata
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.6.1
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-24 00:00:00.000000000 Z
12
+ date: 2012-01-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70170864527740 !ruby/object:Gem::Requirement
16
+ requirement: &70331322795420 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70170864527740
24
+ version_requirements: *70331322795420
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: boolean
27
- requirement: &70170864527260 !ruby/object:Gem::Requirement
27
+ requirement: &70331322972780 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70170864527260
35
+ version_requirements: *70331322972780
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &70170864526780 !ruby/object:Gem::Requirement
38
+ requirement: &70331322983680 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70170864526780
46
+ version_requirements: *70331322983680
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: yard
49
- requirement: &70170864526300 !ruby/object:Gem::Requirement
49
+ requirement: &70331322981100 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70170864526300
57
+ version_requirements: *70331322981100
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: RedCloth
60
- requirement: &70170864525820 !ruby/object:Gem::Requirement
60
+ requirement: &70331322991560 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70170864525820
68
+ version_requirements: *70331322991560
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
- requirement: &70170864525340 !ruby/object:Gem::Requirement
71
+ requirement: &70331322997040 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70170864525340
79
+ version_requirements: *70331322997040
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &70170864524860 !ruby/object:Gem::Requirement
82
+ requirement: &70331323012700 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70170864524860
90
+ version_requirements: *70331323012700
91
91
  description: has_metadata lets you move non-indexed and weighty columns off of your
92
92
  big tables by creating a separate metadata table to store all this extra information.
93
93
  Works with Ruby 1.9. and Rails 3.0.