has_metadata 1.2.4 → 1.3.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 +13 -13
- data/lib/has_metadata/model.rb +3 -7
- data/lib/has_metadata.rb +14 -55
- data/templates/create_metadata.rb +1 -1
- metadata +18 -18
data/README.textile
CHANGED
data/has_metadata.gemspec
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "1.
|
7
|
+
s.name = %q{has_metadata}
|
8
|
+
s.version = "1.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = [
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
11
|
+
s.authors = [%q{Tim Morgan}]
|
12
|
+
s.date = %q{2011-08-31}
|
13
|
+
s.description = %q{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
|
+
s.email = %q{git@timothymorgan.info}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
17
|
"README.textile"
|
@@ -26,17 +26,17 @@ Gem::Specification.new do |s|
|
|
26
26
|
"templates/create_metadata.rb",
|
27
27
|
"templates/metadata.rb"
|
28
28
|
]
|
29
|
-
s.homepage =
|
30
|
-
s.require_paths = [
|
29
|
+
s.homepage = %q{http://github.com/riscfuture/has_metadata}
|
30
|
+
s.require_paths = [%q{lib}]
|
31
31
|
s.required_ruby_version = Gem::Requirement.new(">= 1.9")
|
32
|
-
s.rubygems_version =
|
33
|
-
s.summary =
|
32
|
+
s.rubygems_version = %q{1.8.9}
|
33
|
+
s.summary = %q{Reduce your table width by moving non-indexed columns to a separate metadata table}
|
34
34
|
|
35
35
|
if s.respond_to? :specification_version then
|
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.1"])
|
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.1"])
|
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.1"])
|
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/model.rb
CHANGED
@@ -11,6 +11,9 @@ module HasMetadata
|
|
11
11
|
after_initialize :initialize_data
|
12
12
|
before_save :nullify_empty_fields
|
13
13
|
|
14
|
+
validates :data,
|
15
|
+
presence: true
|
16
|
+
|
14
17
|
# @private
|
15
18
|
def set_fields(fields)
|
16
19
|
return self if @_fields_set
|
@@ -56,19 +59,12 @@ module HasMetadata
|
|
56
59
|
|
57
60
|
options = fields[name] || {}
|
58
61
|
data_will_change!
|
59
|
-
attribute_will_change! name.to_s
|
60
62
|
data[name] = HasMetadata.metadata_typecast(value, options[:type])
|
61
63
|
end
|
62
64
|
|
63
65
|
self
|
64
66
|
end
|
65
67
|
|
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
68
|
private
|
73
69
|
|
74
70
|
def initialize_data
|
data/lib/has_metadata.rb
CHANGED
@@ -26,7 +26,7 @@ module HasMetadata
|
|
26
26
|
if value.kind_of?(String) then
|
27
27
|
if type == Integer or type == Fixnum then
|
28
28
|
begin
|
29
|
-
return Integer(value
|
29
|
+
return Integer(value)
|
30
30
|
rescue ArgumentError
|
31
31
|
return value
|
32
32
|
end
|
@@ -63,23 +63,14 @@ module HasMetadata
|
|
63
63
|
# has_metadata(optional: true, required: { presence: true }, number: { type: Fixnum })
|
64
64
|
|
65
65
|
def has_metadata(fields)
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
66
|
+
belongs_to :metadata, dependent: :destroy
|
67
|
+
accepts_nested_attributes_for :metadata
|
68
|
+
after_save :save_metadata, if: :metadata_changed?
|
69
|
+
class_attribute :metadata_fields
|
70
|
+
self.metadata_fields = fields.deep_clone
|
70
71
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
define_method(:save_metadata) { metadata.save! }
|
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
|
-
else
|
80
|
-
raise "Cannot redefine existing metadata fields: #{(fields.keys & self.metadata_fields.keys).to_sentence}" unless (fields.keys & self.metadata_fields.keys).empty?
|
81
|
-
self.metadata_fields = self.metadata_fields.merge(fields)
|
82
|
-
end
|
72
|
+
define_method(:save_metadata) { metadata.save! }
|
73
|
+
define_method(:metadata_changed?) { metadata.try :changed? }
|
83
74
|
|
84
75
|
fields.each do |name, options|
|
85
76
|
# delegate all attribute methods to the metadata
|
@@ -87,7 +78,6 @@ module HasMetadata
|
|
87
78
|
|
88
79
|
if options.kind_of?(Hash) then
|
89
80
|
type = options.delete(:type)
|
90
|
-
type_validate = !options.delete(:skip_type_validation)
|
91
81
|
options.delete :default
|
92
82
|
|
93
83
|
validate do |obj|
|
@@ -95,7 +85,7 @@ module HasMetadata
|
|
95
85
|
errors.add(name, :incorrect_type) unless
|
96
86
|
HasMetadata.metadata_typecast(value, type).kind_of?(type) or
|
97
87
|
((options[:allow_nil] and value.nil?) or (options[:allow_blank] and value.blank?))
|
98
|
-
end if type
|
88
|
+
end if type
|
99
89
|
validates(name, options) unless options.empty? or (options.keys - [ :allow_nil, :allow_blank ]).empty?
|
100
90
|
end
|
101
91
|
end
|
@@ -106,19 +96,6 @@ module HasMetadata
|
|
106
96
|
|
107
97
|
module InstanceMethods
|
108
98
|
|
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
|
121
|
-
|
122
99
|
# @private
|
123
100
|
def assign_multiparameter_attributes(pairs)
|
124
101
|
fake_attributes = pairs.select { |(field, _)| self.class.metadata_fields.include? field[0, field.index('(')].to_sym }
|
@@ -147,17 +124,13 @@ module HasMetadata
|
|
147
124
|
|
148
125
|
# @return [Metadata] An existing associated {Metadata} instance, or new,
|
149
126
|
# saved one if none was found.
|
150
|
-
|
127
|
+
|
151
128
|
def metadata!
|
152
|
-
if instance_variables.include?(:@metadata)
|
153
|
-
metadata
|
129
|
+
if instance_variables.include?(:@metadata) then
|
130
|
+
metadata.set_fields self.class.metadata_fields
|
154
131
|
else
|
155
|
-
|
156
|
-
|
157
|
-
else
|
158
|
-
metadata || Metadata.transaction { metadata || create_metadata }
|
159
|
-
end
|
160
|
-
end.set_fields self.class.metadata_fields
|
132
|
+
(metadata || Metadata.transaction { metadata || create_metadata }).set_fields self.class.metadata_fields
|
133
|
+
end
|
161
134
|
end
|
162
135
|
|
163
136
|
# @private
|
@@ -165,18 +138,4 @@ module HasMetadata
|
|
165
138
|
"#<#{self.class.to_s} #{attributes.merge(metadata.try(:data).try(:stringify_keys) || {}).map { |k,v| "#{k}: #{v.inspect}" }.join(', ')}>"
|
166
139
|
end
|
167
140
|
end
|
168
|
-
|
169
|
-
# @private
|
170
|
-
def changed_attributes_with_metadata
|
171
|
-
changed_attributes_without_metadata.merge(metadata.try(:changed_metadata) || {})
|
172
|
-
end
|
173
|
-
|
174
|
-
# @private
|
175
|
-
def attribute_will_change_with_metadata!(attr)
|
176
|
-
if attribute_names.include?(attr) then
|
177
|
-
attribute_will_change_without_metadata! attr
|
178
|
-
else
|
179
|
-
metadata!.send :attribute_will_change!, attr
|
180
|
-
end
|
181
|
-
end
|
182
141
|
end
|
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.3.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:
|
12
|
+
date: 2011-08-31 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &2173498820 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: '3.1'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2173498820
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: boolean
|
27
|
-
requirement: &
|
27
|
+
requirement: &2173498260 !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: *2173498260
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: jeweler
|
38
|
-
requirement: &
|
38
|
+
requirement: &2173358180 !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: *2173358180
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: yard
|
49
|
-
requirement: &
|
49
|
+
requirement: &2173357580 !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: *2173357580
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: RedCloth
|
60
|
-
requirement: &
|
60
|
+
requirement: &2173356920 !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: *2173356920
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sqlite3
|
71
|
-
requirement: &
|
71
|
+
requirement: &2173356440 !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: *2173356440
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rspec
|
82
|
-
requirement: &
|
82
|
+
requirement: &2173355920 !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: *2173355920
|
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.
|
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
126
126
|
version: '0'
|
127
127
|
requirements: []
|
128
128
|
rubyforge_project:
|
129
|
-
rubygems_version: 1.8.
|
129
|
+
rubygems_version: 1.8.9
|
130
130
|
signing_key:
|
131
131
|
specification_version: 3
|
132
132
|
summary: Reduce your table width by moving non-indexed columns to a separate metadata
|