has_metadata 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,6 +1,7 @@
1
1
  source :rubygems
2
2
 
3
3
  gem 'rails', '>= 3.0'
4
+ gem 'boolean'
4
5
 
5
6
  group :development do
6
7
  gem 'jeweler'
data/Gemfile.lock CHANGED
@@ -1,99 +1,97 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- RedCloth (4.2.3)
4
+ RedCloth (4.2.7)
5
5
  abstract (1.0.0)
6
- actionmailer (3.0.3)
7
- actionpack (= 3.0.3)
8
- mail (~> 2.2.9)
9
- actionpack (3.0.3)
10
- activemodel (= 3.0.3)
11
- activesupport (= 3.0.3)
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
12
  builder (~> 2.1.2)
13
13
  erubis (~> 2.6.6)
14
- i18n (~> 0.4)
14
+ i18n (~> 0.5.0)
15
15
  rack (~> 1.2.1)
16
- rack-mount (~> 0.6.13)
17
- rack-test (~> 0.5.6)
16
+ rack-mount (~> 0.6.14)
17
+ rack-test (~> 0.5.7)
18
18
  tzinfo (~> 0.3.23)
19
- activemodel (3.0.3)
20
- activesupport (= 3.0.3)
19
+ activemodel (3.0.9)
20
+ activesupport (= 3.0.9)
21
21
  builder (~> 2.1.2)
22
- i18n (~> 0.4)
23
- activerecord (3.0.3)
24
- activemodel (= 3.0.3)
25
- activesupport (= 3.0.3)
26
- arel (~> 2.0.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
27
  tzinfo (~> 0.3.23)
28
- activeresource (3.0.3)
29
- activemodel (= 3.0.3)
30
- activesupport (= 3.0.3)
31
- activesupport (3.0.3)
32
- arel (2.0.3)
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)
33
34
  builder (2.1.2)
34
35
  diff-lcs (1.1.2)
35
36
  erubis (2.6.6)
36
37
  abstract (>= 1.0.0)
37
- ffi (0.6.3)
38
- rake (>= 0.8.7)
39
38
  git (1.2.5)
40
- i18n (0.4.2)
41
- jeweler (1.5.1)
42
- bundler (~> 1.0.0)
39
+ i18n (0.5.0)
40
+ jeweler (1.6.2)
41
+ bundler (~> 1.0)
43
42
  git (>= 1.2.5)
44
43
  rake
45
- mail (2.2.10)
44
+ mail (2.2.19)
46
45
  activesupport (>= 2.3.6)
47
- i18n (~> 0.4.1)
46
+ i18n (>= 0.4.0)
48
47
  mime-types (~> 1.16)
49
48
  treetop (~> 1.4.8)
50
49
  mime-types (1.16)
51
50
  polyglot (0.3.1)
52
- rack (1.2.1)
53
- rack-mount (0.6.13)
51
+ rack (1.2.3)
52
+ rack-mount (0.6.14)
54
53
  rack (>= 1.0.0)
55
- rack-test (0.5.6)
54
+ rack-test (0.5.7)
56
55
  rack (>= 1.0)
57
- rails (3.0.3)
58
- actionmailer (= 3.0.3)
59
- actionpack (= 3.0.3)
60
- activerecord (= 3.0.3)
61
- activeresource (= 3.0.3)
62
- activesupport (= 3.0.3)
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)
63
62
  bundler (~> 1.0)
64
- railties (= 3.0.3)
65
- railties (3.0.3)
66
- actionpack (= 3.0.3)
67
- activesupport (= 3.0.3)
63
+ railties (= 3.0.9)
64
+ railties (3.0.9)
65
+ actionpack (= 3.0.9)
66
+ activesupport (= 3.0.9)
68
67
  rake (>= 0.8.7)
68
+ rdoc (~> 3.4)
69
69
  thor (~> 0.14.4)
70
- rake (0.8.7)
71
- rspec (2.1.0)
72
- rspec-core (~> 2.1.0)
73
- rspec-expectations (~> 2.1.0)
74
- rspec-mocks (~> 2.1.0)
75
- rspec-core (2.1.0)
76
- rspec-expectations (2.1.0)
70
+ rake (0.9.2)
71
+ rdoc (3.6.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)
77
78
  diff-lcs (~> 1.1.2)
