props 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -3,5 +3,13 @@ Manifest.txt
3
3
  README.md
4
4
  Rakefile
5
5
  lib/props.rb
6
+ lib/props/db.rb
7
+ lib/props/db/deleter.rb
8
+ lib/props/db/models.rb
9
+ lib/props/db/schema.rb
10
+ lib/props/env.rb
11
+ lib/props/ini.rb
12
+ lib/props/props.rb
13
+ lib/props/version.rb
6
14
  test/helper.rb
7
15
  test/test_ini.rb
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
1
  require 'hoe'
2
- require './lib/props.rb'
2
+ require './lib/props/version.rb'
3
3
 
4
4
  Hoe.spec 'props' do
5
5
 
6
- self.version = Props::VERSION
7
-
6
+ self.version = ConfUtils::VERSION
7
+
8
8
  self.summary = 'props - Manage Settings Hierachies (Commandline, User, Home, Defaults, etc.)'
9
9
  self.description = summary
10
10
 
@@ -0,0 +1,15 @@
1
+
2
+ module ConfDb
3
+
4
+ class Deleter
5
+ include ConfDb::Models
6
+
7
+ def run
8
+ # for now delete all tables
9
+
10
+ Prop.delete_all
11
+ end
12
+
13
+ end # class Deleter
14
+
15
+ end # module ConfDb
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ module ConfDb
4
+ module Models
5
+
6
+
7
+ class Prop < ActiveRecord::Base
8
+
9
+ end # class Prop
10
+
11
+
12
+ end # module Models
13
+ end # module ConfDb
@@ -0,0 +1,23 @@
1
+ module ConfDb
2
+
3
+
4
+ class CreateDb < ActiveRecord::Migration
5
+
6
+ def up
7
+
8
+ create_table :props do |t|
9
+ t.string :key, :null => false
10
+ t.string :value, :null => false
11
+ t.string :kind # e.g. version|user|sys(tem)|db etc. # note: can NOT use type - already used by ActiveRecord
12
+ t.timestamps
13
+ end
14
+
15
+ end
16
+
17
+ def down
18
+ raise ActiveRecord::IrreversibleMigration
19
+ end
20
+
21
+ end # class CreateDb
22
+
23
+ end # module ConfDb
data/lib/props/db.rb ADDED
@@ -0,0 +1,41 @@
1
+ #########################################
2
+ # NB: only load on demand
3
+ # we do NOT want to pull in activerecord gem/dep for simple scripts
4
+
5
+ # rubygems / 3rd party libs
6
+
7
+ require 'active_record' ## todo: add sqlite3? etc.
8
+
9
+ # our own code
10
+
11
+ require 'props/db/models'
12
+ require 'props/db/schema'
13
+ require 'props/db/deleter'
14
+
15
+
16
+ module ConfDb
17
+
18
+ def self.banner
19
+ "confdb #{ConfUtils::VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
20
+ end
21
+
22
+ def self.create
23
+ CreateDb.new.up
24
+ end
25
+
26
+ # delete ALL records (use with care!)
27
+ def self.delete!
28
+ puts '*** deleting props table records/data...'
29
+ Deleter.new.run
30
+ end # method delete!
31
+
32
+ def self.stats
33
+ # to be done
34
+ end
35
+
36
+
37
+ end # module ConfDb
38
+
39
+
40
+ # say hello
41
+ puts ConfDb.banner
data/lib/props/env.rb ADDED
@@ -0,0 +1,30 @@
1
+ module ConfUtils
2
+
3
+ class Env
4
+
5
+ def self.home
6
+ path = if( ENV['HOME'] || ENV['USERPROFILE'] )
7
+ ENV['HOME'] || ENV['USERPROFILE']
8
+ elsif( ENV['HOMEDRIVE'] && ENV['HOMEPATH'] )
9
+ "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}"
10
+ else
11
+ begin
12
+ File.expand_path('~')
13
+ rescue
14
+ if File::ALT_SEPARATOR
15
+ 'C:/'
16
+ else
17
+ '/'
18
+ end
19
+ end
20
+ end
21
+
22
+ # todo: use logger - how?
23
+ ## puts "env home=>#{path}<"
24
+
25
+ path
26
+ end
27
+
28
+ end # class Env
29
+
30
+ end # module ConfUtils
data/lib/props/ini.rb ADDED
@@ -0,0 +1,84 @@
1
+
2
+ module ConfUtils
3
+
4
+
5
+ class IniFile
6
+
7
+ # returns a nested hash
8
+ # (compatible structure - works like YAML.load_file)
9
+
10
+ def self.load_file( path )
11
+ text = File.open( path, 'r:bom|utf-8' ).read
12
+ self.load( text )
13
+ end
14
+
15
+ def self.load( text )
16
+ IniFile.new( text ).parse
17
+ end
18
+
19
+
20
+ def initialize( text )
21
+ @text = text
22
+ end
23
+
24
+ def parse
25
+ hash = top_hash = Hash.new
26
+
27
+ text = @text
28
+ text = text.gsub( "\t", ' ' ) # replace all tabs w/ spaces
29
+
30
+ text.each_line do |line|
31
+
32
+ ### skip comments
33
+ # e.g. # this is a comment line
34
+ # or ; this too
35
+ # or -- haskell style
36
+ # or % text style
37
+
38
+ if line =~ /^\s*#/ || line =~ /^\s*;/ || line =~ /^\s*--/ || line =~ /^\s*%/
39
+ ## logger.debug 'skipping comment line'
40
+ next
41
+ end
42
+
43
+ ### skip blank lines
44
+ if line =~ /^\s*$/
45
+ ## logger.debug 'skipping blank line'
46
+ next
47
+ end
48
+
49
+ # pass 1) remove possible trailing eol comment
50
+ ## e.g -> New York # Sample EOL Comment Here (with or without commas,,,,)
51
+ ## becomes -> New York
52
+
53
+ line = line.sub( /\s+#.*$/, '' )
54
+
55
+ # pass 2) remove leading and trailing whitespace
56
+
57
+ line = line.strip
58
+
59
+ ## check for new section e.g. [planet012-xxx_bc]
60
+
61
+ ### todo: allow _ or - in strict section key? why? why not??
62
+ ### allow _ or - in value key? why why not??
63
+ if line =~ /^\s*\[\s*([a-z0-9_\-]+)\s*\]\s*$/ # strict section
64
+ key = $1.to_s.dup
65
+ hash = top_hash[ key ] = Hash.new
66
+ elsif line =~ /^\s*\[\s*([^ \]]+)\s*\]\s*$/ # liberal section; allow everything in key
67
+ key = $1.to_s.dup
68
+ hash = top_hash[ key ] = Hash.new
69
+ elsif line =~ /^\s*([a-z0-9_\-]+)\s*[:=](.*)$/
70
+ key = $1.to_s.dup
71
+ value = $2.to_s.strip.dup # check if it can be nil? if yes use blank string e.g. ''
72
+ ### todo: strip quotes from value??? why? why not?
73
+ hash[ key ] = value
74
+ else
75
+ puts "*** warn: skipping unknown line type in ini >#{line}<"
76
+ end
77
+ end # each lines
78
+
79
+ top_hash
80
+ end
81
+
82
+ end # class IniReader
83
+
84
+ end # module ConfUtils
@@ -0,0 +1,64 @@
1
+
2
+ module ConfUtils
3
+
4
+
5
+ class Props
6
+
7
+ VERSION = ConfUtils::VERSION # remove version; depreciated api/constant; use ConfUtils::VERSION
8
+
9
+ attr_reader :path
10
+ attr_reader :parent
11
+
12
+
13
+ def self.load_file( path, parent=nil )
14
+ h = YAML.load_file( path )
15
+ Props.new( h, path, parent )
16
+ end
17
+
18
+ ### todo: use TOP_LEVEL_BINDING for binding default?
19
+ def self.load_file_with_erb( path, binding, parent=nil ) # run through erb first
20
+ text = ERB.new( File.read( path ) ).result( binding )
21
+ h = YAML.load( text )
22
+ Props.new( h, path, parent )
23
+ end
24
+
25
+
26
+ def initialize( hash, path, parent=nil)
27
+ @hash = hash
28
+ @path = path
29
+ @parent = parent
30
+ end
31
+
32
+ def dump # for debugging
33
+ puts "dump of >#{@path}<:"
34
+ pp @hash
35
+ end
36
+
37
+
38
+ def fetch(key, default)
39
+ value = get( key )
40
+ value.nil? ? default : value
41
+ end
42
+
43
+ def fetch_from_section(section, key, default)
44
+ value = get_from_section( section, key )
45
+ value.nil? ? default : value
46
+ end
47
+
48
+ def [](key) get( key ); end
49
+
50
+ def get( key )
51
+ value = @hash.fetch( key.to_s, nil )
52
+ # if not found try lookup in parent hash
53
+ (value.nil? && parent) ? parent.get(key) : value
54
+ end
55
+
56
+ def get_from_section( section, key )
57
+ value = @hash.fetch( section.to_s, {} ).fetch( key.to_s, nil )
58
+ # if not found try lookup in parent hash
59
+ (value.nil? && parent) ? parent.get_from_section(section,key) : value
60
+ end
61
+
62
+ end # class Props
63
+
64
+ end # module ConfUtils
@@ -0,0 +1,5 @@
1
+
2
+ module ConfUtils
3
+ VERSION = '1.0.2'
4
+ end
5
+
data/lib/props.rb CHANGED
@@ -8,161 +8,45 @@ require 'fileutils'
8
8
  require 'erb'
