has_metadata 1.5.4 → 1.6.0

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.5.4 (Jan 24, 2012) |
4
+ | *Version* | 1.6 (Jan 23, 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.5.4"
8
+ s.version = "1.6.0"
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-25"
12
+ s.date = "2012-01-24"
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 = [
@@ -36,7 +36,7 @@ Gem::Specification.new do |s|
36
36
  s.specification_version = 3
37
37
 
38
38
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
39
- s.add_runtime_dependency(%q<rails>, ["< 3.2"])
39
+ s.add_runtime_dependency(%q<rails>, [">= 3.2"])
40
40
  s.add_runtime_dependency(%q<boolean>, [">= 0"])
41
41
  s.add_development_dependency(%q<jeweler>, [">= 0"])
42
42
  s.add_development_dependency(%q<yard>, [">= 0"])
@@ -44,7 +44,7 @@ Gem::Specification.new do |s|
44
44
  s.add_development_dependency(%q<sqlite3>, [">= 0"])
45
45
  s.add_development_dependency(%q<rspec>, [">= 0"])
46
46
  else
47
- s.add_dependency(%q<rails>, ["< 3.2"])
47
+ s.add_dependency(%q<rails>, [">= 3.2"])
48
48
  s.add_dependency(%q<boolean>, [">= 0"])
49
49
  s.add_dependency(%q<jeweler>, [">= 0"])
50
50
  s.add_dependency(%q<yard>, [">= 0"])
@@ -53,7 +53,7 @@ Gem::Specification.new do |s|
53
53
  s.add_dependency(%q<rspec>, [">= 0"])
54
54
  end
55
55
  else
56
- s.add_dependency(%q<rails>, ["< 3.2"])
56
+ s.add_dependency(%q<rails>, [">= 3.2"])
57
57
  s.add_dependency(%q<boolean>, [">= 0"])
58
58
  s.add_dependency(%q<jeweler>, [">= 0"])
59
59
  s.add_dependency(%q<yard>, [">= 0"])
@@ -73,9 +73,6 @@ 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
79
76
  else
80
77
  raise "Cannot redefine existing metadata fields: #{(fields.keys & self.metadata_fields.keys).to_sentence}" unless (fields.keys & self.metadata_fields.keys).empty?
81
78
  self.metadata_fields = self.metadata_fields.merge(fields)
@@ -102,81 +99,58 @@ module HasMetadata
102
99
  end
103
100
  end
104
101
 
105
- # Instance methods that are added to your model.
106
-
107
- module InstanceMethods
108
-
109
- def as_json(options={})
110
- options ||= Hash.new # the JSON encoder can sometimes give us nil options?
111
- options[:except] = Array.wrap(options[:except]) + [ :metadata_id ]
112
- options[:methods] = Array.wrap(options[:methods]) + metadata_fields.keys - options[:except].map(&:to_sym)
113
- super options
114
- end
115
-
116
- def to_xml(options={})
117
- options[:except] = Array.wrap(options[:except]) + [ :metadata_id ]
118
- options[:methods] = Array.wrap(options[:methods]) + metadata_fields.keys - options[:except].map(&:to_sym)
119
- super options
120
- end
102
+ def as_json(options={})
103
+ options ||= Hash.new # the JSON encoder can sometimes give us nil options?
104
+ options[:except] = Array.wrap(options[:except]) + [ :metadata_id ]
105
+ options[:methods] = Array.wrap(options[:methods]) + metadata_fields.keys - options[:except].map(&:to_sym)
106
+ super options
107
+ end
108
+
109
+ def to_xml(options={})
110
+ options[:except] = Array.wrap(options[:except]) + [ :metadata_id ]
111
+ options[:methods] = Array.wrap(options[:methods]) + metadata_fields.keys - options[:except].map(&:to_sym)
112
+ super options
113
+ end
121
114
 
122
- # @private
123
- def assign_multiparameter_attributes(pairs)
124
- fake_attributes = pairs.select { |(field, _)| self.class.metadata_fields.include? field[0, field.index('(')].to_sym }
125
-
126
- fake_attributes.group_by { |(field, _)| field[0, field.index('(')] }.each do |field_name, parts|
127
- options = self.class.metadata_fields[field_name.to_sym]
128
- if options[:type] then
129
- args = parts.each_with_object([]) do |(part_name, value), ary|
130
- part_ann = part_name[part_name.index('(') + 1, part_name.length]
131
- index = part_ann.to_i - 1
132
- raise "Out-of-bounds multiparameter argument index" unless index >= 0
133
- ary[index] = if value.blank? then nil
134
- elsif part_ann.ends_with?('i)') then value.to_i
135
- elsif part_ann.ends_with?('f)') then value.to_f
136
- else value end
137
- end
138
- args.compact!
139
- send :"#{field_name}=", options[:type].new(*args) unless args.empty?
140
- else
141
- raise "#{field_name} has no type and cannot be used for multiparameter assignment"
115
+ # @private
116
+ def assign_multiparameter_attributes(pairs)
117
+ fake_attributes = pairs.select { |(field, _)| self.class.metadata_fields.include? field[0, field.index('(')].to_sym }
118
+
119
+ fake_attributes.group_by { |(field, _)| field[0, field.index('(')] }.each do |field_name, parts|
120
+ options = self.class.metadata_fields[field_name.to_sym]
121
+ if options[:type] then
122
+ args = parts.each_with_object([]) do |(part_name, value), ary|
123
+ part_ann = part_name[part_name.index('(') + 1, part_name.length]
124
+ index = part_ann.to_i - 1
125
+ raise "Out-of-bounds multiparameter argument index" unless index >= 0
126
+ ary[index] = if value.blank? then nil
127
+ elsif part_ann.ends_with?('i)') then value.to_i
128
+ elsif part_ann.ends_with?('f)') then value.to_f
129
+ else value end
142
130
  end
143
- end
144
-
145
- super(pairs - fake_attributes)
146
- end
147
-
148
- # @return [Metadata] An existing associated {Metadata} instance, or new,
149
- # saved one if none was found.
150
-
151
- def metadata!
152
- if instance_variables.include?(:@metadata) && metadata then
153
- metadata
131
+ args.compact!
132
+ send :"#{field_name}=", options[:type].new(*args) unless args.empty?
154
133
  else
155
- if new_record? then
156
- metadata || build_metadata
157
- else
158
- metadata || Metadata.transaction { metadata || create_metadata }
159
- end
160
- end.set_fields self.class.metadata_fields
134
+ raise "#{field_name} has no type and cannot be used for multiparameter assignment"
135
+ end
161
136
  end
162
137
 
163
- # @private
164
- def inspect
165
- "#<#{self.class.to_s} #{attributes.merge(metadata.try(:data).try(:stringify_keys) || {}).map { |k,v| "#{k}: #{v.inspect}" }.join(', ')}>"
166
- end
138
+ super(pairs - fake_attributes)
167
139
  end
168
140
 
169
- # @private
170
- def changed_attributes_with_metadata
171
- changed_attributes_without_metadata.merge(metadata.try(:changed_metadata) || {})
172
- end
141
+ # @return [Metadata] An existing associated {Metadata} instance, or new,
142
+ # saved one if none was found.
173
143
 
174
- # @private
175
- def attribute_will_change_with_metadata!(attr)
176
- if attribute_names.include?(attr) then
177
- attribute_will_change_without_metadata! attr
144
+ def metadata!
145
+ if instance_variables.include?(:@metadata) then
146
+ metadata.set_fields self.class.metadata_fields
178
147
  else
179
- metadata!.send :attribute_will_change!, attr
148
+ (metadata || Metadata.transaction { metadata || create_metadata }).set_fields self.class.metadata_fields
180
149
  end
181
150
  end
151
+
152
+ # @private
153
+ def inspect
154
+ "#<#{self.class.to_s} #{attributes.merge(metadata.try(:data).try(:stringify_keys) || {}).map { |k,v| "#{k}: #{v.inspect}" }.join(', ')}>"
155
+ end
182
156
  end
@@ -5,7 +5,7 @@ module HasMetadata
5
5
  # information.
6
6
 
7
7
  class Model < ActiveRecord::Base
8
- set_table_name 'metadata'
8
+ self.table_name = 'metadata'
9
9
  serialize :data, Hash
10
10
 
11
11
  after_initialize :initialize_data
@@ -56,19 +56,12 @@ module HasMetadata
56
56
 
57
57
  options = fields[name] || {}
58
58
  data_will_change!
59
- attribute_will_change! name.to_s
60
59
  data[name] = HasMetadata.metadata_typecast(value, options[:type])
61
60
  end
62
61
 
63
62
  self
64
63
  end
65
64
 
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
-
72
65
  private
73
66
 
74
67
  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.5.4
4
+ version: 1.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-25 00:00:00.000000000 Z
12
+ date: 2012-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70232408519820 !ruby/object:Gem::Requirement
16
+ requirement: &70170864527740 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - <
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
21
  version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70232408519820
24
+ version_requirements: *70170864527740
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: boolean
27
- requirement: &70232408526660 !ruby/object:Gem::Requirement
27
+ requirement: &70170864527260 !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: *70232408526660
35
+ version_requirements: *70170864527260
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &70232412633380 !ruby/object:Gem::Requirement
38
+ requirement: &70170864526780 !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: *70232412633380
46
+ version_requirements: *70170864526780
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: yard
49
- requirement: &70232412641340 !ruby/object:Gem::Requirement
49
+ requirement: &70170864526300 !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: *70232412641340
57
+ version_requirements: *70170864526300
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: RedCloth
60
- requirement: &70232412651340 !ruby/object:Gem::Requirement
60
+ requirement: &70170864525820 !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: *70232412651340
68
+ version_requirements: *70170864525820
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
- requirement: &70232412659240 !ruby/object:Gem::Requirement
71
+ requirement: &70170864525340 !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: *70232412659240
79
+ version_requirements: *70170864525340
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &70232412546900 !ruby/object:Gem::Requirement
82
+ requirement: &70170864524860 !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: *70232412546900
90
+ version_requirements: *70170864524860
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.