78
- rspec-mocks (2.1.0)
79
- sqlite3 (0.1.1)
80
- ffi (>= 0.6.3)
81
- thor (0.14.4)
79
+ rspec-mocks (2.6.0)
80
+ sqlite3 (1.3.3)
81
+ thor (0.14.6)
82
82
  treetop (1.4.9)
83
83
  polyglot (>= 0.3.1)
84
- tzinfo (0.3.23)
85
- yard (0.6.2)
84
+ tzinfo (0.3.28)
85
+ yard (0.7.2)
86
86
 
87
87
  PLATFORMS
88
88
  ruby
89
89
 
90
90
  DEPENDENCIES
91
91
  RedCloth
92
+ boolean
92
93
  jeweler
93
94
  rails (>= 3.0)
94
95
  rspec
95
96
  sqlite3
96
97
  yard
97
-
98
- METADATA
99
- version: 1.0.6
data/README.textile CHANGED
@@ -1,7 +1,7 @@
1
1
  h1. has_metadata -- Keep your tables narrow
2
2
 
3
3
  | *Author* | Tim Morgan |
4
- | *Version* | 1.1.1 (Nov 21, 2010) |
4
+ | *Version* | 1.2 (Jun 20, 2011) |
5
5
  | *License* | Released under the MIT License. |
6
6
 
7
7
  h2. Important note for those upgrading from 1.0 to 1.1
data/Rakefile CHANGED
@@ -30,7 +30,7 @@ YARD::Rake::YardocTask.new('doc') do |doc|
30
30
  doc.options << "--protected"
31
31
  doc.options << "-r" << "README.textile"
32
32
  doc.options << "-o" << "doc"
33
- doc.options << "--title" << "has_metadata Documentation".inspect
33
+ doc.options << "--title" << "has_metadata Documentation"
34
34
 
35
35
  doc.files = [ 'lib/**/*', 'README.textile', 'templates/metadata.rb' ]
36
36
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.1
1
+ 1.2.0
data/has_metadata.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{has_metadata}
8
- s.version = "1.1.1"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Tim Morgan"]
12
- s.date = %q{2010-11-21}
11
+ s.authors = [%q{Tim Morgan}]
12
+ s.date = %q{2011-06-21}
13
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
14
  s.email = %q{git@timothymorgan.info}
15
15
  s.extra_rdoc_files = [
@@ -36,22 +36,17 @@ Gem::Specification.new do |s|
36
36
  "templates/metadata.rb"
37
37
  ]
