hash_accessor 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 +0 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +65 -0
- data/hash_accessor.gemspec +24 -0
- data/lib/class_methods.rb +88 -0
- data/lib/hash_accessor.rb +15 -0
- data/lib/railtie.rb +9 -0
- data/rails/init.rb +2 -0
- metadata +57 -0
data/.gitignore
ADDED
File without changes
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright on Updates - Copyright (c) 2011 OctopusApp Inc. (http://getjobber.com), released under the MIT license
|
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,65 @@
|
|
1
|
+
= HashAccessor
|
2
|
+
|
3
|
+
This gem provides accessor methods to hash keys.
|
4
|
+
|
5
|
+
The purpose behind building HashAccessor was to be able to quickly add/modify/remove variables being stored in a serialized hash of a rails model. This is very useful if you have a large list of often changing variables on a model which don't get queried against. You can create new variables without messing up your DB. Storing account configurations or custom themes is a common scenario.
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
In your <code>Gemfile</code>:
|
10
|
+
|
11
|
+
gem "hash_accessor"
|
12
|
+
|
13
|
+
Then run:
|
14
|
+
|
15
|
+
bundle install
|
16
|
+
|
17
|
+
== Usage
|
18
|
+
|
19
|
+
You define each accessor you want to be stored inside the serialized hash
|
20
|
+
|
21
|
+
<code>hash_accessor _NAME_OF_HASH_, _NAME_OF_VARIABLE_, options</code>
|
22
|
+
|
23
|
+
Here is an example:
|
24
|
+
|
25
|
+
class MyModel < ActiveRecord::Base
|
26
|
+
serialize :options, Hash
|
27
|
+
|
28
|
+
hash_accessor :options, :currency, :default => "$USD"
|
29
|
+
hash_accessor :options, :display_currency_on_invoices, :type => :boolean, :default => true
|
30
|
+
hash_accessor :options, :invoice_due_date_net, :type => :integer, :default => 3
|
31
|
+
|
32
|
+
|
33
|
+
def some_method
|
34
|
+
|
35
|
+
self.currency = "$CAD"
|
36
|
+
|
37
|
+
display_currency_on_invoices?
|
38
|
+
|
39
|
+
...
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
=== Valid Options:
|
45
|
+
|
46
|
+
<code>:default</code> - if undefined, this plugin will create an empty instance of the defined type or null
|
47
|
+
|
48
|
+
<code>:type</code> - defaults to :string. Can also be :integer, :float, :boolean, or :array
|
49
|
+
|
50
|
+
==== For Arrays only:
|
51
|
+
<code>:collects</code> - only runs on arrays. Calls the lambda method on each item in the array before saving
|
52
|
+
|
53
|
+
<code>:reject_blanks</code> - removes all blank elements after the collect method
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
== Contributing & Reporting Issues
|
58
|
+
|
59
|
+
While I can't promise the most speedy response, I do plan to continue to maintain this gem. Please feel free to report any issues, provide feedback, or make suggestions on the {Issue Tracker}[http://github.com/forrest/hash_accessor/issues]
|
60
|
+
|
61
|
+
|
62
|
+
---
|
63
|
+
|
64
|
+
|
65
|
+
Copyright on Updates - Copyright (c) 2011 OctopusApp Inc. ({getjobber.com}[http://getjobber.com]), released under the MIT license
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path("../lib/hash_accessor", __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "hash_accessor"
|
5
|
+
s.version = HashAccessor::VERSION
|
6
|
+
s.author = ["Forrest"]
|
7
|
+
s.email = ["development@forrestzeisler.com"]
|
8
|
+
s.date = Time.now.utc.strftime("%Y-%m-%d")
|
9
|
+
|
10
|
+
|
11
|
+
s.description = 'This gem provides accessor methods to hash keys.'
|
12
|
+
s.summary = "This gem provides extended functionality to serialized hashed in rails. It allows you to define accessor methods for variable that rest "+
|
13
|
+
"inside a serialized hash. This is very useful if you have a large list of often changing DB variables on a model which don't get queried against."
|
14
|
+
|
15
|
+
s.required_rubygems_version = ">= 1.3.6"
|
16
|
+
s.platform = Gem::Platform::RUBY
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.require_path = "lib"
|
21
|
+
|
22
|
+
s.has_rdoc = true
|
23
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
24
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module HashAccessor
|
2
|
+
module ClassMethods
|
3
|
+
#valid options:
|
4
|
+
# :default - if undefined, this plugin will create an empty instance of the defined type or null
|
5
|
+
# :type - defaults to :string. Can also be :integer, :float, :boolean, or :array
|
6
|
+
#
|
7
|
+
# for arrays only:
|
8
|
+
# :collects - only runs on arrays. Calls the lambda method on each item in the array before saving
|
9
|
+
# :reject_blanks - removes all blank elements after the collect method
|
10
|
+
def hash_accessor(hash_name, method_name, options = {})
|
11
|
+
method_modifier = ""
|
12
|
+
begin
|
13
|
+
|
14
|
+
if options[:type]==:integer
|
15
|
+
method_modifier = "new_val = new_val.to_i"
|
16
|
+
elsif options[:type]==:float
|
17
|
+
method_modifier = "new_val = new_val.to_f"
|
18
|
+
elsif options[:type]==:boolean or options[:type]==:bool
|
19
|
+
method_modifier = "new_val = (new_val.is_a?(TrueClass) or (new_val.is_a?(String) and (new_val=~/true|1/i).present?) or (new_val.is_a?(Fixnum) and new_val==1))"
|
20
|
+
elsif options[:type]==:array
|
21
|
+
method_modifier = "new_val = [new_val] unless new_val.is_a?(Array)"
|
22
|
+
|
23
|
+
if options[:collects]
|
24
|
+
self.class_variable_set("@@lambda_for_#{method_name}", options[:collects])
|
25
|
+
method_modifier += "\nnew_val = new_val.collect(&@@lambda_for_#{method_name})"
|
26
|
+
end
|
27
|
+
|
28
|
+
if options[:reject_blanks]
|
29
|
+
method_modifier += "\nnew_val = new_val.reject(&:blank?)"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
if options[:default].is_a?(String)
|
36
|
+
default = "\"#{options[:default]}\""
|
37
|
+
elsif options[:default].nil? and options[:type]==:array
|
38
|
+
default = "[]"
|
39
|
+
elsif options[:default].nil?
|
40
|
+
default = "nil"
|
41
|
+
else
|
42
|
+
default = options[:default]
|
43
|
+
end
|
44
|
+
|
45
|
+
str = <<-EOS
|
46
|
+
def #{method_name}
|
47
|
+
self.#{hash_name} ||= {}
|
48
|
+
|
49
|
+
if self.#{hash_name}[:#{method_name}].nil?
|
50
|
+
self.#{hash_name}[:#{method_name}] = #{default}
|
51
|
+
end
|
52
|
+
|
53
|
+
self.#{hash_name}[:#{method_name}]
|
54
|
+
end
|
55
|
+
|
56
|
+
def #{method_name}=(new_val)
|
57
|
+
self.#{hash_name} ||= {}
|
58
|
+
#{method_modifier}
|
59
|
+
|
60
|
+
if self.#{hash_name}[:#{method_name}] != new_val
|
61
|
+
@#{method_name}_changed = true
|
62
|
+
end
|
63
|
+
|
64
|
+
self.#{hash_name}[:#{method_name}] = new_val
|
65
|
+
end
|
66
|
+
|
67
|
+
def #{method_name}_changed?
|
68
|
+
!!@#{method_name}_changed
|
69
|
+
end
|
70
|
+
EOS
|
71
|
+
|
72
|
+
if options[:type]==:boolean or options[:type]==:bool
|
73
|
+
str += <<-EOS
|
74
|
+
def #{method_name}?
|
75
|
+
!!self.#{method_name}
|
76
|
+
end
|
77
|
+
EOS
|
78
|
+
end
|
79
|
+
|
80
|
+
class_eval str
|
81
|
+
rescue Exception => e
|
82
|
+
puts "\n\nError adding hash_accessor:\n#{e.message}\n#{e.backtrace}\n\n"
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'railtie' if defined?(Rails)
|
5
|
+
|
6
|
+
module HashAccessor
|
7
|
+
VERSION = "0.0.1"
|
8
|
+
|
9
|
+
autoload :ClassMethods, 'class_methods'
|
10
|
+
|
11
|
+
def self.included(mod)
|
12
|
+
mod.extend(ClassMethods)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
data/lib/railtie.rb
ADDED
data/rails/init.rb
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_accessor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Forrest
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-16 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: This gem provides accessor methods to hash keys.
|
15
|
+
email:
|
16
|
+
- development@forrestzeisler.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files:
|
20
|
+
- README.rdoc
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- MIT-LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
- hash_accessor.gemspec
|
26
|
+
- lib/class_methods.rb
|
27
|
+
- lib/hash_accessor.rb
|
28
|
+
- lib/railtie.rb
|
29
|
+
- rails/init.rb
|
30
|
+
homepage:
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.3.6
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.10
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: This gem provides extended functionality to serialized hashed in rails. It
|
54
|
+
allows you to define accessor methods for variable that rest inside a serialized
|
55
|
+
hash. This is very useful if you have a large list of often changing DB variables
|
56
|
+
on a model which don't get queried against.
|
57
|
+
test_files: []
|