document_record 0.0.11

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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Kasthor
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.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = DocumentRecord
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'DocumentRecord'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,100 @@
1
+ require 'document_hash'
2
+
3
+ module DocumentRecord
4
+ module Serializer
5
+ def self.dump object
6
+ Base64.encode64 Marshal.dump object
7
+ end
8
+ def self.load data
9
+ Marshal.load Base64.decode64 data rescue nil
10
+ end
11
+ end
12
+
13
+ module Base
14
+ extend ActiveSupport::Concern
15
+
16
+ def document_field name
17
+ raise "Field must exist in record in order to become a document field" unless column_names.include? name.to_s
18
+ @@_document_field_name = name
19
+ @@_index_fields ||= []
20
+
21
+ class_eval do
22
+ alias_method :regular_assign_attributes, :assign_attributes
23
+ alias_method :regular_method_missing, :method_missing
24
+ alias_method :regular_save, :save
25
+
26
+ def read_serialized_hash_attribute field_name
27
+ raw = read_attribute field_name
28
+ raw && Serializer.load( raw ) || {}
29
+ end
30
+
31
+ def write_serialized_hash_attribute field_name, hash
32
+ write_attribute field_name, Serializer.dump(hash)
33
+ end
34
+
35
+ def document
36
+ @document ||= ::DocumentHash::Core[read_serialized_hash_attribute(@@_document_field_name)].tap do |d|
37
+ d.before_change do |path, value|
38
+ key = path.join "_"
39
+
40
+ value = case self.class.columns_hash[key].type
41
+ when :integer then value.to_i
42
+ else value
43
+ end if self.class.columns_hash[key]
44
+
45
+ value
46
+ end
47
+ d.after_change do |path, value|
48
+ key = path.join "_"
49
+ write_attribute key, value if self.class.columns_hash[key]
50
+ end
51
+ end
52
+ end
53
+
54
+ def save *arguments
55
+ save_document
56
+ regular_save *arguments
57
+ end
58
+
59
+ def save_document
60
+ write_serialized_hash_attribute @@_document_field_name, document.to_hash
61
+ end
62
+
63
+ def assign_attributes new_attributes, options
64
+ new_attributes.each do | key, value |
65
+ self.send "#{key}=".to_sym, value
66
+ end
67
+ end
68
+
69
+ def method_missing method, *args
70
+ if method =~ /(.*)=$/
71
+ document[$1] = args.shift
72
+ else
73
+ document[method.to_s]
74
+ end
75
+ end
76
+
77
+ def is_indexed? attr
78
+ self.class.column_names.include? attr.to_s
79
+ end
80
+
81
+ def as_json options = {}
82
+ ( read_serialized_hash_attribute(@@_document_field_name) || {} ).merge(super.reject{ |k, v| k === @@_document_field_name.to_s })
83
+ end
84
+ end
85
+
86
+ column_names.each do |column|
87
+ class_eval <<-METHOD
88
+ def #{column}
89
+ document["#{column}"] || read_attribute("#{column}")
90
+ end
91
+ def #{column}= value
92
+ document["#{column}"] = value
93
+ end
94
+ METHOD
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ ActiveRecord::Base.send :extend, DocumentRecord::Base
@@ -0,0 +1,3 @@
1
+ module DocumentRecord
2
+ VERSION = "0.0.11"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :document_record do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: document_record
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.11
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kasthor Corleone
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: document_hash
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.12
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.0.12
30
+ - !ruby/object:Gem::Dependency
31
+ name: rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 3.2.11
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 3.2.11
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec-rails
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: debugger
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Uses an Active Record as a schema-less document
95
+ email:
96
+ - kasthor@kasthor.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - lib/document_record/version.rb
102
+ - lib/document_record.rb
103
+ - lib/tasks/document_record_tasks.rake
104
+ - MIT-LICENSE
105
+ - Rakefile
106
+ - README.rdoc
107
+ homepage: ''
108
+ licenses: []
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.23
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Uses an Active Record as a schema-less document
131
+ test_files: []