38
38
  s.homepage = %q{http://github.com/riscfuture/has_metadata}
39
- s.require_paths = ["lib"]
39
+ s.require_paths = [%q{lib}]
40
40
  s.required_ruby_version = Gem::Requirement.new(">= 1.9")
41
- s.rubygems_version = %q{1.3.7}
41
+ s.rubygems_version = %q{1.8.5}
42
42
  s.summary = %q{Reduce your table width by moving non-indexed columns to a separate metadata table}
43
- s.test_files = [
44
- "spec/has_metadata_spec.rb",
45
- "spec/metadata_spec.rb",
46
- "spec/spec_helper.rb"
47
- ]
48
43
 
49
44
  if s.respond_to? :specification_version then
50
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
45
  s.specification_version = 3
52
46
 
53
47
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
48
  s.add_runtime_dependency(%q<rails>, [">= 3.0"])
49
+ s.add_runtime_dependency(%q<boolean>, [">= 0"])
55
50
  s.add_development_dependency(%q<jeweler>, [">= 0"])
56
51
  s.add_development_dependency(%q<yard>, [">= 0"])
57
52
  s.add_development_dependency(%q<RedCloth>, [">= 0"])
@@ -59,6 +54,7 @@ Gem::Specification.new do |s|
59
54
  s.add_development_dependency(%q<rspec>, [">= 0"])
60
55
  else
61
56
  s.add_dependency(%q<rails>, [">= 3.0"])
57
+ s.add_dependency(%q<boolean>, [">= 0"])
62
58
  s.add_dependency(%q<jeweler>, [">= 0"])
63
59
  s.add_dependency(%q<yard>, [">= 0"])
64
60
  s.add_dependency(%q<RedCloth>, [">= 0"])
@@ -67,6 +63,7 @@ Gem::Specification.new do |s|
67
63
  end
68
64
  else
69
65
  s.add_dependency(%q<rails>, [">= 3.0"])
66
+ s.add_dependency(%q<boolean>, [">= 0"])
70
67
  s.add_dependency(%q<jeweler>, [">= 0"])
71
68
  s.add_dependency(%q<yard>, [">= 0"])
72
69
  s.add_dependency(%q<RedCloth>, [">= 0"])
@@ -30,8 +30,9 @@ module HasMetadata
30
30
  singleton_class.send(:define_method, :attribute) do |name|
31
31
  name = name.to_sym
32
32
  super(name) unless fields.include?(name)
33
-
34
- default = (fields[name].kind_of?(Hash) && fields[name][:default]) ? fields[name][:default] : nil
33
+
34
+ options = fields[name] || {}
35
+ default = options.include?(:default) ? options[:default] : nil
35
36
  data.include?(name) ? data[name] : default
36
37
  end
37
38
  singleton_class.send :alias_method, :attribute_before_type_cast, :attribute
@@ -39,9 +40,10 @@ module HasMetadata
39
40
  singleton_class.send(:define_method, :query_attribute) do |name|
40
41
  name = name.to_sym
41
42
  super(name) unless fields.include?(name)
42
-
43
- if fields[name].kind_of?(Hash) and fields[name].include?(:type) then
44
- if fields[name][:type].ancestors.include?(Numeric) then
43
+
44
+ options = fields[name] || {}
45
+ if options.include?(:type) then
46
+ if options[:type].ancestors.include?(Numeric) then
45
47
  not send(name).zero?
46
48
  else
47
49
  not send(name).blank?
@@ -54,8 +56,10 @@ module HasMetadata
54
56
  singleton_class.send(:define_method, :attribute=) do |name, value|
55
57
  name = name.to_sym
56
58
  super(name, value) unless fields.include?(name)
57
-
58
- data[name] = value
59
+
60
+ options = fields[name] || {}
61
+ data_will_change!
62
+ data[name] = HasMetadata.metadata_typecast(value, options[:type])
59
63
  end
60
64
 
61
65
  self
@@ -68,7 +72,7 @@ module HasMetadata
68
72
  end
69
73
 
70
74
  def nullify_empty_fields
71
- data.each { |key, value| data[key] = nil if data[key].blank? }
75
+ data.each { |key, value| data[key] = nil if value.blank? }
72
76
  end
73
77
  end
74
78
  end
data/lib/has_metadata.rb CHANGED
@@ -20,6 +20,26 @@ end
20
20
  module HasMetadata
21
21
  extend ActiveSupport::Concern
22
22
 
23
+ # @private
24
+ def self.metadata_typecast(value, type)
25
+ if value.kind_of?(String) then
26
+ if type == Integer or type == Fixnum then
27
+ begin
28
+ return Integer(value)
29
+ rescue ArgumentError
30
+ return value
31
+ end
32
+ elsif type == Float then
33
+ begin
34
+ return Float(value)
35
+ rescue ArgumentError
36
+ return value
37
+ end
38
+ elsif type == Boolean then return value.parse_bool end
39
+ end
40
+ return value
41
+ end
42
+
23
43
  # Class methods that are added to your model.
24
44
 
25
45
  module ClassMethods
@@ -44,12 +64,12 @@ module HasMetadata
44
64
  def has_metadata(fields)
45
65
  belongs_to :metadata, dependent: :destroy
46
66
  accepts_nested_attributes_for :metadata
47
- #after_save :save_metadata, if: :metadata_changed?
67
+ after_save :save_metadata, if: :metadata_changed?
48
68
  class_inheritable_hash :metadata_fields
49
69
  self.metadata_fields = fields.deep_clone
50
70
 
51
- #define_method(:save_metadata) { metadata.save! }
52
- #define_method(:metadata_changed?) { metadata and metadata.changed? }
71
+ define_method(:save_metadata) { metadata.save! }
72
+ define_method(:metadata_changed?) { metadata.try :changed? }
53
73
 
54
74
  fields.each do |name, options|
55
75
  # delegate all attribute methods to the metadata
@@ -60,9 +80,10 @@ module HasMetadata
60
80
  options.delete :default
61
81
 
62
82
  validate do |obj|
83
+ value = obj.send(name)
63
84
  errors.add(name, :incorrect_type) unless
64
- metadata_typecast(obj.send(name), type).kind_of?(type) or
65
- ((options[:allow_nil] and obj.send(name).nil?) or (options[:allow_blank] and obj.send(name).blank?))
85
+ HasMetadata.metadata_typecast(value, type).kind_of?(type) or
86
+ ((options[:allow_nil] and value.nil?) or (options[:allow_blank] and value.blank?))
66
87
  end if type
67
88
  validates(name, options) unless options.empty? or (options.keys - [ :allow_nil, :allow_blank ]).empty?
68
89
  end
@@ -74,15 +95,6 @@ module HasMetadata
74
95
 
75
96
  module InstanceMethods
76
97
 
77
- # @private
78
- def metadata_typecast(value, type)
79
- if value.kind_of?(String) then
80
- if type == Integer or type == Fixnum then return value.to_i
81
- elsif type == Float then return value.to_f end
82
- end
83
- return value
84
- end
85
-
86
98
  # @private
87
99
  def assign_multiparameter_attributes(pairs)
88
100
  fake_attributes = pairs.select { |(field, _)| self.class.metadata_fields.include? field[0, field.index('(')].to_sym }
@@ -12,8 +12,12 @@ module SpecSupport
12
12
  has_metadata({
13
13
  untyped: {},
14
14
  can_be_nil: { type: Date, allow_nil: true },
15
+ can_be_nil_with_default: { type: Date, allow_nil: true, default: Date.today },
15
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 },
16
19
  number: { type: Fixnum, numericality: true },
20
+ boolean: { type: Boolean },
17
21
  multiparam: { type: SpecSupport::ConstructorTester },
18
22
  has_default: { default: 'default' }
19
23
  })
