activerecord-utils 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +62 -0
- data/lib/activerecord/utils.rb +102 -2
- data/lib/activerecord/utils/version.rb +1 -1
- metadata +6 -6
data/README.md
CHANGED
@@ -2,3 +2,65 @@
|
|
2
2
|
|
3
3
|
activerecord-utils gem - utilities (e.g. random, alias_attr, etc.) for activerecord in ruby
|
4
4
|
|
5
|
+
* home :: [github.com/rubylibs/activerecord-utils](https://github.com/rubylibs/activerecord-utils)
|
6
|
+
* bugs :: [github.com/rubylibs/activerecord-utils/issues](https://github.com/rubylibs/activerecord-utils/issues)
|
7
|
+
* gem :: [rubygems.org/gems/activerecord-utils](https://rubygems.org/gems/activerecord-utils)
|
8
|
+
* rdoc :: [rubydoc.info/gems/activerecord-utils](http://rubydoc.info/gems/activerecord-utils)
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
### `alias_attr` macro
|
13
|
+
|
14
|
+
|
15
|
+
alias_attr :plato, :og # new, old
|
16
|
+
|
17
|
+
generates
|
18
|
+
|
19
|
+
def plato # reader
|
20
|
+
send( :og ) # e.g. return og
|
21
|
+
end
|
22
|
+
|
23
|
+
def plato=(value) # writer
|
24
|
+
send( :'og=', value ) # e.g. return self.og=value
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
### `attr_reader_w_fallback` macro
|
29
|
+
|
30
|
+
attr_reader_w_fallbacks :published, :touched
|
31
|
+
|
32
|
+
generates
|
33
|
+
|
34
|
+
def published
|
35
|
+
read_attribute_w_fallbacks( :published, :touched )
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
### 'rnd' finder
|
40
|
+
|
41
|
+
e.g. find random record e.g.
|
42
|
+
|
43
|
+
|
44
|
+
beer_of_the_week = Beer.rnd
|
45
|
+
|
46
|
+
equals
|
47
|
+
|
48
|
+
beer_of_the_week = Beer.offset( rand( Beer.count) ).limit(1).first
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
## Alternatives
|
53
|
+
|
54
|
+
### Random
|
55
|
+
|
56
|
+
- [`random_record` gem](http://rubygems.org/gems/random_record) - [(Source)](https://github.com/rahult/random_record) returns a random record for Ruby Models using ActiveRecord
|
57
|
+
- [`activerecord_random` gem](http://rubygems.org/gems/activerecord_random) - [(Source)](https://github.com/GnomesLab/activerecord_random) empowers your ActiveRecord Models with the ability to return a random record without using SQL RAND()
|
58
|
+
- [`randomizr` gem](http://rubygems.org/gems/randomizr) - Returns one random Active Record object using cross-platform ANSI compliant SQL
|
59
|
+
- [`fast_random` gem](http://rubygems.org/gems/fast_random) - [(Source)](https://github.com/xdite/fast_random) ultra fast order by rand() solution
|
60
|
+
- [`randumb`](https://github.com/spilliton/randumb) - adds ability to pull back random records from Active Record
|
61
|
+
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
The scripts are dedicated to the public domain.
|
66
|
+
Use it as you please with no restrictions whatsoever.
|
data/lib/activerecord/utils.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
|
2
2
|
|
3
|
-
require 'activerecord/utils/version' # let
|
3
|
+
require 'activerecord/utils/version' # let version always go first
|
4
4
|
|
5
5
|
|
6
6
|
module ActiveRecord
|
7
|
+
|
7
8
|
module Utils
|
8
9
|
|
9
10
|
def self.banner
|
@@ -15,10 +16,109 @@ module Utils
|
|
15
16
|
"#{File.expand_path( File.dirname(File.dirname(__FILE__)) )}"
|
16
17
|
end
|
17
18
|
=end
|
18
|
-
|
19
19
|
end # module Utils
|
20
|
+
|
20
21
|
end # module ActiveRecord
|
21
22
|
|
22
23
|
|
24
|
+
########################
|
25
|
+
# add some methods to ActiveRecord::Base
|
26
|
+
#
|
27
|
+
# - todo: use Concerns ?? why? why not??
|
28
|
+
|
29
|
+
|
30
|
+
class ActiveRecord::Base
|
31
|
+
|
32
|
+
### note:
|
33
|
+
## alias_method will NOT work for activerecord attributes
|
34
|
+
## - attribute reader/writer will NOT exist on startup
|
35
|
+
# thus, lets add a new alias_attr that will
|
36
|
+
# send message / forward method call on demand
|
37
|
+
#
|
38
|
+
# e.g. use like:
|
39
|
+
#
|
40
|
+
# alias_attr :abvii, :abv
|
41
|
+
|
42
|
+
##
|
43
|
+
# todo: add opts={} e.g. lets us add, for example, depreciated: true ??
|
44
|
+
# - will issue a warning
|
45
|
+
|
46
|
+
def self.alias_attr_reader( new, old )
|
47
|
+
define_method( new ) do
|
48
|
+
send( old )
|
49
|
+
### use read_attribute( old ) instead? why? why not??
|
50
|
+
# see http://www.davidverhasselt.com/2011/06/28/5-ways-to-set-attributes-in-activerecord/
|
51
|
+
end
|
52
|
+
## todo: check for boolean values - add new? version too ??
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.alias_attr_writer( new, old )
|
56
|
+
define_method( "#{new}=" ) do |value|
|
57
|
+
send( "#{old}=", value )
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.alias_attr( new, old )
|
62
|
+
alias_attr_reader( new, old )
|
63
|
+
alias_attr_writer( new, old )
|
64
|
+
end
|
65
|
+
|
66
|
+
###################
|
67
|
+
#
|
68
|
+
#
|
69
|
+
# e.g. use like:
|
70
|
+
#
|
71
|
+
# def published
|
72
|
+
# read_attribute_w_fallbacks( :published, :touched )
|
73
|
+
# end
|
74
|
+
#
|
75
|
+
# or use macro e.g.
|
76
|
+
#
|
77
|
+
# attr_reader_w_fallbacks :published, :touched
|
78
|
+
|
79
|
+
|
80
|
+
def self.attr_reader_w_fallbacks( *keys )
|
81
|
+
define_method( keys[0] ) do
|
82
|
+
read_attribute_w_fallbacks( keys )
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def read_attribute_w_fallbacks( *keys )
|
87
|
+
### todo: use a different name e.g.:
|
88
|
+
## read_attribute_cascade ?? - does anything like this exists already?
|
89
|
+
## why? why not?
|
90
|
+
keys.each do |key|
|
91
|
+
value = read_attribute( key )
|
92
|
+
return value unless value.nil?
|
93
|
+
end
|
94
|
+
value # fallthrough? return latest value (will be nil) --or return just nil - why? why not??
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
###############
|
99
|
+
# random
|
100
|
+
#
|
101
|
+
#
|
102
|
+
# e.g. use like:
|
103
|
+
#
|
104
|
+
# beer_of_the_week = Beer.rnd
|
105
|
+
|
106
|
+
|
107
|
+
def self.rnd
|
108
|
+
## works w/ sqlite3 and postgresql
|
109
|
+
## - check if it works w/ mysql/mariadb too ? suppots offset and limit in SQL?
|
110
|
+
|
111
|
+
## todo: use ::rand -- and lets add an self.rand alias too ??
|
112
|
+
## check if ::rand call works
|
113
|
+
rnd_offset = rand( self.count ) ## NB: call "global" std lib rand
|
114
|
+
|
115
|
+
self.offset( rnd_offset ).limit( 1 ).first
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
end # class ActiveRecord::Base
|
120
|
+
|
121
|
+
|
122
|
+
|
23
123
|
puts ActiveRecord::Utils.banner # say hello
|
24
124
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-11-
|
12
|
+
date: 2013-11-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rdoc
|
16
|
-
requirement: &
|
16
|
+
requirement: &71207220 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.10'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *71207220
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: hoe
|
27
|
-
requirement: &
|
27
|
+
requirement: &71206890 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '3.3'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *71206890
|
36
36
|
description: activerecord-utils - utilities (e.g. random, alias_attr, etc.) for activerecord
|
37
37
|
email: webslideshow@googlegroups.com
|
38
38
|
executables: []
|