Skip to content

Commit f8a4ebf

Browse files
committed
add props
1 parent a0e280b commit f8a4ebf

24 files changed

+757
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ Gems:
77
- [logutils-admin](logutils-admin) - addon for browsing logs in database (e.g. LogDb, log model etc.)
88

99

10+
<!-- break -->
11+
- [**props**](props) - manage settings hierarchies (commandline, user, home, defaults, etc.)
12+
- [props-activerecord](props-activerecord) - addon for database support (ConfDb, props model, etc.)
13+
14+
1015
<!-- break -->
1116
- [shell-lite](shell-lite) - run / execute shell commands
1217

props-activerecord/.gitignore

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
*.gem
2+
*.rbc
3+
/.config
4+
/coverage/
5+
/InstalledFiles
6+
/pkg/
7+
/spec/reports/
8+
/test/tmp/
9+
/test/version_tmp/
10+
/tmp/
11+
12+
## Specific to RubyMotion:
13+
.dat*
14+
.repl_history
15+
build/
16+
17+
## Documentation cache and generated files:
18+
/.yardoc/
19+
/_yardoc/
20+
/doc/
21+
/rdoc/
22+
23+
## Environment normalisation:
24+
/.bundle/
25+
/vendor/bundle
26+
/lib/bundler/man/
27+
28+
# for a library or gem, you might want to ignore these files since the code is
29+
# intended to run in multiple environments; otherwise, check them in:
30+
# Gemfile.lock
31+
# .ruby-version
32+
# .ruby-gemset
33+
34+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35+
.rvmrc
36+
37+

props-activerecord/HISTORY.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### 0.0.1 / 2014-12-03
2+
3+
* Everything is new. First release

props-activerecord/Manifest.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
HISTORY.md
2+
Manifest.txt
3+
README.md
4+
Rakefile
5+
lib/props/activerecord.rb
6+
lib/props/activerecord/models.rb
7+
lib/props/activerecord/schema.rb
8+
lib/props/activerecord/version.rb
9+
test/helper.rb
10+
test/test_models.rb