@@ -69,10 +73,12 @@ describe HasMetadata do
69
73
  end
70
74
  end
71
75
 
72
- context "setters" do
76
+ describe "#attribute=" do
73
77
  before :each do
74
78
  @object = SpecSupport::HasMetadataTester.new
75
79
  @metadata = @object.metadata!
80
+ @object.boolean = false
81
+ @object.multiparam = SpecSupport::ConstructorTester.new(1,2,3)
76
82
  end
77
83
 
78
84
  it "should set the value in the metadata object" do
@@ -95,6 +101,20 @@ describe HasMetadata do
95
101
  @object.should_not be_valid
96
102
  @object.errors[:multiparam].should_not be_empty
97
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
98
118
 
99
119
  it "should not enforce a type if :allow_nil is given" do
100
120
  @object.can_be_nil = nil
@@ -107,6 +127,20 @@ describe HasMetadata do
107
127
  @object.valid? #@object.should be_valid
108
128
  @object.errors[:can_be_blank].should be_empty
109
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
110
144
 
111
145
  it "should enforce other validations as given" do
112
146
  @object.number = 'not number'
@@ -175,5 +209,16 @@ describe HasMetadata do
175
209
  end
176
210
  end
177
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
178
223
  end
179
224
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'bundler'
2
+ Bundler.require :default, :development
1
3
  require 'active_support'
2
4
  require 'active_record'
3
5
 
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_metadata
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 1
8
- - 1
9
- version: 1.1.1
4
+ prerelease:
5
+ version: 1.2.0
10
6
  platform: ruby
11
7
  authors:
12
8
  - Tim Morgan
@@ -14,8 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2010-11-21 00:00:00 -08:00
18
- default_executable:
13
+ date: 2011-06-21 00:00:00 Z
19
14
  dependencies:
