bangers_and_hash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jeff Felchner
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ bangers_and_hash
2
+ =======
3
+
4
+ Easy Accessors for ActiveModel Objects
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ require 'bangers_and_hash/version'
2
+ require 'bangers_and_hash/attribute_hash_accessors'
3
+
4
+ module BangersAndHash
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,3 @@
1
+ module BangersAndHash
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,59 @@
1
+ module AttributeHashAccessors
2
+ module ClassMethods
3
+ def hash_attr_accessors(*attrs)
4
+ attrs.each do |method|
5
+ method_name = method.to_s
6
+
7
+ define_method(method.to_sym) do
8
+ @attributes[method_name]
9
+ end
10
+
11
+ define_method("#{method_name}=".to_sym) do |value|
12
+ send("#{method_name}_will_change!") unless @attributes[method_name] == value
13
+ @attributes[method_name] = value
14
+ end
15
+ end
16
+
17
+ define_method(:attributes) do
18
+ ActiveSupport::HashWithIndifferentAccess.new.tap do |hash|
19
+ attrs.each do |attr|
20
+ attr_name = attr.to_s
21
+
22
+ hash[attr_name] = @attributes[attr_name]
23
+ end
24
+ end
25
+ end
26
+
27
+ self.class_eval do
28
+ define_attribute_methods attrs
29
+ end
30
+ end
31
+ end
32
+
33
+ def self.included(base)
34
+ base.send(:include, ActiveModel::AttributeMethods)
35
+ base.send(:include, ActiveModel::Dirty)
36
+
37
+ base.extend ClassMethods
38
+ end
39
+
40
+ def initialize(attrs = {})
41
+ @attributes = ActiveSupport::HashWithIndifferentAccess.new
42
+ self.attributes = attrs
43
+ end
44
+
45
+ ###
46
+ # TODO: Raise error if an attrubute passed in does not exist in the list
47
+ # of acceptable attributes
48
+ #
49
+ def attributes=(attrs = {})
50
+ attrs = attrs.with_indifferent_access
51
+ safe_attributes = attrs.slice *attributes.keys
52
+
53
+ @attributes.merge! safe_attributes
54
+ end
55
+
56
+ def attributes
57
+ ActiveSupport::HashWithIndifferentAccess.new
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bangers_and_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - jfelchner
9
+ - m5rk
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-10-02 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: ''
16
+ email: support@chirrpy.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files:
20
+ - README.md
21
+ - LICENSE
22
+ files:
23
+ - lib/bangers_and_hash/version.rb
24
+ - lib/bangers_and_hash.rb
25
+ - lib/hashish/attribute_hash_accessors.rb
26
+ - Rakefile
27
+ - README.md
28
+ - LICENSE
29
+ homepage: https://github.com/chirrpy/bangers_and_hash
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --charset = UTF-8
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: '0'
48
+ requirements: []
49
+ rubyforge_project: bangers_and_hash
50
+ rubygems_version: 1.8.24
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Easy Accessors for ActiveModel Objects
54
+ test_files: []