activerecord-utils 0.1.0 → 0.2.0

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: eecd95d124a2fccb441fb7dfeafc2d5dc87c3679
4
+ data.tar.gz: ce9c1533eb2bd30b81bc0ba41b31fb4223c15bb3
5
+ SHA512:
6
+ metadata.gz: b3f887f9f2709b50bafe9b8ed500be5fece830ad71355f4c7e7d9baacaa41d1d10d42aca019b2be98f00b26445fb50f74d031ebc336928e561f57fa630cd9ec7
7
+ data.tar.gz: f036fddcc6990e7932b15d9ccbb29168214d58a12b6b5080e0e908cb084ca6d46f395fcd2f8b9f2d6f95f9f968108c7438ae9c025c2170149961955a92fa17b8
data/.gemtest ADDED
File without changes
data/Manifest.txt CHANGED
@@ -3,4 +3,10 @@ Manifest.txt
3
3
  README.md
4
4
  Rakefile
5
5
  lib/activerecord/utils.rb
6
+ lib/activerecord/utils/alias.rb
7
+ lib/activerecord/utils/comp3.rb
8
+ lib/activerecord/utils/random.rb
6
9
  lib/activerecord/utils/version.rb
10
+ test/helper.rb
11
+ test/test_finders.rb
12
+ test/test_random.rb
data/README.md CHANGED
@@ -19,7 +19,7 @@ generates
19
19
  def plato # reader
20
20
  send( :og ) # e.g. return og
21
21
  end
22
-
22
+
23
23
  def plato=(value) # writer
24
24
  send( :'og=', value ) # e.g. return self.og=value
25
25
  end
@@ -32,20 +32,23 @@ generates
32
32
  generates
33
33
 
34
34
  def published
35
- read_attribute_w_fallbacks( :published, :touched )
35
+ # e.g. read_attribute_w_fallbacks( :published, :touched ) - equals:
36
+
37
+ value = read_attribute( :published )
38
+ value = read_attribute( :touched ) if value.nil?
39
+ value
36
40
  end
37
41
 
38
42
 
39
- ### 'rnd' finder
40
-
41
- e.g. find random record e.g.
43
+ ### `rnd` finder
42
44
 
45
+ Find random record e.g.:
43
46
 
44
47
  beer_of_the_week = Beer.rnd
45
48
 
46
49
  equals
47
50
 
48
- beer_of_the_week = Beer.offset( rand( Beer.count) ).limit(1).first
51
+ beer_of_the_week = Beer.offset( rand( Beer.count ) ).limit(1).first
49
52
 
50
53
 
51
54
 
@@ -63,4 +66,4 @@ equals
63
66
  ## License
64
67
 
65
68
  The scripts are dedicated to the public domain.
66
- Use it as you please with no restrictions whatsoever.
69
+ Use it as you please with no restrictions whatsoever.
data/Rakefile CHANGED
@@ -18,13 +18,13 @@ Hoe.spec 'activerecord-utils' do
18
18
  self.history_file = 'History.md'
19
19
 
20
20
  self.extra_deps = [
21
- ### ['logutils', '>= 0.5']
21
+ ['logutils']
22
22
  ]
23
23
 
24
24
  self.licenses = ['Public Domain']
25
25
 
26
26
  self.spec_extras = {
27
- :required_ruby_version => '>= 1.9.2'
27
+ required_ruby_version: '>= 1.9.2'
28
28
  }
29
29
 
30
30
 
@@ -1,5 +1,4 @@
1
1
 
2
-
3
2
  require 'activerecord/utils/version' # let version always go first
4
3
 
5
4
 
@@ -8,14 +7,15 @@ module ActiveRecord
8
7
  module Utils
9
8
 
10
9
  def self.banner
11
- "activerecord-utils #{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
10
+ ## add ar PRE too? how?
11
+ ar = "activerecord/#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}.#{ActiveRecord::VERSION::TINY}"
12
+ "activerecord-utils/#{VERSION} (#{ar}) on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
12
13
  end
13
14
 
14
- =begin
15
15
  def self.root
16
- "#{File.expand_path( File.dirname(File.dirname(__FILE__)) )}"
16
+ "#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}"
17
17
  end
18
- =end
18
+
19
19
  end # module Utils
20
20
 
21
21
  end # module ActiveRecord
@@ -23,102 +23,17 @@ end # module ActiveRecord
23
23
 
24
24
  ########################
25
25
  # add some methods to ActiveRecord::Base
26
- #
27
26
  # - todo: use Concerns ?? why? why not??
28
27
 
28
+ if ActiveRecord::VERSION::MAJOR == 3 # only add to 3.x series
29
+ require 'activerecord/utils/comp3'
30
+ end
29
31
 
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
-
32
+ require 'activerecord/utils/alias'
33
+ require 'activerecord/utils/random'
121
34
 
122
35
 
123
36
  puts ActiveRecord::Utils.banner # say hello
124
37
 
38
+ ### puts "root: >#{ActiveRecord::Utils.root}<" # check root path (remove later)
39
+
@@ -0,0 +1,71 @@
1
+
2
+ module ActiveRecord
3
+ class Base
4
+
5
+ ### note:
6
+ ## alias_method will NOT work for activerecord attributes
7
+ ## - attribute reader/writer will NOT exist on startup
8
+ # thus, lets add a new alias_attr that will
9
+ # send message / forward method call on demand
10
+ #
11
+ # e.g. use like:
12
+ #
13
+ # alias_attr :abvii, :abv
14
+
15
+ ##
16
+ # todo: add opts={} e.g. lets us add, for example, depreciated: true ??
17
+ # - will issue a warning
18
+
19
+ def self.alias_attr_reader( new, old )
20
+ define_method( new ) do
21
+ send( old )
22
+ ### use read_attribute( old ) instead? why? why not??
23
+ # see http://www.davidverhasselt.com/2011/06/28/5-ways-to-set-attributes-in-activerecord/
24
+ end
25
+ ## todo: check for boolean values - add new? version too ??
26
+ end
27
+
28
+ def self.alias_attr_writer( new, old )
29
+ define_method( "#{new}=" ) do |value|
30
+ send( "#{old}=", value )
31
+ end
32
+ end
33
+
34
+ def self.alias_attr( new, old )
35
+ alias_attr_reader( new, old )
36
+ alias_attr_writer( new, old )
37
+ end
38
+
39
+ ###################
40
+ #
41
+ #
42
+ # e.g. use like:
43
+ #
44
+ # def published
45
+ # read_attribute_w_fallbacks( :published, :touched )
46
+ # end
47
+ #
48
+ # or use macro e.g.
49
+ #
50
+ # attr_reader_w_fallbacks :published, :touched
51
+
52
+
53
+ def self.attr_reader_w_fallbacks( *keys )
54
+ define_method( keys[0] ) do
55
+ read_attribute_w_fallbacks( keys )
56
+ end
57
+ end
58
+
59
+ def read_attribute_w_fallbacks( *keys )
60
+ ### todo: use a different name e.g.:
61
+ ## read_attribute_cascade ?? - does anything like this exists already?
62
+ ## why? why not?
63
+ keys.each do |key|
64
+ value = read_attribute( key )
65
+ break unless value.nil? # if value.nil? == false
66
+ end
67
+ value # fallthrough -- return latest value (will be nil)
68
+ end
69
+
70
+ end # class Base
71
+ end # module ActiveRecord
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+
3
+ module ActiveRecord
4
+ class Base
5
+
6
+ ###
7
+ # todo/fix:
8
+ # better check if responds_to? find_by! and find_by ???
9
+ puts " [ActiveRecord::Utils] adding find_by n find_by! finders for 3.x series"
10
+
11
+ ## lets us use ActiveRecord 4-style find_by and find_by! in ActiveRecord 3.x
12
+ def self.find_by( hash )
13
+ self.where( hash ).first
14
+ end
15
+
16
+ def self.find_by!( hash )
17
+ self.where( hash ).first! ## or use: first or raise RecordNotFound directly??
18
+ end
19
+
20
+ end # class Base
21
+ end # module ActiveRecord
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+
3
+ module ActiveRecord
4
+ class Base
5
+
6
+ ###############
7
+ # random
8
+ #
9
+ #
10
+ # e.g. use like:
11
+ #
12
+ # beer_of_the_week = Beer.rnd
13
+
14
+ def self.rnd
15
+ ## works w/ sqlite3 and postgresql
16
+ ## - check if it works w/ mysql/mariadb too ? suppots offset and limit in SQL?
17
+
18
+ ## todo: use ::rand -- and lets add an self.rand alias too ??
19
+ ## check if ::rand call works
20
+ rnd_offset = rand( self.count ) ## NB: call "global" std lib rand
21
+
22
+ self.offset( rnd_offset ).limit( 1 ).first
23
+ end
24
+
25
+
26
+ end # class Base
27
+ end # module ActiveRecord
@@ -2,7 +2,8 @@
2
2
  module ActiveRecord
