matterful_attributes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ab5c548e45b69263103845a1625c849bd03ce30a
4
+ data.tar.gz: 20b3d880f2911cd653f321d5ea0747e0b0bccc64
5
+ SHA512:
6
+ metadata.gz: 637c48c60453952b52c801563f1ac45926aac456d0d88b5c1bcb2dd1b30f5d4e31a596e93e09b5397ee593583f646462e065cbb84c419bee4a3897e4ab9188e8
7
+ data.tar.gz: e2095a1958f80956e6d3d8f608ece71c36dbb70e0b859def9de1bfdf5f05fc161c3c033573e3928e7cda57249dae86d9cf43db47be437f0b5892a8358cd65818
data/.gitignore ADDED
@@ -0,0 +1,32 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.sassc
19
+ .sass-cache
20
+ capybara-*.html
21
+ .rspec
22
+ /.bundle
23
+ /vendor/bundle
24
+ /log/*
25
+ /tmp/*
26
+ /db/*.sqlite3
27
+ /public/system/*
28
+ /coverage/
29
+ /spec/tmp/*
30
+ **.orig
31
+ rerun.txt
32
+ pickle-email-*.html
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in matterful_attributes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nick Gorbikoff
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,27 @@
1
+ matterful_attributes
2
+ ====================
3
+
4
+ Ruby / Rails gem that shims ActiveRecord::Base to provide some helpful methods for parsing out attributes that matter to humans, i.e.
5
+ ``` Address.first.matterful_attributes ```
6
+ will return a hash of attributes minus
7
+ <b> id, type, polymorphic_id, polymorphic_type, cretated_at, updated_at</b> and can also skip all foreign_keys, like <b>category_id or status_id </b>.
8
+
9
+ This is really useful when you need to be able to import, compare and update information on a record from an external source like a CSV file or a legacy DB. In that context only attributes specific to the model matter. In the world outside of rails, such as some old legacy DB these Rails specific attributes don't mean much. And I got tired of writing things like
10
+ ```ruby
11
+ Address.first.attributes.except('id','created_at','updated_at').diff(OldStyleAddress.first.attributes)
12
+ ```
13
+ It's much cleaner to right it this way:
14
+ ```ruby
15
+ Address.first.matterful_diff(OldStyleAddress.first)
16
+ ```
17
+ don't you think?
18
+
19
+
20
+ Production
21
+ ==========
22
+ This was extracted from production code that's tested. However use at your own risk. NO warranties or guarantees provided!
23
+
24
+
25
+ Tests
26
+ =====
27
+ This gem needs tests! It's tested in the context of my production code, but no gem specific tests. I'll add them later if time allows. Really bad practice, I know, but I'm really short on time.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module MatterfulAttributes
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,62 @@
1
+ require "matterful_attributes/version"
2
+
3
+ module MatterfulAttributes
4
+ module ActiveRecord
5
+ class Base
6
+
7
+ def matterful_attributes(options={})
8
+ options = options.dup
9
+ default = options[:default].nil? ? true : options[:default]
10
+ foreign_key = options[:foreign_key].nil? ? true : options[:foreign_key]
11
+ sti = options[:sti].nil? ? true : options[:sti]
12
+ polymorphic = options[:polymorphic].nil? ? true : options[:polymorphic]
13
+ # extra keys supplied as array of strings to ignore in comparison
14
+ attributes_to_ignore = options[:extra].nil? ? [] : options[:extra]
15
+
16
+ # Let's check for and add typical attributes that sti & polymorphic models have, override this by sti: false, polymorphic: false, :foreign_key: false
17
+ # by default when looking to compare two objects Customer.find(1).shipping_address.same_as? Address.new(city: 'Chicago')
18
+ # we really only want to see if any of the 'matterfule information changed', like address_line_1 or city.
19
+ # TODO: I guess I could do some smart checking on the model to see what kind of attributes it has. For now we'll just check for all that we want to remove
20
+ if default
21
+ attributes_to_ignore += ['id', 'created_at', 'updated_at']
22
+ end
23
+ if foreign_key
24
+ attributes_to_ignore += attributes.keys.keep_if{|k| k.match(/_id\z/) && !k.match(/able_id\z/) } # This will skipp polymorphic style foreign keys
25
+ end
26
+ if sti
27
+ attributes_to_ignore += ['type']
28
+ end
29
+ if polymorphic
30
+ attributes_to_ignore += attributes.keys.keep_if{|k| k.match(/able_id\z/) }
31
+ attributes_to_ignore += attributes.keys.keep_if{|k| k.match(/able_type\z/) }
32
+ end
33
+ attributes.except(*attributes_to_ignore)
34
+ end
35
+
36
+ def same_as?(source,options={})
37
+ matterful_attributes(options) == source.matterful_attributes(options)
38
+ end
39
+
40
+ def matterful_diff(source,options={})
41
+ matterful_attributes(options).diff(source.matterful_attributes(options))
42
+ end
43
+
44
+ def matterful_update(source,options={})
45
+ # Update self if source object has new data but DO NOT save. Let's say you want to do futer processing on the new data. Validation, upcasing, concatination
46
+ if !(target_attributes = source.matterful_diff(self, options={})).empty? # Pay attention to this! Reversing comparison to get expected results
47
+ target_attributes.each_pair do |k,v|
48
+ self[k] = v
49
+ end
50
+ end
51
+ self
52
+ end
53
+
54
+ def matterful_update!(source,options={})
55
+ # This will update self and save self if valid. Will return true or false.
56
+ self.matterful_update(source,options={})
57
+ save
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'matterful_attributes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "matterful_attributes"
8
+ spec.version = MatterfulAttributes::VERSION
9
+ spec.authors = ["Nick Gorbikoff"]
10
+ spec.email = ["nick.gorbikoff@gmail.com"]
11
+ spec.description = %q{Ruby / Rails gem that shims ActiveRecord::Base to provide some helpful methods for parsing out attributes that matter to humans, i.e. Address.first.matterful_attributes will return a hash of attributes minus id, type, polymorphic_id, polymorphic_type, cretated_at, updated_at and can also skip all foreign_keys, like category_id or status_id. }
12
+ spec.summary = %q{See Readme.md}
13
+ spec.homepage = "https://github.com/konung/matterful_attributes"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: matterful_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nick Gorbikoff
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: 'Ruby / Rails gem that shims ActiveRecord::Base to provide some helpful
42
+ methods for parsing out attributes that matter to humans, i.e. Address.first.matterful_attributes
43
+ will return a hash of attributes minus id, type, polymorphic_id, polymorphic_type,
44
+ cretated_at, updated_at and can also skip all foreign_keys, like category_id or
45
+ status_id. '
46
+ email:
47
+ - nick.gorbikoff@gmail.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - .gitignore
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - lib/matterful_attributes.rb
58
+ - lib/matterful_attributes/version.rb
59
+ - matterful_attributes.gemspec
60
+ homepage: https://github.com/konung/matterful_attributes
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.0.3
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: See Readme.md
84
+ test_files: []
85
+ has_rdoc: