tracks-attributes 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +60 -0
- data/Rakefile +29 -0
- data/lib/tasks/tracks_attributes_tasks.rake +4 -0
- data/lib/tracks_attributes/railtie.rb +10 -0
- data/lib/tracks_attributes/version.rb +3 -0
- data/lib/tracks_attributes.rb +127 -0
- metadata +87 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 YOURNAME
|
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.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
|
2
|
+
[![Build Status](https://travis-ci.org/leopoldodonnell/tracks-attributes.png?branch=master)](https://travis-ci.org/leopoldodonnell/tracks-attributes)
|
3
|
+
[![Dependency Status](https://gemnasium.com/leopoldodonnell/tracks-attributes.png)](https://gemnasium.com/leopoldodonnell/tracks-attributes)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/leopoldodonnell/tracks-attributes.png)](https://codeclimate.com/github/leopoldodonnell/tracks-attributes)
|
5
|
+
# TracksAttributes
|
6
|
+
|
7
|
+
TracksAttributes adds the ability to track ActiveRecord and Object level attributes.
|
8
|
+
|
9
|
+
Sometimes you just need to know what your accessors are at runtime, like when you're writing a controller that
|
10
|
+
needs to return JSON or XML. This module extends ActiveRecord::Base with the *tracks_attributes* class method. Once this has
|
11
|
+
been called the class is extended with the ability to track attributes through *attr_accessor*, *attr_reader*, and *attr_writer*.
|
12
|
+
Plain old Ruby classes may also use *TracksAttributes* by including it as a module first.
|
13
|
+
|
14
|
+
## Enhanced JSON and XML processing
|
15
|
+
|
16
|
+
Beyond the ability to track your attributes, this gem simplifies your use of converting your objects to an from JSON or XML.
|
17
|
+
Once a class has been extended, it can convert to and from JSON or XML without having to explicitly include attributes.
|
18
|
+
|
19
|
+
Example:
|
20
|
+
```ruby
|
21
|
+
class Person < ActiveRecordBase
|
22
|
+
tracks_attributes
|
23
|
+
|
24
|
+
attr_accessible :name, :email
|
25
|
+
attr_accessor :favorite_food
|
26
|
+
end
|
27
|
+
|
28
|
+
fred = Person.find_by_name("Fred")
|
29
|
+
fred.favorite_food = 'Brontosaurus Burgers'
|
30
|
+
|
31
|
+
fred_json = fred.to_json
|
32
|
+
puts fred_json
|
33
|
+
# => {"id":1,"name":"Fred","email":"fred@bedrock.com","favorite_food":"Brontosaurus Burgers"}
|
34
|
+
|
35
|
+
fred2 = Person.new
|
36
|
+
fred2.from_json(fred_json)
|
37
|
+
puts "#{fred2.name} loves #{fred2.favorite_food}"
|
38
|
+
# => Fred loves Brontosaurus Burgers
|
39
|
+
```
|
40
|
+
Both the JSON and XML take the same options as their Hash and ActiveRecord counterparts so you can still
|
41
|
+
use *:only* and *:includes* in your code as needed.
|
42
|
+
|
43
|
+
## Add Validations To Non Active Record Attributes
|
44
|
+
|
45
|
+
To add ActiveModel::Validations to your class just initialize your class with *tracks_attributes* as
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
tracks_attributes :validates => true
|
49
|
+
```
|
50
|
+
## Installation
|
51
|
+
|
52
|
+
Add the following to your Gemfile
|
53
|
+
|
54
|
+
gem 'tracks-attributes', :git => "git://github.com/leopoldodonnell/tracks-attributes"
|
55
|
+
|
56
|
+
Then call bundle to install it.
|
57
|
+
|
58
|
+
> bundle
|
59
|
+
|
60
|
+
This project rocks and uses MIT-LICENSE.
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
begin
|
9
|
+
require 'rdoc/task'
|
10
|
+
rescue LoadError
|
11
|
+
require 'rdoc/rdoc'
|
12
|
+
require 'rake/rdoctask'
|
13
|
+
RDoc::Task = Rake::RDocTask
|
14
|
+
end
|
15
|
+
|
16
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'TracksAttributes'
|
19
|
+
rdoc.options << '--line-numbers'
|
20
|
+
rdoc.rdoc_files.include('README.rdoc')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
23
|
+
|
24
|
+
Bundler::GemHelper.install_tasks
|
25
|
+
|
26
|
+
desc "Run all specs"
|
27
|
+
RSpec::Core::RakeTask.new(:spec)
|
28
|
+
|
29
|
+
task :default => :spec
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# @author Leo O'Donnell
|
2
|
+
|
3
|
+
##
|
4
|
+
# A Module that can be used to extend ActiveRecord or Plain Old Ruby Objects
|
5
|
+
# with the ability to track all attributes. It also simplifies streaming to and
|
6
|
+
# from JSON or XML since it will automatically consider all attributes in the
|
7
|
+
# conversion.
|
8
|
+
#
|
9
|
+
# This module can also be used as building block for other classes that need to
|
10
|
+
# be dynamically aware of their attributes.
|
11
|
+
#
|
12
|
+
module TracksAttributes
|
13
|
+
extend ActiveSupport::Concern
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
##
|
17
|
+
# Call this class method to begin tracking attributes on a class.
|
18
|
+
#
|
19
|
+
# @param [Hash] options With values:
|
20
|
+
# * :validates => true will enable attribute validation
|
21
|
+
#
|
22
|
+
# Note:
|
23
|
+
# Classes that include TracksAttributes will not be extended unless
|
24
|
+
# until this method is called.
|
25
|
+
#
|
26
|
+
# @see TracksAttributesInternal TracksAttributesInternal for full method list
|
27
|
+
def tracks_attributes(options={})
|
28
|
+
include TracksAttributesInternal
|
29
|
+
include ActiveModel::Validations if options[:validates]
|
30
|
+
self
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module TracksAttributesInternal
|
35
|
+
extend ActiveSupport::Concern
|
36
|
+
|
37
|
+
included do
|
38
|
+
@tracked_attrs ||= []
|
39
|
+
end
|
40
|
+
|
41
|
+
module ClassMethods
|
42
|
+
# override attr_accessor to track accessors for an TracksAttributes
|
43
|
+
def attr_accessor(*vars)
|
44
|
+
@tracked_attrs.concat vars
|
45
|
+
super
|
46
|
+
end
|
47
|
+
|
48
|
+
# override attr_reader to track accessors for an TracksAttributes
|
49
|
+
def attr_reader(*vars)
|
50
|
+
@tracked_attrs.concat vars
|
51
|
+
super
|
52
|
+
end
|
53
|
+
|
54
|
+
# override attr_writer to track accessors for an TracksAttributes
|
55
|
+
def attr_writer(*vars)
|
56
|
+
# avoid tracking attributes that are added by the class_attribute
|
57
|
+
# as these are class attributes and not instance attributes.
|
58
|
+
@tracked_attrs.concat vars.reject {|var| respond_to? var }
|
59
|
+
super
|
60
|
+
end
|
61
|
+
|
62
|
+
# return an array of all of the attributes that are not in active record
|
63
|
+
def accessors
|
64
|
+
@tracked_attrs ||= []
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
# Return the array of accessor symbols for instances of this
|
70
|
+
# class.
|
71
|
+
def accessors
|
72
|
+
self.class.accessors
|
73
|
+
end
|
74
|
+
|
75
|
+
# Return a hash all of the accessor symbols and their values
|
76
|
+
def all_attributes
|
77
|
+
attributes.merge Hash[accessors.collect {|v| [v, send(v.to_s)] if respond_to? "#{v}".to_sym}]
|
78
|
+
end
|
79
|
+
|
80
|
+
# Set all attributes with hash of symbols and their values and returns instance
|
81
|
+
def all_attributes=(hash = {})
|
82
|
+
hash.each {|k, v| send("#{k.to_s}=", v) if respond_to? "#{k.to_s}=".to_sym}
|
83
|
+
self
|
84
|
+
end
|
85
|
+
|
86
|
+
# Convert an TracksAttributes instance to JSON by delegating conversion to Hash.to_json
|
87
|
+
#
|
88
|
+
# @param [Hash] options
|
89
|
+
# * Without any options, the returned JSON string will include all of the attributes.
|
90
|
+
# * :only => one, or an array of Hash Keys that define which keys to process
|
91
|
+
# * :except => one, or an array of Hash Keys that define which keys not to process
|
92
|
+
#
|
93
|
+
def to_json(options = nil)
|
94
|
+
all_attributes.to_json(options)
|
95
|
+
end
|
96
|
+
|
97
|
+
# Returns an instance of TracksAttributes from a JSON string
|
98
|
+
def from_json(json, include_root=false)
|
99
|
+
hash = ActiveSupport::JSON.decode(json)
|
100
|
+
hash = hash.values.first if include_root
|
101
|
+
self.all_attributes = hash
|
102
|
+
end
|
103
|
+
|
104
|
+
# Convert an TracksAttributes instance to XML by delegating conversion to Hash.to_xml
|
105
|
+
#
|
106
|
+
# @param [Hash] options
|
107
|
+
# * Without any options, the returned XML string will include all of the attributes.
|
108
|
+
# * :only => one, or an array of Hash Keys that define which keys to process
|
109
|
+
# * :except => one, or an array of Hash Keys that define which keys not to process
|
110
|
+
#
|
111
|
+
# The :builder option uses key as the :root for the XML
|
112
|
+
# result
|
113
|
+
#
|
114
|
+
def to_xml(options = nil)
|
115
|
+
all_attributes.to_xml(options || {})
|
116
|
+
end
|
117
|
+
|
118
|
+
# Returns an instance of TracksAttributes from an XML string
|
119
|
+
def from_xml(xml)
|
120
|
+
hash = Hash.from_xml(xml).values.first
|
121
|
+
self.all_attributes = hash
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
require 'tracks_attributes/railtie'
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tracks-attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Leo O'Donnell
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.2'
|
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: '3.2'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sqlite3
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: ! "Sometimes you just need to know what your accessors are at runtime,
|
47
|
+
like when you're writing a controller that\n needs to return JSON or XML..."
|
48
|
+
email:
|
49
|
+
- leopold.odonnell@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- lib/tasks/tracks_attributes_tasks.rake
|
55
|
+
- lib/tracks_attributes/railtie.rb
|
56
|
+
- lib/tracks_attributes/version.rb
|
57
|
+
- lib/tracks_attributes.rb
|
58
|
+
- MIT-LICENSE
|
59
|
+
- Rakefile
|
60
|
+
- README.md
|
61
|
+
homepage: https://github.com/leopoldodonnell/tracks-attributes
|
62
|
+
licenses: []
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.8.24
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: TracksAttributes adds the ability to track ActiveRecord and Object level
|
85
|
+
attributes.
|
86
|
+
test_files: []
|
87
|
+
has_rdoc:
|