9
9
 
10
10
 
11
+ # our own code
11
12
 
12
- class Env
13
-
14
- def self.home
15
- path = if( ENV['HOME'] || ENV['USERPROFILE'] )
16
- ENV['HOME'] || ENV['USERPROFILE']
17
- elsif( ENV['HOMEDRIVE'] && ENV['HOMEPATH'] )
18
- "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}"
19
- else
20
- begin
21
- File.expand_path('~')
22
- rescue
23
- if File::ALT_SEPARATOR
24
- 'C:/'
25
- else
26
- '/'
27
- end
28
- end
29
- end
30
-
31
- # todo: use logger - how?
32
- ## puts "env home=>#{path}<"
33
-
34
- path
35
- end
36
-
37
- end # class Env
13
+ require 'props/version' # version always goes first
14
+ require 'props/env'
15
+ require 'props/ini'
16
+ require 'props/props'
38
17
 
39
18
 
40
- module INI
19
+ ######################
20
+ # add top_level convenience alias for classes
41
21
 
42
- def self.load_file( path )
43
- # returns a nested hash
44
- # (compatible structure - works like YAML.load_file)
22
+ Env = ConfUtils::Env
23
+ Props = ConfUtils::Props
45
24
 
46
- text = File.open( path, 'r:bom|utf-8' ).read
47
- self.load( text )
48
- end
49
25
 
26
+ module INI
50
27
 
51
- def self.load( text )
52
- hash = top_hash = Hash.new
53
-
54
- text = text.gsub( "\t", ' ' ) # replace all tabs w/ spaces
55
-
56
- text.each_line do |line|
57
-
58
- ### skip comments
59
- # e.g. # this is a comment line
60
- # or ; this too
61
- # or -- haskell style
62
- # or % text style
63
-
64
- if line =~ /^\s*#/ || line =~ /^\s*;/ || line =~ /^\s*--/ || line =~ /^\s*%/
65
- ## logger.debug 'skipping comment line'
66
- next
67
- end
68
-
69
- ### skip blank lines
70
- if line =~ /^\s*$/
71
- ## logger.debug 'skipping blank line'
72
- next
73
- end
74
-
75
- # pass 1) remove possible trailing eol comment
76
- ## e.g -> New York # Sample EOL Comment Here (with or without commas,,,,)
77
- ## becomes -> New York
78
-
79
- line = line.sub( /\s+#.*$/, '' )
80
-
81
- # pass 2) remove leading and trailing whitespace
82
-
83
- line = line.strip
84
-
85
- ## check for new section e.g. [planet012-xxx_bc]
86
-
87
- ### todo: allow _ or - in strict section key? why? why not??
88
- ### allow _ or - in value key? why why not??
89
- if line =~ /^\s*\[\s*([a-z0-9_\-]+)\s*\]\s*$/ # strict section
90
- key = $1.to_s.dup
91
- hash = top_hash[ key ] = Hash.new
92
- elsif line =~ /^\s*\[\s*([^ \]]+)\s*\]\s*$/ # liberal section; allow everything in key
93
- key = $1.to_s.dup
94
- hash = top_hash[ key ] = Hash.new
95
- elsif line =~ /^\s*([a-z0-9_\-]+)\s*[:=](.*)$/
96
- key = $1.to_s.dup
97
- value = $2.to_s.strip.dup # check if it can be nil? if yes use blank string e.g. ''
98
- ### todo: strip quotes from value??? why? why not?
99
- hash[ key ] = value
100
- else
101
- puts "*** warn: skipping unknown line type in ini >#{line}<"
102
- end
103
- end # each lines
104
-
105
- top_hash
106
- end # method load
28
+ # returns a nested hash
29
+ # (compatible structure - works like YAML.load_file)
107
30
 
31
+ def self.load_file( path ) ConfUtils::IniFile.load_file( path ); end
32
+ def self.load( text ) ConfUtils::IniFile.load( text ); end
108
33
 
109
34
  end # module INI
110
35
 
111
36
 
37
+ module ConfUtils
112
38
 
113
- class Props
114
-
115
- VERSION = '1.0.1'
116
-
117
- attr_reader :path
118
- attr_reader :parent
119
-
120
- def initialize( hash, path, parent=nil)
121
- @hash = hash
122
- @path = path
123
- @parent = parent
124
- end
125
-
126
- def self.load_file( path, parent=nil )
127
- h = YAML.load_file( path )
128
- Props.new( h, path, parent )
129
- end
130
-
131
- ### todo: use TOP_LEVEL_BINDING for binding default?
132
- def self.load_file_with_erb( path, binding, parent=nil ) # run through erb first
133
- text = ERB.new( File.read( path ) ).result( binding )
134
- h = YAML.load( text )
135
- Props.new( h, path, parent )
136
- end
137
-
138
- def dump # for debugging
139
- puts "dump of >#{@path}<:"
140
- pp @hash
39
+ def self.banner
40
+ "props #{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
141
41
  end
142
42
 
143
-
144
- def fetch(key, default)
145
- value = get( key )
146
- value.nil? ? default : value
43
+ =begin
44
+ def self.root
45
+ "#{File.expand_path( File.dirname(File.dirname(__FILE__)) )}"
147
46
  end
47
+ =end
148
48
 
149
- def fetch_from_section(section, key, default)
150
- value = get_from_section( section, key )
151
- value.nil? ? default : value
152
- end
49
+ end # module ConfUtils
153
50
 
154
- def [](key) get( key ); end
155
-
156
- def get( key )
157
- value = @hash.fetch( key.to_s, nil )
158
- # if not found try lookup in parent hash
159
- (value.nil? && parent) ? parent.get(key) : value
160
- end
161
-
162
- def get_from_section( section, key )
163
- value = @hash.fetch( section.to_s, {} ).fetch( key.to_s, nil )
164
- # if not found try lookup in parent hash
165
- (value.nil? && parent) ? parent.get_from_section(section,key) : value
166
- end
167
51
 
168
- end # class Props
52
+ puts ConfUtils.banner # say hello
data/test/test_ini.rb CHANGED
@@ -6,8 +6,8 @@
6
6
 
7
7
  require 'helper'
8
8
 
9
- class TestIni < MiniTest::Unit::TestCase
10
9
 
10
+ class TestIni < MiniTest::Unit::TestCase
11
11
 
12
12
  def test_load
13
13
 
@@ -36,15 +36,15 @@ EOS
36
36
  hash = INI.load( text )
37
37
  pp hash
38
38
 
39
- assert( hash['key1'] == 'hello' )
40
- assert( hash['key2'] == 'hi!' )
41
- assert( hash['section1']['key3'] == 'salut' )
42
- assert( hash['section2']['key4'] == 'hola' )
43
- assert( hash['section2']['blank'] == '' )
44
- assert( hash['section2']['blank2'] == '' )
45
- assert( hash['http://example.com']['title'] == 'A rose is a rose is a rose, eh?' )
46
- assert( hash['http://example.com']['title2'] == 'A rose is a rose is a rose, eh?' )
47
- assert( hash['http://example.com']['title3'] == 'A rose is a rose is a rose, eh?' )
39
+ assert_equal( 'hello', hash['key1'] )
40
+ assert_equal( 'hi!', hash['key2'] )
41
+ assert_equal( 'salut', hash['section1']['key3'] )
42
+ assert_equal( 'hola', hash['section2']['key4'] )
43
+ assert_equal( '', hash['section2']['blank'] )
44
+ assert_equal( '', hash['section2']['blank2'] )
45
+ assert_equal( 'A rose is a rose is a rose, eh?', hash['http://example.com']['title'] )
46
+ assert_equal( 'A rose is a rose is a rose, eh?', hash['http://example.com']['title2'] )
47
+ assert_equal( 'A rose is a rose is a rose, eh?', hash['http://example.com']['title3'] )
48
48
  end
49
49
 
50
50
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: props
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
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-09-25 00:00:00.000000000 Z
12
+ date: 2013-09-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
16
- requirement: &83434100 !ruby/object:Gem::Requirement
16
+ requirement: &79922660 !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: *83434100
24
+ version_requirements: *79922660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hoe
27
- requirement: &83477370 !ruby/object:Gem::Requirement
27
+ requirement: &79922080 !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: *83477370
35
+ version_requirements: *79922080
36
36
  description: props - Manage Settings Hierachies (Commandline, User, Home, Defaults,
37
37
  etc.)
38
38
  email: webslideshow@googlegroups.com
@@ -46,6 +46,14 @@ files:
46
46
  - README.md
47
47
  - Rakefile
48
48
  - lib/props.rb
49
+ - lib/props/db.rb
50
+ - lib/props/db/deleter.rb
51
+ - lib/props/db/models.rb
52
+ - lib/props/db/schema.rb
53
+ - lib/props/env.rb
54
+ - lib/props/ini.rb
55
+ - lib/props/props.rb
56
+ - lib/props/version.rb
49
57
  - test/helper.rb
50
58
  - test/test_ini.rb
51
59
  - .gemtest