20
15
  - !ruby/object:Gem::Dependency
21
16
  name: rails
@@ -24,78 +19,76 @@ dependencies:
24
19
  requirements:
25
20
  - - ">="
26
21
  - !ruby/object:Gem::Version
27
- segments:
28
- - 3
29
- - 0
30
22
  version: "3.0"
31
23
  type: :runtime
32
24
  prerelease: false
33
25
  version_requirements: *id001
34
26
  - !ruby/object:Gem::Dependency
35
- name: jeweler
27
+ name: boolean
36
28
  requirement: &id002 !ruby/object:Gem::Requirement
37
29
  none: false
38
30
  requirements:
39
31
  - - ">="
40
32
  - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
33
  version: "0"
44
- type: :development
34
+ type: :runtime
45
35
  prerelease: false
46
36
  version_requirements: *id002
47
37
  - !ruby/object:Gem::Dependency
48
- name: yard
38
+ name: jeweler
49
39
  requirement: &id003 !ruby/object:Gem::Requirement
50
40
  none: false
51
41
  requirements:
52
42
  - - ">="
53
43
  - !ruby/object:Gem::Version
54
- segments:
55
- - 0
56
44
  version: "0"
57
45
  type: :development
58
46
  prerelease: false
59
47
  version_requirements: *id003
60
48
  - !ruby/object:Gem::Dependency
61
- name: RedCloth
49
+ name: yard
62
50
  requirement: &id004 !ruby/object:Gem::Requirement
63
51
  none: false
64
52
  requirements:
65
53
  - - ">="
66
54
  - !ruby/object:Gem::Version
67
- segments:
68
- - 0
69
55
  version: "0"
70
56
  type: :development
71
57
  prerelease: false
72
58
  version_requirements: *id004
73
59
  - !ruby/object:Gem::Dependency
74
- name: sqlite3
60
+ name: RedCloth
75
61
  requirement: &id005 !ruby/object:Gem::Requirement
76
62
  none: false
77
63
  requirements:
78
64
  - - ">="
79
65
  - !ruby/object:Gem::Version
80
- segments:
81
- - 0
82
66
  version: "0"
83
67
  type: :development
84
68
  prerelease: false
85
69
  version_requirements: *id005
86
70
  - !ruby/object:Gem::Dependency
87
- name: rspec
71
+ name: sqlite3
88
72
  requirement: &id006 !ruby/object:Gem::Requirement
89
73
  none: false
90
74
  requirements:
91
75
  - - ">="
92
76
  - !ruby/object:Gem::Version
93
- segments:
94
- - 0
95
77
  version: "0"
96
78
  type: :development
97
79
  prerelease: false
98
80
  version_requirements: *id006
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: &id007 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *id007
99
92
  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.
100
93
  email: git@timothymorgan.info
101
94
  executables: []
@@ -123,7 +116,6 @@ files:
123
116
  - spec/spec_helper.rb
124
117
  - templates/create_metadata.rb
125
118
  - templates/metadata.rb
126
- has_rdoc: true
127
119
  homepage: http://github.com/riscfuture/has_metadata
128
120
  licenses: []
129
121
 
@@ -137,26 +129,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
129
  requirements:
138
130
  - - ">="
139
131
  - !ruby/object:Gem::Version
140
- segments:
141
- - 1
142
- - 9
143
132
  version: "1.9"
144
133
  required_rubygems_version: !ruby/object:Gem::Requirement
145
134
  none: false
146
135
  requirements:
147
136
  - - ">="
148
137
  - !ruby/object:Gem::Version
149
- segments:
150
- - 0
151
138
  version: "0"
152
139
  requirements: []
153
140
 
154
141
  rubyforge_project:
155
- rubygems_version: 1.3.7
142
+ rubygems_version: 1.8.5
156
143
  signing_key:
157
144
  specification_version: 3
158
145
  summary: Reduce your table width by moving non-indexed columns to a separate metadata table
159
- test_files:
160
- - spec/has_metadata_spec.rb
161
- - spec/metadata_spec.rb
162
- - spec/spec_helper.rb
146
+ test_files: []
147
+