has_metadata 1.2.2 → 1.2.3
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 -22
- data/lib/has_metadata/model.rb +0 -3
- data/lib/has_metadata.rb +37 -13
- data/templates/create_metadata.rb +1 -1
- metadata +19 -28
- data/.document +0 -5
- data/.rspec +0 -1
- data/Gemfile +0 -12
- data/Gemfile.lock +0 -98
- data/Rakefile +0 -38
- data/VERSION +0 -1
- data/spec/has_metadata_spec.rb +0 -224
- data/spec/metadata_spec.rb +0 -21
- data/spec/spec_helper.rb +0 -24
data/README.textile
CHANGED
data/has_metadata.gemspec
CHANGED
@@ -4,48 +4,39 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "1.2.
|
7
|
+
s.name = "has_metadata"
|
8
|
+
s.version = "1.2.3"
|
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 = ["Tim Morgan"]
|
12
|
+
s.date = "2012-01-24"
|
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
|
+
s.email = "git@timothymorgan.info"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
17
|
"README.textile"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
|
-
".document",
|
21
|
-
".rspec",
|
22
|
-
"Gemfile",
|
23
|
-
"Gemfile.lock",
|
24
20
|
"LICENSE",
|
25
21
|
"README.textile",
|
26
|
-
"Rakefile",
|
27
|
-
"VERSION",
|
28
22
|
"has_metadata.gemspec",
|
29
23
|
"lib/has_metadata.rb",
|
30
24
|
"lib/has_metadata/metadata_generator.rb",
|
31
25
|
"lib/has_metadata/model.rb",
|
32
|
-
"spec/has_metadata_spec.rb",
|
33
|
-
"spec/metadata_spec.rb",
|
34
|
-
"spec/spec_helper.rb",
|
35
26
|
"templates/create_metadata.rb",
|
36
27
|
"templates/metadata.rb"
|
37
28
|
]
|
38
|
-
s.homepage =
|
39
|
-
s.require_paths = [
|
29
|
+
s.homepage = "http://github.com/riscfuture/has_metadata"
|
30
|
+
s.require_paths = ["lib"]
|
40
31
|
s.required_ruby_version = Gem::Requirement.new(">= 1.9")
|
41
|
-
s.rubygems_version =
|
42
|
-
s.summary =
|
32
|
+
s.rubygems_version = "1.8.15"
|
33
|
+
s.summary = "Reduce your table width by moving non-indexed columns to a separate metadata table"
|
43
34
|
|
44
35
|
if s.respond_to? :specification_version then
|
45
36
|
s.specification_version = 3
|
46
37
|
|
47
38
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
-
s.add_runtime_dependency(%q<rails>, ["
|
39
|
+
s.add_runtime_dependency(%q<rails>, ["< 3.1"])
|
49
40
|
s.add_runtime_dependency(%q<boolean>, [">= 0"])
|
50
41
|
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
51
42
|
s.add_development_dependency(%q<yard>, [">= 0"])
|
@@ -53,7 +44,7 @@ Gem::Specification.new do |s|
|
|
53
44
|
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
54
45
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
55
46
|
else
|
56
|
-
s.add_dependency(%q<rails>, ["
|
47
|
+
s.add_dependency(%q<rails>, ["< 3.1"])
|
57
48
|
s.add_dependency(%q<boolean>, [">= 0"])
|
58
49
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
59
50
|
s.add_dependency(%q<yard>, [">= 0"])
|
@@ -62,7 +53,7 @@ Gem::Specification.new do |s|
|
|
62
53
|
s.add_dependency(%q<rspec>, [">= 0"])
|
63
54
|
end
|
64
55
|
else
|
65
|
-
s.add_dependency(%q<rails>, ["
|
56
|
+
s.add_dependency(%q<rails>, ["< 3.1"])
|
66
57
|
s.add_dependency(%q<boolean>, [">= 0"])
|
67
58
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
68
59
|
s.add_dependency(%q<yard>, [">= 0"])
|
data/lib/has_metadata/model.rb
CHANGED
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.sub(/^0+/, '')) # so that it doesn't think it's in octal
|
30
30
|
rescue ArgumentError
|
31
31
|
return value
|
32
32
|
end
|
@@ -63,14 +63,20 @@ 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
|
-
|
70
|
-
self.metadata_fields = fields.deep_clone
|
66
|
+
if !respond_to?(:metadata_fields) then
|
67
|
+
belongs_to :metadata, dependent: :destroy
|
68
|
+
accepts_nested_attributes_for :metadata
|
69
|
+
after_save :save_metadata, if: :metadata_changed?
|
71
70
|
|
72
|
-
|
73
|
-
|
71
|
+
class_inheritable_hash :metadata_fields
|
72
|
+
self.metadata_fields = fields.deep_clone
|
73
|
+
|
74
|
+
define_method(:save_metadata) { metadata.save! }
|
75
|
+
define_method(:metadata_changed?) { metadata.try :changed? }
|
76
|
+
else
|
77
|
+
raise "Cannot redefine existing metadata fields: #{(fields.keys & self.metadata_fields.keys).to_sentence}" unless (fields.keys & self.metadata_fields.keys).empty?
|
78
|
+
self.metadata_fields = self.metadata_fields.merge(fields)
|
79
|
+
end
|
74
80
|
|
75
81
|
fields.each do |name, options|
|
76
82
|
# delegate all attribute methods to the metadata
|
@@ -78,6 +84,7 @@ module HasMetadata
|
|
78
84
|
|
79
85
|
if options.kind_of?(Hash) then
|
80
86
|
type = options.delete(:type)
|
87
|
+
type_validate = !options.delete(:skip_type_validation)
|
81
88
|
options.delete :default
|
82
89
|
|
83
90
|
validate do |obj|
|
@@ -85,7 +92,7 @@ module HasMetadata
|
|
85
92
|
errors.add(name, :incorrect_type) unless
|
86
93
|
HasMetadata.metadata_typecast(value, type).kind_of?(type) or
|
87
94
|
((options[:allow_nil] and value.nil?) or (options[:allow_blank] and value.blank?))
|
88
|
-
end if type
|
95
|
+
end if type && type_validate
|
89
96
|
validates(name, options) unless options.empty? or (options.keys - [ :allow_nil, :allow_blank ]).empty?
|
90
97
|
end
|
91
98
|
end
|
@@ -96,6 +103,19 @@ module HasMetadata
|
|
96
103
|
|
97
104
|
module InstanceMethods
|
98
105
|
|
106
|
+
def as_json(options={})
|
107
|
+
options ||= Hash.new # the JSON encoder can sometimes give us nil options?
|
108
|
+
options[:except] = Array.wrap(options[:except]) + [ :metadata_id ]
|
109
|
+
options[:methods] = Array.wrap(options[:methods]) + metadata_fields.keys - options[:except].map(&:to_sym)
|
110
|
+
super options
|
111
|
+
end
|
112
|
+
|
113
|
+
def to_xml(options={})
|
114
|
+
options[:except] = Array.wrap(options[:except]) + [ :metadata_id ]
|
115
|
+
options[:methods] = Array.wrap(options[:methods]) + metadata_fields.keys - options[:except].map(&:to_sym)
|
116
|
+
super options
|
117
|
+
end
|
118
|
+
|
99
119
|
# @private
|
100
120
|
def assign_multiparameter_attributes(pairs)
|
101
121
|
fake_attributes = pairs.select { |(field, _)| self.class.metadata_fields.include? field[0, field.index('(')].to_sym }
|
@@ -124,13 +144,17 @@ module HasMetadata
|
|
124
144
|
|
125
145
|
# @return [Metadata] An existing associated {Metadata} instance, or new,
|
126
146
|
# saved one if none was found.
|
127
|
-
|
147
|
+
|
128
148
|
def metadata!
|
129
149
|
if instance_variables.include?(:@metadata) then
|
130
|
-
metadata
|
150
|
+
metadata
|
131
151
|
else
|
132
|
-
|
133
|
-
|
152
|
+
if new_record? then
|
153
|
+
metadata || build_metadata
|
154
|
+
else
|
155
|
+
metadata || Metadata.transaction { metadata || create_metadata }
|
156
|
+
end
|
157
|
+
end.set_fields self.class.metadata_fields
|
134
158
|
end
|
135
159
|
|
136
160
|
# @private
|
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.2.
|
4
|
+
version: 1.2.3
|
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: 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: &70201000005300 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - <
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '3.
|
21
|
+
version: '3.1'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70201000005300
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: boolean
|
27
|
-
requirement: &
|
27
|
+
requirement: &70201000004600 !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: *70201000004600
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: jeweler
|
38
|
-
requirement: &
|
38
|
+
requirement: &70201000004040 !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: *70201000004040
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: yard
|
49
|
-
requirement: &
|
49
|
+
requirement: &70201000003480 !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: *70201000003480
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: RedCloth
|
60
|
-
requirement: &
|
60
|
+
requirement: &70201000002860 !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: *70201000002860
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sqlite3
|
71
|
-
requirement: &
|
71
|
+
requirement: &70201000002280 !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: *70201000002280
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rspec
|
82
|
-
requirement: &
|
82
|
+
requirement: &70201000001640 !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: *70201000001640
|
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.
|
@@ -98,21 +98,12 @@ extra_rdoc_files:
|
|
98
98
|
- LICENSE
|
99
99
|
- README.textile
|
100
100
|
files:
|
101
|
-
- .document
|
102
|
-
- .rspec
|
103
|
-
- Gemfile
|
104
|
-
- Gemfile.lock
|
105
101
|
- LICENSE
|
106
102
|
- README.textile
|
107
|
-
- Rakefile
|
108
|
-
- VERSION
|
109
103
|
- has_metadata.gemspec
|
110
104
|
- lib/has_metadata.rb
|
111
105
|
- lib/has_metadata/metadata_generator.rb
|
112
106
|
- lib/has_metadata/model.rb
|
113
|
-
- spec/has_metadata_spec.rb
|
114
|
-
- spec/metadata_spec.rb
|
115
|
-
- spec/spec_helper.rb
|
116
107
|
- templates/create_metadata.rb
|
117
108
|
- templates/metadata.rb
|
118
109
|
homepage: http://github.com/riscfuture/has_metadata
|
@@ -135,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
126
|
version: '0'
|
136
127
|
requirements: []
|
137
128
|
rubyforge_project:
|
138
|
-
rubygems_version: 1.8.
|
129
|
+
rubygems_version: 1.8.15
|
139
130
|
signing_key:
|
140
131
|
specification_version: 3
|
141
132
|
summary: Reduce your table width by moving non-indexed columns to a separate metadata
|
data/.document
DELETED
data/.rspec
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
-cfs
|
data/Gemfile
DELETED
data/Gemfile.lock
DELETED
@@ -1,98 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
RedCloth (4.2.7)
|
5
|
-
abstract (1.0.0)
|
6
|
-
actionmailer (3.0.9)
|
7
|
-
actionpack (= 3.0.9)
|
8
|
-
mail (~> 2.2.19)
|
9
|
-
actionpack (3.0.9)
|
10
|
-
activemodel (= 3.0.9)
|
11
|
-
activesupport (= 3.0.9)
|
12
|
-
builder (~> 2.1.2)
|
13
|
-
erubis (~> 2.6.6)
|
14
|
-
i18n (~> 0.5.0)
|
15
|
-
rack (~> 1.2.1)
|
16
|
-
rack-mount (~> 0.6.14)
|
17
|
-
rack-test (~> 0.5.7)
|
18
|
-
tzinfo (~> 0.3.23)
|
19
|
-
activemodel (3.0.9)
|
20
|
-
activesupport (= 3.0.9)
|
21
|
-
builder (~> 2.1.2)
|
22
|
-
i18n (~> 0.5.0)
|
23
|
-
activerecord (3.0.9)
|
24
|
-
activemodel (= 3.0.9)
|
25
|
-
activesupport (= 3.0.9)
|
26
|
-
arel (~> 2.0.10)
|
27
|
-
tzinfo (~> 0.3.23)
|
28
|
-
activeresource (3.0.9)
|
29
|
-
activemodel (= 3.0.9)
|
30
|
-
activesupport (= 3.0.9)
|
31
|
-
activesupport (3.0.9)
|
32
|
-
arel (2.0.10)
|
33
|
-
boolean (1.0.1)
|
34
|
-
builder (2.1.2)
|
35
|
-
diff-lcs (1.1.2)
|
36
|
-
erubis (2.6.6)
|
37
|
-
abstract (>= 1.0.0)
|
38
|
-
git (1.2.5)
|
39
|
-
i18n (0.5.0)
|
40
|
-
jeweler (1.6.4)
|
41
|
-
bundler (~> 1.0)
|
42
|
-
git (>= 1.2.5)
|
43
|
-
rake
|
44
|
-
mail (2.2.19)
|
45
|
-
activesupport (>= 2.3.6)
|
46
|
-
i18n (>= 0.4.0)
|
47
|
-
mime-types (~> 1.16)
|
48
|
-
treetop (~> 1.4.8)
|
49
|
-
mime-types (1.16)
|
50
|
-
polyglot (0.3.2)
|
51
|
-
rack (1.2.3)
|
52
|
-
rack-mount (0.6.14)
|
53
|
-
rack (>= 1.0.0)
|
54
|
-
rack-test (0.5.7)
|
55
|
-
rack (>= 1.0)
|
56
|
-
rails (3.0.9)
|
57
|
-
actionmailer (= 3.0.9)
|
58
|
-
actionpack (= 3.0.9)
|
59
|
-
activerecord (= 3.0.9)
|
60
|
-
activeresource (= 3.0.9)
|
61
|
-
activesupport (= 3.0.9)
|
62
|
-
bundler (~> 1.0)
|
63
|
-
railties (= 3.0.9)
|
64
|
-
railties (3.0.9)
|
65
|
-
actionpack (= 3.0.9)
|
66
|
-
activesupport (= 3.0.9)
|
67
|
-
rake (>= 0.8.7)
|
68
|
-
rdoc (~> 3.4)
|
69
|
-
thor (~> 0.14.4)
|
70
|
-
rake (0.9.2)
|
71
|
-
rdoc (3.9.1)
|
72
|
-
rspec (2.6.0)
|
73
|
-
rspec-core (~> 2.6.0)
|
74
|
-
rspec-expectations (~> 2.6.0)
|
75
|
-
rspec-mocks (~> 2.6.0)
|
76
|
-
rspec-core (2.6.4)
|
77
|
-
rspec-expectations (2.6.0)
|
78
|
-
diff-lcs (~> 1.1.2)
|
79
|
-
rspec-mocks (2.6.0)
|
80
|
-
sqlite3 (1.3.4)
|
81
|
-
thor (0.14.6)
|
82
|
-
treetop (1.4.10)
|
83
|
-
polyglot
|
84
|
-
polyglot (>= 0.3.1)
|
85
|
-
tzinfo (0.3.29)
|
86
|
-
yard (0.7.2)
|
87
|
-
|
88
|
-
PLATFORMS
|
89
|
-
ruby
|
90
|
-
|
91
|
-
DEPENDENCIES
|
92
|
-
RedCloth
|
93
|
-
boolean
|
94
|
-
jeweler
|
95
|
-
rails (>= 3.0)
|
96
|
-
rspec
|
97
|
-
sqlite3
|
98
|
-
yard
|
data/Rakefile
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'bundler'
|
3
|
-
begin
|
4
|
-
Bundler.setup(:default, :development)
|
5
|
-
rescue Bundler::BundlerError => e
|
6
|
-
$stderr.puts e.message
|
7
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
-
exit e.status_code
|
9
|
-
end
|
10
|
-
require 'rake'
|
11
|
-
|
12
|
-
require 'jeweler'
|
13
|
-
Jeweler::Tasks.new do |gem|
|
14
|
-
gem.name = "has_metadata"
|
15
|
-
gem.summary = %Q{Reduce your table width by moving non-indexed columns to a separate metadata table}
|
16
|
-
gem.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.}
|
17
|
-
gem.email = "git@timothymorgan.info"
|
18
|
-
gem.homepage = "http://github.com/riscfuture/has_metadata"
|
19
|
-
gem.authors = [ "Tim Morgan" ]
|
20
|
-
gem.required_ruby_version = '>= 1.9'
|
21
|
-
end
|
22
|
-
Jeweler::RubygemsDotOrgTasks.new
|
23
|
-
|
24
|
-
require 'rspec/core/rake_task'
|
25
|
-
RSpec::Core::RakeTask.new
|
26
|
-
|
27
|
-
require 'yard'
|
28
|
-
YARD::Rake::YardocTask.new('doc') do |doc|
|
29
|
-
doc.options << "-m" << "textile"
|
30
|
-
doc.options << "--protected"
|
31
|
-
doc.options << "-r" << "README.textile"
|
32
|
-
doc.options << "-o" << "doc"
|
33
|
-
doc.options << "--title" << "has_metadata Documentation"
|
34
|
-
|
35
|
-
doc.files = [ 'lib/**/*', 'README.textile', 'templates/metadata.rb' ]
|
36
|
-
end
|
37
|
-
|
38
|
-
task(default: :spec)
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.2.2
|
data/spec/has_metadata_spec.rb
DELETED
@@ -1,224 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
module SpecSupport
|
4
|
-
class ConstructorTester
|
5
|
-
attr_reader :args
|
6
|
-
def initialize(*args) @args = args end
|
7
|
-
end
|
8
|
-
|
9
|
-
class HasMetadataTester < ActiveRecord::Base
|
10
|
-
include HasMetadata
|
11
|
-
set_table_name 'users'
|
12
|
-
has_metadata({
|
13
|
-
untyped: {},
|
14
|
-
can_be_nil: { type: Date, allow_nil: true },
|
15
|
-
can_be_nil_with_default: { type: Date, allow_nil: true, default: Date.today },
|
16
|
-
can_be_blank: { type: Date, allow_blank: true },
|
17
|
-
can_be_blank_with_default: { type: Date, allow_blank: true, default: Date.today },
|
18
|
-
cannot_be_nil_with_default: { type: Boolean, allow_nil: false, default: false },
|
19
|
-
number: { type: Fixnum, numericality: true },
|
20
|
-
boolean: { type: Boolean },
|
21
|
-
multiparam: { type: SpecSupport::ConstructorTester },
|
22
|
-
has_default: { default: 'default' }
|
23
|
-
})
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
describe HasMetadata do
|
28
|
-
describe "#has_metadata" do
|
29
|
-
it "should add a :metadata association" do
|
30
|
-
SpecSupport::HasMetadataTester.reflect_on_association(:metadata).macro.should eql(:belongs_to)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should set the model to accept nested attributes for :metadata" do
|
34
|
-
SpecSupport::HasMetadataTester.nested_attributes_options[:metadata].should_not be_nil
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should define methods for each field" do
|
38
|
-
[ :attribute, :attribute_before_type_cast, :attribute= ].each do |meth|
|
39
|
-
SpecSupport::HasMetadataTester.new.should respond_to(meth.to_s.sub('attribute', 'untyped'))
|
40
|
-
SpecSupport::HasMetadataTester.new.should respond_to(meth.to_s.sub('attribute', 'multiparam'))
|
41
|
-
SpecSupport::HasMetadataTester.new.should respond_to(meth.to_s.sub('attribute', 'number'))
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
[ :attribute, :attribute_before_type_cast ].each do |getter|
|
46
|
-
describe "##{getter}" do
|
47
|
-
before :each do
|
48
|
-
@object = SpecSupport::HasMetadataTester.new
|
49
|
-
@metadata = @object.metadata!
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should return a field in the metadata object" do
|
53
|
-
@metadata.data[:untyped] = 'bar'
|
54
|
-
@object.send(getter.to_s.sub('attribute', 'untyped')).should eql('bar')
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should return nil if there is no associated metadata" do
|
58
|
-
@object.stub!(:metadata).and_return(nil)
|
59
|
-
ivars = @object.instance_variables - [ :@metadata ]
|
60
|
-
@object.stub!(:instance_variables).and_return(ivars)
|
61
|
-
|
62
|
-
@object.send(getter.to_s.sub('attribute', 'untyped')).should be_nil
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should return a default if one is specified" do
|
66
|
-
@object.send(getter.to_s.sub('attribute', 'has_default')).should eql('default')
|
67
|
-
end
|
68
|
-
|
69
|
-
it "should return nil if nil is stored and the default is not nil" do
|
70
|
-
@metadata.data[:has_default] = nil
|
71
|
-
@object.send(getter.to_s.sub('attribute', 'has_default')).should eql(nil)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
describe "#attribute=" do
|
77
|
-
before :each do
|
78
|
-
@object = SpecSupport::HasMetadataTester.new
|
79
|
-
@metadata = @object.metadata!
|
80
|
-
@object.boolean = false
|
81
|
-
@object.multiparam = SpecSupport::ConstructorTester.new(1,2,3)
|
82
|
-
end
|
83
|
-
|
84
|
-
it "should set the value in the metadata object" do
|
85
|
-
@object.untyped = 'foo'
|
86
|
-
@metadata.data[:untyped].should eql('foo')
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should create the metadata object if it doesn't exist" do
|
90
|
-
@object.stub!(:metadata).and_return(nil)
|
91
|
-
ivars = @object.instance_variables - [ :@metadata ]
|
92
|
-
@object.stub!(:instance_variables).and_return(ivars)
|
93
|
-
Metadata.should_receive(:new).once.and_return(@metadata)
|
94
|
-
|
95
|
-
@object.untyped = 'foo'
|
96
|
-
@metadata.data[:untyped].should eql('foo')
|
97
|
-
end
|
98
|
-
|
99
|
-
it "should enforce a type if given" do
|
100
|
-
@object.multiparam = 'not correct'
|
101
|
-
@object.should_not be_valid
|
102
|
-
@object.errors[:multiparam].should_not be_empty
|
103
|
-
end
|
104
|
-
|
105
|
-
it "should cast a type if possible" do
|
106
|
-
@object.number = "50"
|
107
|
-
@object.should be_valid
|
108
|
-
@object.number.should eql(50)
|
109
|
-
|
110
|
-
@object.boolean = "1"
|
111
|
-
@object.should be_valid
|
112
|
-
@object.boolean.should eql(true)
|
113
|
-
|
114
|
-
@object.boolean = "0"
|
115
|
-
@object.should be_valid
|
116
|
-
@object.boolean.should eql(false)
|
117
|
-
end
|
118
|
-
|
119
|
-
it "should not enforce a type if :allow_nil is given" do
|
120
|
-
@object.can_be_nil = nil
|
121
|
-
@object.valid? #@object.should be_valid
|
122
|
-
@object.errors[:can_be_nil].should be_empty
|
123
|
-
end
|
124
|
-
|
125
|
-
it "should not enforce a type if :allow_blank is given" do
|
126
|
-
@object.can_be_blank = ""
|
127
|
-
@object.valid? #@object.should be_valid
|
128
|
-
@object.errors[:can_be_blank].should be_empty
|
129
|
-
end
|
130
|
-
|
131
|
-
it "should set to the default if given nil and allow_blank or allow_nil are false" do
|
132
|
-
@object.can_be_nil_with_default = nil
|
133
|
-
@object.can_be_nil_with_default.should be_nil
|
134
|
-
|
135
|
-
@object.can_be_blank_with_default = nil
|
136
|
-
@object.can_be_blank_with_default.should be_nil
|
137
|
-
|
138
|
-
@object.cannot_be_nil_with_default.should eql(false)
|
139
|
-
|
140
|
-
@object.cannot_be_nil_with_default = nil
|
141
|
-
@object.should_not be_valid
|
142
|
-
@object.errors[:cannot_be_nil_with_default].should_not be_empty
|
143
|
-
end
|
144
|
-
|
145
|
-
it "should enforce other validations as given" do
|
146
|
-
@object.number = 'not number'
|
147
|
-
@object.should_not be_valid
|
148
|
-
@object.errors[:number].should_not be_empty
|
149
|
-
end
|
150
|
-
|
151
|
-
it "should mass-assign a multiparameter attribute" do
|
152
|
-
@object.attributes = { 'multiparam(1)' => 'foo', 'multiparam(2)' => '1' }
|
153
|
-
@object.multiparam.args.should eql([ 'foo', '1' ])
|
154
|
-
end
|
155
|
-
|
156
|
-
it "should compact blank multiparameter parts" do
|
157
|
-
@object.attributes = { 'multiparam(1)' => '', 'multiparam(2)' => 'foo' }
|
158
|
-
@object.multiparam.args.should eql([ 'foo' ])
|
159
|
-
end
|
160
|
-
|
161
|
-
it "should typecast multiparameter parts" do
|
162
|
-
@object.attributes = { 'multiparam(1i)' => '1982', 'multiparam(2f)' => '10.5' }
|
163
|
-
@object.multiparam.args.should eql([ 1982, 10.5 ])
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
describe "#attribute?" do
|
168
|
-
before :each do
|
169
|
-
@object = SpecSupport::HasMetadataTester.new
|
170
|
-
@metadata = @object.metadata!
|
171
|
-
end
|
172
|
-
|
173
|
-
context "untyped field" do
|
174
|
-
it "should return true if the string is not blank" do
|
175
|
-
@metadata.data = { untyped: 'foo' }
|
176
|
-
@object.untyped?.should be_true
|
177
|
-
end
|
178
|
-
|
179
|
-
it "should return false if the string is blank" do
|
180
|
-
@metadata.data = { untyped: ' ' }
|
181
|
-
@object.untyped?.should be_false
|
182
|
-
|
183
|
-
@metadata.data = { untyped: '' }
|
184
|
-
@object.untyped?.should be_false
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
context "numeric field" do
|
189
|
-
it "should return true if the number is not zero" do
|
190
|
-
@metadata.data = { number: 4 }
|
191
|
-
@object.number?.should be_true
|
192
|
-
end
|
193
|
-
|
194
|
-
it "should return false if the number is zero" do
|
195
|
-
@metadata.data = { number: 0 }
|
196
|
-
@object.number?.should be_false
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
context "typed, non-numeric field" do
|
201
|
-
it "should return true if the string is not blank" do
|
202
|
-
@metadata.data = { can_be_nil: Date.today }
|
203
|
-
@object.can_be_nil?.should be_true
|
204
|
-
end
|
205
|
-
|
206
|
-
it "should return false if the string is blank" do
|
207
|
-
@metadata.data = { can_be_nil: nil }
|
208
|
-
@object.can_be_nil?.should be_false
|
209
|
-
end
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
context "[association]" do
|
214
|
-
it "should save the metadata when it is changed" do
|
215
|
-
object = SpecSupport::HasMetadataTester.new
|
216
|
-
object.number = 123
|
217
|
-
object.boolean = true
|
218
|
-
object.multiparam = SpecSupport::ConstructorTester.new(1,2,3)
|
219
|
-
object.metadata.should_receive(:save).once.and_return(true)
|
220
|
-
object.save!
|
221
|
-
end
|
222
|
-
end
|
223
|
-
end
|
224
|
-
end
|
data/spec/metadata_spec.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
describe Metadata do
|
4
|
-
describe ".new" do
|
5
|
-
it "should initialize data to an empty hash" do
|
6
|
-
Metadata.new.data.should eql({})
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should initialize data to the value given in the initializer" do
|
10
|
-
Metadata.new(data: { foo: 'bar' }).data.should eql(foo: 'bar')
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should set empty strings to nil" do
|
14
|
-
Metadata.create!(data: { foo: '' }).data.should eql(foo: nil)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should not set false values to nil" do
|
18
|
-
Metadata.create!(data: { foo: false }).data.should eql(foo: false)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'bundler'
|
2
|
-
Bundler.require :default, :development
|
3
|
-
require 'active_support'
|
4
|
-
require 'active_record'
|
5
|
-
|
6
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
|
-
|
9
|
-
require 'has_metadata'
|
10
|
-
|
11
|
-
ActiveRecord::Base.establish_connection(
|
12
|
-
adapter: 'sqlite3',
|
13
|
-
database: 'test.sqlite'
|
14
|
-
)
|
15
|
-
require "#{File.dirname __FILE__}/../templates/metadata"
|
16
|
-
|
17
|
-
RSpec.configure do |config|
|
18
|
-
config.before(:each) do
|
19
|
-
Metadata.connection.execute "DROP TABLE IF EXISTS metadata"
|
20
|
-
Metadata.connection.execute "CREATE TABLE metadata (id INTEGER PRIMARY KEY ASC, data TEXT)"
|
21
|
-
Metadata.connection.execute "DROP TABLE IF EXISTS users"
|
22
|
-
Metadata.connection.execute "CREATE TABLE users (id INTEGER PRIMARY KEY ASC)"
|
23
|
-
end
|
24
|
-
end
|