3
3
 
4
4
  module Utils
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
 
8
8
  end # module ActiveRecord
9
+
data/test/helper.rb ADDED
@@ -0,0 +1,90 @@
1
+
2
+ ## $:.unshift(File.dirname(__FILE__))
3
+
4
+ ## minitest setup
5
+
6
+ # require 'minitest/unit'
7
+ require 'minitest/autorun'
8
+
9
+ # include MiniTest::Unit # lets us use TestCase instead of MiniTest::Unit::TestCase
10
+
11
+
12
+ # ruby stdlibs
13
+
14
+ require 'json'
15
+ require 'uri'
16
+ require 'pp'
17
+ require 'logger'
18
+
19
+ # ruby gems
20
+
21
+ require 'active_record'
22
+
23
+ require 'logutils'
24
+ require 'logutils/db' # NB: explict require required for LogDb (not automatic)
25
+
26
+
27
+ # our own code
28
+
29
+ require 'activerecord/utils'
30
+
31
+
32
+ class Beer < ActiveRecord::Base
33
+ alias_attr :abvii, :abv
34
+ alias_attr_reader :abvread, :abv
35
+ alias_attr_writer :abvwrite, :abv
36
+ end
37
+
38
+
39
+
40
+ class CreateBeerDb
41
+
42
+ def up
43
+ ActiveRecord::Schema.define do
44
+
45
+ create_table :beers do |t|
46
+ t.string :key, null: false
47
+ t.string :name, null: false
48
+
49
+ t.string :web # optional url link (e.g. )
50
+ t.integer :since # optional year (e.g. 1896)
51
+
52
+ t.decimal :abv # Alcohol by volume (abbreviated as ABV, abv, or alc/vol) e.g. 4.9 %
53
+ t.decimal :og # malt extract (original gravity) in plato
54
+
55
+ t.timestamps
56
+ end
57
+ end # Schema.define
58
+ end # method up
59
+
60
+ end # class CreateMovieDb
61
+
62
+
63
+
64
+ def setup_in_memory_db
65
+ # Database Setup & Config
66
+
67
+ db_config = {
68
+ adapter: 'sqlite3',
69
+ database: ':memory:'
70
+ }
71
+
72
+ pp db_config
73
+
74
+ ActiveRecord::Base.logger = Logger.new( STDOUT )
75
+ ## ActiveRecord::Base.colorize_logging = false - no longer exists - check new api/config setting?
76
+
77
+ ## NB: every connect will create a new empty in memory db
78
+ ActiveRecord::Base.establish_connection( db_config )
79
+
80
+
81
+ ## build schema
82
+ LogDb.create
83
+ CreateBeerDb.new.up
84
+
85
+ Beer.create!( key: 'ottakringerhelles', name: 'Ottakringer Helles' )
86
+ Beer.create!( key: 'ottakringerzwicklrot', name: 'Ottakringer Zwickl Rot' )
87
+ end
88
+
89
+
90
+ setup_in_memory_db()
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ require 'helper'
4
+
5
+ class TestFinders < MiniTest::Unit::TestCase
6
+
7
+
8
+ def test_find_by
9
+ b = Beer.find_by( key: 'ottakringerhelles' )
10
+ assert_equal 'Ottakringer Helles', b.name
11
+
12
+ b = Beer.find_by( key: 'zwettlerdunkles' )
13
+ assert_equal nil, b
14
+
15
+ b = Beer.find_by!( key: 'ottakringerhelles' )
16
+ assert_equal 'Ottakringer Helles', b.name
17
+
18
+ ## fix/todo: add
19
+ ## raises RecordNotfound
20
+ ## b = Beer.find_by!( key: 'zwettlerdunkles' )
21
+ end
22
+
23
+ end # class TestFinders
24
+
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ require 'helper'
4
+
5
+ class TestRandom < MiniTest::Unit::TestCase
6
+
7
+ def test_rnd
8
+ b = Beer.rnd
9
+ assert_equal false, b.name.blank?
10
+ assert_equal false, b.key.blank?
11
+
12
+ ## try another
13
+ b = Beer.rnd
14
+ assert_equal false, b.name.blank?
15
+ assert_equal false, b.key.blank?
16
+ end
17
+
18
+ end # class TestRandom
metadata CHANGED
@@ -1,54 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Gerald Bauer
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-11-24 00:00:00.000000000 Z
11
+ date: 2014-04-15 00:00:00.000000000 Z
13
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logutils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
14
27
  - !ruby/object:Gem::Dependency