props-activerecord/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# props-activerecord
2+
3+
Manage Setting Hierarchies Addon for Database Support (ConfDb, Props Model, etc.)
4+
5+
* home :: [github.com/rubylibs/props](https://github.com/rubylibs/props)
6+
* bugs :: [github.com/rubylibs/props/issues](https://github.com/rubylibs/props/issues)
7+
* gem :: [rubygems.org/gems/props-activerecord](https://rubygems.org/gems/props-activerecord)
8+
* rdoc :: [rubydoc.info/gems/props-activerecord](http://rubydoc.info/gems/props-activerecord)
9+
10+
11+
## Usage
12+
13+
### DB - Config Database / Schema / Model
14+
15+
Example:
16+
17+
```
18+
require 'props/activerecord' # include database support
19+
20+
ConfDb.create # build schema / tables (props)
21+
22+
Prop.create!( key: 'db.schema.version', '1.0.0' )
23+
24+
puts "Props:"
25+
Prop.order( 'created_at asc' ).all.each do |prop|
26+
puts " #{prop.key} / #{prop.value} | #{prop.created_at}"
27+
end
28+
```
29+
30+
More examples:
31+
32+
```
33+
ConfDb.tables # dump stats to console
34+
ConfDb.delete! # delete all records
35+
```
36+
37+
38+
## License
39+
40+
The `props-activerecord` scripts are dedicated to the public domain.
41+
Use it as you please with no restrictions whatsoever.

props-activerecord/Rakefile

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'hoe'
2+
require './lib/props/activerecord/version.rb'
3+
4+
Hoe.spec 'props-activerecord' do
5+
6+
self.version = ConfDb::VERSION
7+
8+
self.summary = 'props-activerecord - Manage Setting Hierachies Addon for Database Support (ConfDb, Props Model, etc.)'
9+
self.description = summary
10+
11+
self.urls = ['https://github.com/rubylibs/props']
12+
13+
self.author = 'Gerald Bauer'
14+
self.email = '[email protected]'
15+
16+
# switch extension to .markdown for gihub formatting
17+
self.readme_file = 'README.md'
18+
self.history_file = 'HISTORY.md'
19+
20+
self.licenses = ['Public Domain']
21+
22+
self.extra_deps = [
23+
['props', '>=1.2.0'],
24+
['activerecord'], # Note: will include activesupport, etc.
25+
]
26+
27+
self.spec_extras = {
28+
required_ruby_version: '>= 2.3'
29+
}
30+
31+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# encoding: utf-8
2+
3+
4+
## note: assumes props alreay required
5+
## this is an addon/plugin/extension for the props gems (that is, will NOT work stand-alone)
6+
7+
# rubygems / 3rd party libs
8+
require 'active_record' ## todo: add sqlite3? etc.
9+
10+
# our own code
11+
12+
require 'props/activerecord/version' # version always goes first
13+
require 'props/activerecord/schema'
14+
require 'props/activerecord/models'
15+
16+
17+
18+
module ConfDb
19+
20+
def self.create
21+
CreateDb.new.up
22+
end
23+
24+
# delete ALL records (use with care!)
25+
def self.delete!
26+
puts '*** deleting props table records/data...'
27+
Prop.delete_all
28+
end # method delete!
29+
30+
## def self.stats ## remove ? -- duplicate - use tables - why?? why not????
31+
## puts "#{Model::Prop} props"
32+
## end
33+
34+
def self.tables
35+
puts "#{Prop.count} props"
36+
end
37+
38+
end # module ConfDb
39+
40+
41+
42+
puts ConfDb.banner if $DEBUG || (defined?($RUBYLIBS_DEBUG) && $RUBYLIBS_DEBUG) # say hello
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# encoding: utf-8
2+
3+
module ConfDb
4+
module Model
5+
6+
7+
class Prop < ActiveRecord::Base
8+
9+
### todo/fix: move create_from_fixture! method to textutils ??? - why? why not?
10+
## yes - move to textutils! - fixture concept/notion is part of textutils
11+
12+
def self.create_from_fixture!( name, path )
13+
key = "db.#{fixture_name_to_prop_key(name)}.version"
14+
15+
value = "txt.#{File.mtime(path).strftime('%Y.%m.%d')}"
16+
17+
Prop.create!( key: key, value: value )
18+
end
19+
20+
### todo:
21+
## add latest scope -> order props by creation date desc!!!!
22+
23+
## add dump -> dump latest props to console via puts
24+
## check - dump already taken by activerecord??
25+
26+
private
27+
28+
# helper
29+
# change at/2012_13/bl to at.2012/13.bl
30+
# or quali_2012_13_europe_c to quali.2012/13.europe.c
31+
32+
def self.fixture_name_to_prop_key( name )
33+
prop_key = name.gsub( '/', '.' )
34+
prop_key = prop_key.gsub( /(\d{4})_(\d{2})/, '\1/\2' ) # 2012_13 => 2012/13
35+
prop_key = prop_key.gsub( '_', '.' )
36+
prop_key
37+
end
38+
39+
end # class Prop
40+
41+
end # module Model
42+
43+
##### add convenience module alias in plural e.g. lets you use include ConfDb::Models
44+
Models = Model
45+
##### add convenience class alias e.g lets you use Prop instead of Model::Prop
46+
Prop = Model::Prop
47+
48+
end # module ConfDb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# encoding: utf-8
2+
3+
module ConfDb
4+
5+
class CreateDb
6+
7+
def up
8+
ActiveRecord::Schema.define do
9+
10+
create_table :props do |t|
11+
t.string :key, null: false
12+
t.string :value, null: false
13+
14+
# note: do NOT/can NOT use type - already used by ActiveRecord (for single-table-inheritance/STI)
15+
t.string :kind # e.g. version|user|sys(tem)|db etc.
16+
17+
t.timestamps
18+
end
19+
20+
end # Schema.define
21+
end # method up
22+
23+
end # class CreateDb
24+
25+
end # module ConfDb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# encoding: utf-8
2+
3+
module ConfDb
4+
5+
MAJOR = 0 ## todo: namespace inside version or something - why? why not??
6+
MINOR = 2
7+
PATCH = 0
8+
VERSION = [MAJOR,MINOR,PATCH].join('.')
9+
10+
def self.version
11+
VERSION
12+
end
13+
14+
def self.banner
15+
# todo: add RUBY_PATCHLEVEL or RUBY_PATCH_LEVEL??
16+
"confdb/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
17+
end
18+
19+
def self.root
20+
"#{File.expand_path( File.dirname(File.dirname(File.dirname(File.dirname(__FILE__)))) )}"
21+
end
22+
23+
end # module ConfDb

props-activerecord/test/helper.rb

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
## $:.unshift(File.dirname(__FILE__))
3+
4+
## minitest setup
5+
6+
require 'minitest/autorun'
7+
8+
9+
# our own code
10+
require 'props'
11+
require 'props/activerecord' # Note: explict require required for ConfDb (not automatic)
12+
13+
14+
Prop = ConfDb::Model::Prop
15+
16+
17+
18+
def setup_in_memory_db
19+
# Database Setup & Config
20+
21+
db_config = {
22+
adapter: 'sqlite3',
23+
database: ':memory:'
24+
}
25+
26+
pp db_config
27+
28+
ActiveRecord::Base.logger = Logger.new( STDOUT )
29+
## ActiveRecord::Base.colorize_logging = false - no longer exists - check new api/config setting?
30+
31+
## NB: every connect will create a new empty in memory db
32+
ActiveRecord::Base.establish_connection( db_config )
33+
34+
35+
## build schema
36+
ConfDb.create
37+
end
38+
39+
40+
setup_in_memory_db()
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# encoding: utf-8
2+
3+
require 'helper'
4+
5+
class TestModels < MiniTest::Test
6+
7+
def setup
8+
ConfDb.delete!
9+
end
10+
11+
def test_count
12+
assert_equal 0, Prop.count
13+
end
14+
15+
16+
def test_create
17+
prop = Prop.create!( key: 'key', value: 'value' )
18+
19+
assert_equal 1, Prop.count
20+
end
21+
22+
end # class TestModels

props/.gitignore

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
*.gem
2+
*.rbc
3+
/.config
4+
/coverage/
5+
/InstalledFiles
6+
/pkg/
7+
/spec/reports/
8+
/test/tmp/
9+
/test/version_tmp/
10+
/tmp/
11+
12+
## Specific to RubyMotion:
13+
.dat*
14+
.repl_history
15+
build/
16+
17+
## Documentation cache and generated files:
18+
/.yardoc/
19+
/_yardoc/
20+
/doc/
21+
/rdoc/
22+
23+
## Environment normalisation:
24+
/.bundle/
25+
/vendor/bundle
26+
/lib/bundler/man/
27+
28+
# for a library or gem, you might want to ignore these files since the code is
29+
# intended to run in multiple environments; otherwise, check them in:
30+
# Gemfile.lock
31+
# .ruby-version
32+
# .ruby-gemset
33+
34+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35+
.rvmrc
36+
37+

props/HISTORY.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
### 0.2 / 2012-06-10
2+
3+
* Add optional erb processing with passed in binding (`load_file_with_erb`)
4+
* Add section lookup (`fetch_with_section`)
5+
6+
### 0.1 / 2012-06-03
7+
8+
* Everything is new. First release

0 commit comments

Comments
 (0)