activerecord-relations_annotations 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ca4b86b9a7472d5974ae27ede0a4d5f17754d96
4
+ data.tar.gz: 6749de01c00c1d87c0a5d5f991a89b89e1746c07
5
+ SHA512:
6
+ metadata.gz: c823c5ac2531808e582e9f5fe2d269bd9755a029ad85d2f870ed5f8004fe0b1fd9b49dd3f3eee722e1820e856af9059a9993a73928189399d046c50a2371b967
7
+ data.tar.gz: d50858a412b93831b6cdd52c3b3880cb3ea6ed6fe985875db70f0243876db4d6ff6c6ba78722ba743f8fe1592d1653557c94aeab42b2d3dbe543a3a9b2e603f8
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-relations_annotations.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Fire-Dragon-DoL
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.
@@ -0,0 +1,130 @@
1
+ # RelationsAnnotations
2
+
3
+ Annotate ActiveRecord relations objects with custom data, allowing metadata
4
+ on relations that can be used to customize code.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'activerecord-relations_annotations', '~> 0.0.1'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install activerecord-relations_annotations
21
+
22
+ ## Usage
23
+
24
+ Not the greatest example, but gives you a rough idea:
25
+
26
+ ```ruby
27
+ class User < ActiveRecord::base
28
+ include ActiveRecord::RelationsAnnotations
29
+ end
30
+
31
+ users = User.where.not(email: nil)
32
+ with_email_users = users.annotate(:with_email? => true)
33
+ without_email_users = users.annotate(:with_email? => false)
34
+
35
+ if with_email_users.annotations.with_email?
36
+ # Do some stuff
37
+ end
38
+
39
+ unless without_email_users.annotations.with_email?
40
+ # Do other stuff on this group of users
41
+ end
42
+ ```
43
+
44
+ Annotations can also be accessed in a hash-fashion, like on `OpenStruct`:
45
+
46
+ ```ruby
47
+ users = User.where(email: 'foo').annotate(foo: 'bar')
48
+
49
+ users.annotations[:foo] # => 'bar'
50
+ users.annotations[:something] # => nil
51
+ ```
52
+
53
+ ## Documentation
54
+
55
+ `#annotate(**values)` accepts a hash, values are merged into the **annotations**
56
+ `OpenStruct`, it returns a new `ActiveRecord::Relation` based on the one is
57
+ called on.
58
+ If it's called as the first method (directly on model class), it behaves like
59
+ it's called on `all` method.
60
+
61
+ `annotations` method returns an `OpenStruct` with all annotations set on the
62
+ relation.
63
+
64
+ `annotated` method should not be used, basically ensures we are in an
65
+ `ActiveRecord::Relation` and ensures the annotations `OpenStruct` exists, so
66
+ even in case it's called, it won't do any damage.
67
+
68
+ ## Features
69
+
70
+ ### Chainable
71
+
72
+ ```ruby
73
+ User.where.not(email: 'foo').
74
+ annotate(testme: 123, blah: 'hi').
75
+ annotate(foo: 'bar').
76
+ where(something: 'else').
77
+ annotations
78
+ # => <OpenStruct testme=123, blah='hi', something='else'>
79
+ ```
80
+
81
+ ### Always returns an OpenStruct
82
+
83
+ ```ruby
84
+ User.annotations
85
+ # => <OpenStruct>
86
+ ```
87
+
88
+ ```ruby
89
+ User.where.not(email: 'foo').annotations
90
+ # => <OpenStruct>
91
+ ```
92
+
93
+ ### It always creates a relation
94
+
95
+ ```ruby
96
+ User.annotate(foo: 'bar').class
97
+ # => User::ActiveRecord_Relation
98
+ ```
99
+
100
+ ## Gotchas
101
+
102
+ When you call methods on relations, they create new values, much like `map` for
103
+ array, which creates a new array.
104
+ This means the following:
105
+
106
+ ```ruby
107
+ tmp = User.where.name(email: 'foo')
108
+
109
+ tmp2 = tmp.annotate(bar: 'baz')
110
+
111
+ tmp.annotations # => <OpenStruct>
112
+ tmp2.annotations # => <OpenStruct bar='baz'>
113
+ ```
114
+
115
+ It shouldn't be a problem, though, because it's standard
116
+ `ActiveRecord::Relation` behavior.
117
+
118
+ ## How it works
119
+ Functionality is simple, the only _hack_ is that when `annotate`, `annotated`
120
+ and `annotations` are called, a instance variable is created (if missing),
121
+ called `@_annotations` and it's used to read values, through
122
+ `instance_variable_set` and `instance_variable_get`.
123
+
124
+ ## Contributing
125
+
126
+ 1. Fork it ( https://github.com/Fire-Dragon-DoL/activerecord-relations_annotations/fork )
127
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
128
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
129
+ 4. Push to the branch (`git push origin my-new-feature`)
130
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/relations_annotations/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord-relations_annotations"
8
+ spec.version = ActiveRecord::RelationsAnnotations::VERSION
9
+ spec.authors = ["Fire-Dragon-DoL"]
10
+ spec.email = ["francesco.belladonna@gmail.com"]
11
+ spec.summary = %q{Annotate ActiveRecord relations objects with custom data}
12
+ spec.description = %q{Annotate ActiveRecord relations objects with custom data, allowing metadata on relations that can be used to customize code}
13
+ spec.homepage = "https://github.com/Fire-Dragon-DoL/activerecord-relations_annotations"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.1.0"
24
+ spec.add_development_dependency "sqlite3", "~> 1.3"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "pry-remote"
27
+ spec.add_development_dependency "pry-byebug"
28
+
29
+ spec.add_dependency "activesupport", "~> 4.0"
30
+ spec.add_dependency "activerecord", "~> 4.0"
31
+ end
@@ -0,0 +1,37 @@
1
+ require 'ostruct'
2
+ require 'active_record'
3
+ require 'active_support/concern'
4
+ require 'active_record/relations_annotations/version'
5
+
6
+ module ActiveRecord
7
+ module RelationsAnnotations
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+
12
+ def annotate(**values)
13
+ annotated.tap do |relation|
14
+ values.each do |key, value|
15
+ relation.instance_variable_get(:@_annotations)[key.to_sym] = value
16
+ end
17
+ end
18
+ end
19
+
20
+ def annotations
21
+ annotated.instance_variable_get(:@_annotations)
22
+ end
23
+
24
+ # This should never be explicitly called, but if done, nothing should
25
+ # happen
26
+ def annotated
27
+ all.tap do |relation|
28
+ if relation.instance_variable_get(:@_annotations).nil?
29
+ relation.instance_variable_set(:@_annotations, OpenStruct.new)
30
+ end
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module RelationsAnnotations
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require 'active_record/relations_annotations'
3
+ require 'support/dummy'
4
+
5
+ RSpec.describe ActiveRecord::RelationsAnnotations do
6
+ subject { Dummy.where(name: nil) }
7
+
8
+ it "creates dummy correctly" do
9
+ expect{Dummy.create}.not_to raise_error
10
+ end
11
+
12
+ describe "#annotated" do
13
+
14
+ it "is always a relation" do
15
+ expect(subject.annotated.class).to be < ActiveRecord::Relation
16
+ end
17
+
18
+ end
19
+
20
+ describe "#annotate" do
21
+
22
+ it "can annotate fetched relation" do
23
+ expect{subject.annotate(foo: 'bar')}.not_to raise_error
24
+ end
25
+
26
+ it "is chainable" do
27
+ expect{subject.annotate(foo: 'bar').where(name: 'baz')}.not_to raise_error
28
+ end
29
+
30
+ it "keeps annotation when adding conditions to query" do
31
+ expect(
32
+ subject.annotate(foo: 'bar').where(name: 'baz').annotations.foo
33
+ ).to eq 'bar'
34
+ end
35
+
36
+ it "is always a relation" do
37
+ expect(subject.annotate(foo: 'bar').class).to be < ActiveRecord::Relation
38
+ end
39
+
40
+ end
41
+
42
+ describe "#annotations" do
43
+
44
+ it "contains annotated values" do
45
+ expect(
46
+ subject.annotate(foo: 'bar').annotations.foo
47
+ ).to eq 'bar'
48
+ end
49
+
50
+ it "has nil values when annotation not present" do
51
+ expect(subject.annotations[:foo]).to be_nil
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,19 @@
1
+ $LOAD_PATH << File.expand_path('..', __FILE__).to_s
2
+
3
+ RSpec.configure do |config|
4
+ config.expect_with :rspec do |expectations|
5
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
6
+ end
7
+
8
+ config.mock_with :rspec do |mocks|
9
+ mocks.verify_partial_doubles = true
10
+ end
11
+
12
+ config.filter_run :focus
13
+ config.run_all_when_everything_filtered = true
14
+ config.disable_monkey_patching!
15
+ # config.warnings = true
16
+ config.default_formatter = 'doc' if config.files_to_run.one?
17
+ config.order = :random
18
+ Kernel.srand config.seed
19
+ end
@@ -0,0 +1,18 @@
1
+ require 'active_record'
2
+ require 'sqlite3'
3
+
4
+ # ActiveRecord::Base.logger = Logger.new(STDERR)
5
+ ActiveRecord::Base.establish_connection(
6
+ adapter: 'sqlite3',
7
+ database: ':memory:'
8
+ )
9
+
10
+ ActiveRecord::Schema.verbose = false
11
+
12
+ ActiveRecord::Schema.define do
13
+
14
+ create_table :dummies, force: true do |t|
15
+ t.column :name, :string
16
+ end
17
+
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_record'
2
+ require 'active_record/relations_annotations'
3
+ require 'support/config/active_record'
4
+
5
+ class Dummy < ActiveRecord::Base
6
+ include ActiveRecord::RelationsAnnotations
7
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-relations_annotations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fire-Dragon-DoL
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-18 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-remote
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activesupport
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '4.0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activerecord
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '4.0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '4.0'
139
+ description: Annotate ActiveRecord relations objects with custom data, allowing metadata
140
+ on relations that can be used to customize code
141
+ email:
142
+ - francesco.belladonna@gmail.com
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - Gemfile
150
+ - LICENSE.txt
151
+ - README.md
152
+ - Rakefile
153
+ - activerecord-relations_annotations.gemspec
154
+ - lib/active_record/relations_annotations.rb
155
+ - lib/active_record/relations_annotations/version.rb
156
+ - spec/active_record/relations_annotations_spec.rb
157
+ - spec/spec_helper.rb
158
+ - spec/support/config/active_record.rb
159
+ - spec/support/dummy.rb
160
+ homepage: https://github.com/Fire-Dragon-DoL/activerecord-relations_annotations
161
+ licenses:
162
+ - MIT
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.4.5
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Annotate ActiveRecord relations objects with custom data
184
+ test_files:
185
+ - spec/active_record/relations_annotations_spec.rb
186
+ - spec/spec_helper.rb
187
+ - spec/support/config/active_record.rb
188
+ - spec/support/dummy.rb