15
28
  name: rdoc
16
- requirement: &71207220 !ruby/object:Gem::Requirement
17
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
18
30
  requirements:
19
31
  - - ~>
20
32
  - !ruby/object:Gem::Version
21
- version: '3.10'
33
+ version: '4.0'
22
34
  type: :development
23
35
  prerelease: false
24
- version_requirements: *71207220
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
25
41
  - !ruby/object:Gem::Dependency
26
42
  name: hoe
27
- requirement: &71206890 !ruby/object:Gem::Requirement
28
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
29
44
  requirements:
30
45
  - - ~>
31
46
  - !ruby/object:Gem::Version
32
- version: '3.3'
47
+ version: '3.10'
33
48
  type: :development
34
49
  prerelease: false
35
- version_requirements: *71206890
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.10'
36
55
  description: activerecord-utils - utilities (e.g. random, alias_attr, etc.) for activerecord
37
56
  email: webslideshow@googlegroups.com
38
57
  executables: []
39
58
  extensions: []
40
59
  extra_rdoc_files:
60
+ - History.md
41
61
  - Manifest.txt
62
+ - README.md
42
63
  files:
43
64
  - History.md
44
65
  - Manifest.txt
45
66
  - README.md
46
67
  - Rakefile
47
68
  - lib/activerecord/utils.rb
69
+ - lib/activerecord/utils/alias.rb
70
+ - lib/activerecord/utils/comp3.rb
71
+ - lib/activerecord/utils/random.rb
48
72
  - lib/activerecord/utils/version.rb
73
+ - test/helper.rb
74
+ - test/test_finders.rb
75
+ - test/test_random.rb
76
+ - .gemtest
49
77
  homepage: https://github.com/rubylibs/activerecord-utils
50
78
  licenses:
51
79
  - Public Domain
80
+ metadata: {}
52
81
  post_install_message:
53
82
  rdoc_options:
54
83
  - --main
@@ -56,21 +85,21 @@ rdoc_options:
56
85
  require_paths:
57
86
  - lib
58
87
  required_ruby_version: !ruby/object:Gem::Requirement
59
- none: false
60
88
  requirements:
61
89
  - - ! '>='
62
90
  - !ruby/object:Gem::Version
63
91
  version: 1.9.2
64
92
  required_rubygems_version: !ruby/object:Gem::Requirement
65
- none: false
66
93
  requirements:
67
94
  - - ! '>='
68
95
  - !ruby/object:Gem::Version
69
96
  version: '0'
70
97
  requirements: []
71
- rubyforge_project: activerecord-utils
72
- rubygems_version: 1.8.17
98
+ rubyforge_project:
99
+ rubygems_version: 2.1.10
73
100
  signing_key:
74
- specification_version: 3
101
+ specification_version: 4
75
102
  summary: activerecord-utils - utilities (e.g. random, alias_attr, etc.) for activerecord
76
- test_files: []
103
+ test_files:
104
+ - test/test_finders.rb
105
+ - test/test_random.rb