hashed_attributes 0.0.1
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/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.rdoc +51 -0
- data/Rakefile +2 -0
- data/hashed_attributes.gemspec +21 -0
- data/lib/hashed_attributes/version.rb +3 -0
- data/lib/hashed_attributes.rb +49 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
= Hashed Attributes
|
2
|
+
|
3
|
+
Requires a column to be used for a serialized hash
|
4
|
+
|
5
|
+
# db/migrations/****_create_people.rb
|
6
|
+
t.text :preferences
|
7
|
+
|
8
|
+
|
9
|
+
In the model you declare the column name and a list of attributes the hash will store
|
10
|
+
# app/models/person.rb
|
11
|
+
class Person < ActiveRecord::Base
|
12
|
+
hashed_attributes :preferences, :theme, :plan, :favorite_color
|
13
|
+
end
|
14
|
+
|
15
|
+
== Initialization
|
16
|
+
Initializes preferences as a hash
|
17
|
+
Person.new
|
18
|
+
{
|
19
|
+
:id => nil,
|
20
|
+
:name => nil,
|
21
|
+
:preferences => {},
|
22
|
+
:emails => nil,
|
23
|
+
:created_at => nil,
|
24
|
+
:updated_at => nil
|
25
|
+
}
|
26
|
+
|
27
|
+
== Usage
|
28
|
+
Hash keys are made available as getter/setter methods on the model
|
29
|
+
|
30
|
+
person = Person.new(:theme=>"molokai", :plan => "pro", :favorite_color=>"orange")
|
31
|
+
{
|
32
|
+
:id => nil,
|
33
|
+
:name => nil,
|
34
|
+
:preferences => {
|
35
|
+
:favorite_color => "orange",
|
36
|
+
:theme => "molokai",
|
37
|
+
:plan => "pro"
|
38
|
+
},
|
39
|
+
:emails => nil,
|
40
|
+
:created_at => nil,
|
41
|
+
:updated_at => nil
|
42
|
+
}
|
43
|
+
|
44
|
+
Methods can be used normally and are stored in the preferences hash
|
45
|
+
person.theme = 'the blue theme'
|
46
|
+
person.plan = 'pro account'
|
47
|
+
person.favorite_color = 'black'
|
48
|
+
|
49
|
+
person.theme
|
50
|
+
# => 'the blue theme'
|
51
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "hashed_attributes/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "hashed_attributes"
|
7
|
+
s.version = HashedAttributes::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Sean Behan"]
|
10
|
+
s.email = ["bseanvt@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/bseanvt/hashed_attributes"
|
12
|
+
s.summary = %q{Key/value store...}
|
13
|
+
s.description = %q{Key/value store...}
|
14
|
+
|
15
|
+
s.rubyforge_project = "hashed_attributes"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module HashedAttributes
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def hashed_attributes name, *options
|
9
|
+
include InstanceMethods
|
10
|
+
|
11
|
+
serialize name.to_sym, Hash
|
12
|
+
|
13
|
+
options.each do |n|
|
14
|
+
define_method "#{n}" do
|
15
|
+
get_hashed_attribute_for(n)
|
16
|
+
end
|
17
|
+
|
18
|
+
define_method "#{n}=" do |arg|
|
19
|
+
set_hashed_attribute_for(n,arg)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
define_method "hashed_attributes_column" do
|
24
|
+
name
|
25
|
+
end
|
26
|
+
|
27
|
+
self.after_initialize do
|
28
|
+
initialize_hashed_attributes
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module InstanceMethods
|
34
|
+
def get_hashed_attribute_for(key)
|
35
|
+
self[hashed_attributes_column][key]
|
36
|
+
end
|
37
|
+
|
38
|
+
def set_hashed_attribute_for(key,value)
|
39
|
+
initialize_hashed_attributes
|
40
|
+
self[hashed_attributes_column][key]=value
|
41
|
+
end
|
42
|
+
|
43
|
+
def initialize_hashed_attributes
|
44
|
+
self[hashed_attributes_column] = {} if self[hashed_attributes_column].nil?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
ActiveRecord::Base.send(:include, HashedAttributes)
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashed_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sean Behan
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-16 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Key/value store...
|
23
|
+
email:
|
24
|
+
- bseanvt@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- hashed_attributes.gemspec
|
37
|
+
- lib/hashed_attributes.rb
|
38
|
+
- lib/hashed_attributes/version.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: https://github.com/bseanvt/hashed_attributes
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: hashed_attributes
|
69
|
+
rubygems_version: 1.3.7
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Key/value store...
|
73
|
+
test_files: []
|
74
|
+
|