marshaled_attributes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem "activerecord"
4
+ gem "rspec", "2.0.0.rc"
5
+ gem "sqlite3-ruby"
@@ -0,0 +1,38 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.0)
5
+ activesupport (= 3.0.0)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.4.1)
8
+ activerecord (3.0.0)
9
+ activemodel (= 3.0.0)
10
+ activesupport (= 3.0.0)
11
+ arel (~> 1.0.0)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.0)
14
+ arel (1.0.1)
15
+ activesupport (~> 3.0.0)
16
+ builder (2.1.2)
17
+ diff-lcs (1.1.2)
18
+ i18n (0.4.1)
19
+ rspec (2.0.0.rc)
20
+ rspec-core (= 2.0.0.rc)
21
+ rspec-expectations (= 2.0.0.rc)
22
+ rspec-mocks (= 2.0.0.rc)
23
+ rspec-core (2.0.0.rc)
24
+ rspec-expectations (2.0.0.rc)
25
+ diff-lcs (>= 1.1.2)
26
+ rspec-mocks (2.0.0.rc)
27
+ rspec-core (= 2.0.0.rc)
28
+ rspec-expectations (= 2.0.0.rc)
29
+ sqlite3-ruby (1.3.1)
30
+ tzinfo (0.3.23)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ activerecord
37
+ rspec (= 2.0.0.rc)
38
+ sqlite3-ruby
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Nando Vieira
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,29 @@
1
+ = Marshaled Attributes
2
+
3
+ Save marshaled objects using ActiveRecord.
4
+
5
+ == Instalation
6
+
7
+ gem install marshaled_attributes
8
+
9
+ == Usage
10
+
11
+ Considering the following schema:
12
+
13
+ create_table :pages do |t|
14
+ t.string :name
15
+ t.text :body
16
+ t.binary :meta
17
+ end
18
+
19
+ Add the method call <tt>marshaled_attributes</tt> to your model.
20
+
21
+ class Page < ActiveRecord::Base
22
+ marshaled :meta
23
+ end
24
+
25
+ @page = Page.new(:meta => {:last_comment_id => 100})
26
+ @page.meta
27
+ #=> {:last_comment_id => 100}
28
+
29
+ Copyright (c) 2008 Nando Vieira, released under the MIT license
@@ -0,0 +1,25 @@
1
+ require "rspec/core/rake_task"
2
+ require "./lib/marshaled_attributes/version"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ begin
7
+ require "jeweler"
8
+
9
+ JEWEL = Jeweler::Tasks.new do |gem|
10
+ gem.name = "marshaled_attributes"
11
+ gem.version = MarshaledAttributes::Version::STRING
12
+ gem.summary = "Save marshaled objects using ActiveRecord"
13
+ gem.description = "Save marshaled objects using ActiveRecord"
14
+ gem.authors = ["Nando Vieira"]
15
+ gem.email = "fnando.vieira@gmail.com"
16
+ gem.homepage = "http://github.com/fnando/marshaled_attributes"
17
+ gem.has_rdoc = false
18
+ gem.add_dependency "rails", ">= 3.0.0"
19
+ gem.files = FileList["{Gemfile,Gemfile.lock,Rakefile,MIT-LICENSE,marshaled_attributes.gemspec,README.*}", "{lib,spec,templates}/**/*"]
20
+ end
21
+
22
+ Jeweler::GemcutterTasks.new
23
+ rescue LoadError => e
24
+ puts "You don't Jeweler installed, so you won't be able to build gems."
25
+ end
@@ -0,0 +1,5 @@
1
+ require "active_record"
2
+ require "marshaled_attributes/active_record"
3
+ require "marshaled_attributes/version"
4
+
5
+ ActiveRecord::Base.send(:include, MarshaledAttributes::ActiveRecord)
@@ -0,0 +1,43 @@
1
+ module MarshaledAttributes
2
+ module ActiveRecord
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+
6
+ class << base
7
+ attr_accessor :marshaled_attributes_options
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ # marshaled :title, :settings
13
+ def marshaled(*attrs)
14
+ self.marshaled_attributes_options = {
15
+ :attributes => attrs
16
+ }
17
+
18
+ include InstanceMethods
19
+
20
+ after_initialize do
21
+ add_marshaled_attributes
22
+ end
23
+ end
24
+ end
25
+
26
+ module InstanceMethods
27
+ private
28
+ def add_marshaled_attributes
29
+ self.class.class_eval do
30
+ marshaled_attributes_options[:attributes].each do |name|
31
+ define_method name do
32
+ Marshal.load read_attribute(name) unless read_attribute(name).blank?
33
+ end
34
+
35
+ define_method "#{name}=" do |value|
36
+ write_attribute(name, Marshal.dump(value))
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,8 @@
1
+ module MarshaledAttributes
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ describe MarshaledAttributes do
4
+ it "should not raise with blank values" do
5
+ beer = Beer.create!(:name => "Duffys")
6
+
7
+ expect {
8
+ beer.meta = nil
9
+ beer.meta.should == nil
10
+
11
+ beer.meta = ""
12
+ beer.meta.should == ""
13
+ }.to_not raise_error
14
+ end
15
+
16
+ it "should marshal using writer" do
17
+ expect {
18
+ beer = Beer.create!
19
+ beer.meta = {:color => :dark}
20
+ beer.save!
21
+ beer.reload
22
+ beer.meta.should == {:color => :dark}
23
+ }.to_not raise_error
24
+ end
25
+
26
+ it "should marshal using mass-assignment" do
27
+ beer = Beer.create!(:meta => {:color => :dark})
28
+ beer.reload
29
+ beer.meta.should == {:color => :dark}
30
+ end
31
+ end
@@ -0,0 +1,6 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :beers do |t|
3
+ t.string :name
4
+ t.binary :meta
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ require "rspec"
2
+ require "marshaled_attributes"
3
+
4
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
5
+
6
+ load("schema.rb")
7
+
8
+ class Beer < ActiveRecord::Base
9
+ marshaled :meta
10
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: marshaled_attributes
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
+ - Nando Vieira
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-08 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rails
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: Save marshaled objects using ActiveRecord
36
+ email: fnando.vieira@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ files:
44
+ - Gemfile
45
+ - Gemfile.lock
46
+ - MIT-LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - lib/marshaled_attributes.rb
50
+ - lib/marshaled_attributes/active_record.rb
51
+ - lib/marshaled_attributes/version.rb
52
+ - spec/marshaled_attributes_spec.rb
53
+ - spec/schema.rb
54
+ - spec/spec_helper.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/fnando/marshaled_attributes
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.7
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Save marshaled objects using ActiveRecord
87
+ test_files:
88
+ - spec/marshaled_attributes_spec.rb
89
+ - spec/schema.rb
90
+ - spec/spec_helper.rb