attr_bucket 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in attr_bucket.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ernie Miller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+ = attr_bucket
2
+
3
+ This is probably a horrible idea.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2011 {Ernie Miller}[http://metautonomo.us]. See LICENSE for details.
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "attr_bucket/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "attr_bucket"
7
+ s.version = AttrBucket::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Ernie Miller"]
10
+ s.email = ["ernie@metautonomo.us"]
11
+ s.homepage = "http://metautonomo.us"
12
+ s.summary = %q{This is probably a horrible idea.}
13
+ s.description = %q{A really, really, horrible idea.}
14
+
15
+ s.add_runtime_dependency(%q<activerecord>, ["~> 3.0.0"])
16
+
17
+ s.rubyforge_project = "attr_bucket"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,72 @@
1
+ module AttrBucket
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ private
7
+
8
+ def get_attr_bucket(name)
9
+ unless read_attribute(name).is_a?(Hash)
10
+ write_attribute(name, {})
11
+ end
12
+ read_attribute(name)
13
+ end
14
+
15
+ def explicitly_type_cast(value, type)
16
+ return nil if value.nil?
17
+ case type
18
+ when :string then value.to_s
19
+ when :text then value.to_s
20
+ when :integer then value.to_i rescue value ? 1 : 0
21
+ when :float then value.to_f
22
+ when :decimal then self.class.value_to_decimal(value)
23
+ when :datetime then self.class.string_to_time(value)
24
+ when :timestamp then self.class.string_to_time(value)
25
+ when :time then self.class.string_to_dummy_time(value)
26
+ when :date then self.class.string_to_date(value)
27
+ when :binary then self.class.binary_to_string(value)
28
+ when :boolean then self.class.value_to_boolean(value)
29
+ else value
30
+ end
31
+ end
32
+
33
+ module ClassMethods
34
+ private
35
+
36
+ def attr_bucket(opts = {})
37
+ opts.map do |bucket_name, attrs|
38
+ serialize bucket_name, Hash
39
+
40
+ if attrs.is_a?(Hash)
41
+ attrs.map do|attr_name, attr_type|
42
+ define_bucket_reader bucket_name, attr_name
43
+ define_bucket_writer bucket_name, attr_name, attr_type
44
+ end
45
+ else
46
+ Array.wrap(attrs).each do |attr_name|
47
+ define_bucket_reader bucket_name, attr_name
48
+ define_bucket_writer bucket_name, attr_name, :string
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ def define_bucket_reader(bucket_name, attr_name)
55
+ define_method attr_name do
56
+ get_attr_bucket(bucket_name)[attr_name]
57
+ end unless method_defined? attr_name
58
+ end
59
+
60
+ def define_bucket_writer(bucket_name, attr_name, attr_type)
61
+ define_method "#{attr_name}=" do |val|
62
+ # TODO: Make this more resilient/granular for multiple bucketed changes
63
+ send("#{bucket_name}_will_change!")
64
+ get_attr_bucket(bucket_name)[attr_name] = explicitly_type_cast(val, attr_type)
65
+ end unless method_defined? "#{attr_name}="
66
+ end
67
+ end
68
+ end
69
+
70
+ require 'active_record'
71
+
72
+ ActiveRecord::Base.send(:include, AttrBucket)
@@ -0,0 +1,3 @@
1
+ module AttrBucket
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attr_bucket
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Ernie Miller
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-27 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 0
32
+ version: 3.0.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: A really, really, horrible idea.
36
+ email:
37
+ - ernie@metautonomo.us
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - attr_bucket.gemspec
51
+ - lib/attr_bucket.rb
52
+ - lib/attr_bucket/version.rb
53
+ has_rdoc: true
54
+ homepage: http://metautonomo.us
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project: attr_bucket
81
+ rubygems_version: 1.3.7
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: This is probably a horrible idea.
85
+ test_files: []
86
+