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.
- data/README.textile +1 -1
- data/has_metadata.gemspec +5 -5
- data/lib/has_metadata.rb +43 -69
- data/lib/has_metadata/model.rb +1 -8
- metadata +17 -17
data/README.textile
CHANGED
data/has_metadata.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "has_metadata"
|
8
|
-
s.version = "1.
|
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-
|
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>, ["
|
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>, ["
|
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>, ["
|
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"])
|
data/lib/has_metadata.rb
CHANGED
@@ -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
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
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
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
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
|
-
|
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
|
-
|
156
|
-
|
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
|
-
|
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
|
-
# @
|
170
|
-
|
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
|
-
|
175
|
-
|
176
|
-
|
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
|
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
|
data/lib/has_metadata/model.rb
CHANGED
@@ -5,7 +5,7 @@ module HasMetadata
|
|
5
5
|
# information.
|
6
6
|
|
7
7
|
class Model < ActiveRecord::Base
|
8
|
-
|
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.
|
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-
|
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: &
|
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: *
|
24
|
+
version_requirements: *70170864527740
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: boolean
|
27
|
-
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: *
|
35
|
+
version_requirements: *70170864527260
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: jeweler
|
38
|
-
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: *
|
46
|
+
version_requirements: *70170864526780
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: yard
|
49
|
-
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: *
|
57
|
+
version_requirements: *70170864526300
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: RedCloth
|
60
|
-
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: *
|
68
|
+
version_requirements: *70170864525820
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sqlite3
|
71
|
-
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: *
|
79
|
+
version_requirements: *70170864525340
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rspec
|
82
|
-
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: *
|
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.
|