drain 0.3.0 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +7 -10
- data/lib/dr/base/delegate.rb +28 -0
- data/lib/dr/base/graph.rb +14 -5
- data/lib/dr/base/uri.rb +3 -1
- data/lib/dr/base/utils.rb +31 -4
- data/lib/dr/parse/simple_keywords.rb +1 -1
- data/lib/dr/parse/time_parse.rb +28 -22
- data/lib/dr/ruby_ext/core_modules.rb +5 -3
- data/lib/dr/version.rb +1 -1
- data/test/test_core_ext.rb +2 -2
- data/test/test_date_parse.rb +4 -0
- data/test/test_time_parse.rb +43 -0
- data/test/test_uri.rb +35 -0
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 879680e62956b55119d14b0c5c334c1836c26b8922db6eaf12539c21fd1ccd76
|
4
|
+
data.tar.gz: 00134a0c8b8cb4e79aa8e2be089244864e46084c338917890caed1ae1e9abfc7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a9f6e1b457a54666f1fc2441d2104322811fc8c87f1857079092fdea94c1d819aadd63ee839b6a55c1ecb2c90a4260f53b3f6c3ecfd9e98a017a85c698e2107
|
7
|
+
data.tar.gz: c01a56879bf4e5558f461ad5c1a35582db768d6d62421cb62142061774a3bfbf8e8c3460fbdee6c0c26b816d7effd864b1cd0ab3be01bf3b541b4d2e6dc093bd
|
data/Rakefile
CHANGED
@@ -1,15 +1,5 @@
|
|
1
1
|
require 'rake'
|
2
2
|
|
3
|
-
begin
|
4
|
-
require 'rubygems/tasks'
|
5
|
-
Gem::Tasks.new(sign: {checksum: true, pgp: true},
|
6
|
-
scm: {status: true}) do |tasks|
|
7
|
-
tasks.console.command = 'pry'
|
8
|
-
end
|
9
|
-
rescue LoadError => e
|
10
|
-
warn e.message
|
11
|
-
end
|
12
|
-
|
13
3
|
require 'rake/testtask'
|
14
4
|
Rake::TestTask.new do |test|
|
15
5
|
test.libs << 'test'
|
@@ -27,3 +17,10 @@ rescue LoadError => e
|
|
27
17
|
end
|
28
18
|
task :doc => :yard
|
29
19
|
|
20
|
+
begin
|
21
|
+
require 'dr/rake_gems'
|
22
|
+
Gem::MyTasks.new
|
23
|
+
rescue LoadError => e
|
24
|
+
warn e.message
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module DR
|
2
|
+
module Delegator
|
3
|
+
extend self
|
4
|
+
def delegate_h(klass, var)
|
5
|
+
require 'forwardable'
|
6
|
+
# put in a Module so that they are easier to distinguish from the
|
7
|
+
# 'real' functions
|
8
|
+
m=Module.new do
|
9
|
+
extend(Forwardable)
|
10
|
+
methods=[:[], :[]=, :any?, :assoc, :clear, :compact, :compact!, :delete, :delete_if, :dig, :each, :each_key, :each_pair, :each_value, :empty?, :fetch, :fetch_values, :has_key?, :has_value?, :include?, :index, :invert, :keep_if, :key, :key?, :keys, :length, :member?, :merge, :merge!, :rassoc, :reject, :reject!, :select, :select!, :shift, :size, :slice, :store, :to_a, :to_h, :to_s, :transform_keys, :transform_keys!, :transform_values, :transform_values!, :update, :value?, :values, :values_at]
|
11
|
+
include(Enumerable)
|
12
|
+
def_delegators var, *methods
|
13
|
+
end
|
14
|
+
klass.include(m)
|
15
|
+
end
|
16
|
+
|
17
|
+
def access_methods(klass, var, *methods)
|
18
|
+
methods.each do |k|
|
19
|
+
klass.define_method k do
|
20
|
+
instance_variable_get(var)[k]
|
21
|
+
end
|
22
|
+
klass.defined_method :"#{k}=" do |*args|
|
23
|
+
instance_variable_get(var).send(:[]=, k, *args)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/dr/base/graph.rb
CHANGED
@@ -7,7 +7,7 @@ module DR
|
|
7
7
|
attr_reader :graph
|
8
8
|
attr_accessor :name, :attributes, :parents, :children
|
9
9
|
def initialize(name, attributes: {}, graph: nil)
|
10
|
-
@name = name
|
10
|
+
@name = name
|
11
11
|
@children = []
|
12
12
|
@parents = []
|
13
13
|
@attributes = attributes
|
@@ -78,7 +78,7 @@ module DR
|
|
78
78
|
|
79
79
|
STEP = 4
|
80
80
|
def to_s(show_attr: true)
|
81
|
-
@name + (show_attr && ! attributes.empty? ? " #{attributes}" : "")
|
81
|
+
@name.to_s + (show_attr && ! attributes.empty? ? " #{attributes}" : "")
|
82
82
|
end
|
83
83
|
def inspect
|
84
84
|
"#{self.class}: #{to_s(show_attr: true)}"+(graph.nil? ? "" : " (#{graph})")
|
@@ -107,10 +107,12 @@ module DR
|
|
107
107
|
class Graph
|
108
108
|
attr_accessor :nodes
|
109
109
|
include Enumerable
|
110
|
+
# note: passing a graph won't work
|
110
111
|
def initialize(*nodes, attributes: {}, infos: nil)
|
111
112
|
@nodes=[]
|
112
113
|
# a node can be a Hash or a Node
|
113
|
-
|
114
|
+
# so nodes really is a list of subgraphs
|
115
|
+
build(*nodes, attributes: attributes, infos: infos)
|
114
116
|
end
|
115
117
|
def each(&b)
|
116
118
|
@nodes.each(&b)
|
@@ -123,6 +125,10 @@ module DR
|
|
123
125
|
def to_a
|
124
126
|
return @nodes
|
125
127
|
end
|
128
|
+
def names
|
129
|
+
@nodes.map(&:name)
|
130
|
+
end
|
131
|
+
|
126
132
|
def to_hash(methods: [:children,:parents,:attributes], compact: true, recursive: true)
|
127
133
|
require 'dr/base/converter'
|
128
134
|
Converter.to_hash(@nodes, methods: methods, recursive: recursive, compact: compact)
|
@@ -142,7 +148,7 @@ module DR
|
|
142
148
|
h=to_hash(methods: [:children])
|
143
149
|
Hash[h.map {|k,v| [k.name, v.map(&:name)]}]
|
144
150
|
end
|
145
|
-
alias to_children to_h
|
151
|
+
#alias to_children to_h
|
146
152
|
|
147
153
|
def to_children
|
148
154
|
require 'dr/base/converter'
|
@@ -150,8 +156,11 @@ module DR
|
|
150
156
|
end
|
151
157
|
|
152
158
|
def inspect
|
153
|
-
"#{self.class}: #{map {|x| x.to_s}}"
|
159
|
+
"#{self.class}: #{map {|x| x.to_s(show_attr: false)}}"
|
154
160
|
end
|
161
|
+
# def to_s
|
162
|
+
# "#{self.class}: #{map {|x| x.to_s(show_attr: false)}}"
|
163
|
+
# end
|
155
164
|
|
156
165
|
#construct a node (without edges)
|
157
166
|
def new_node(node,**attributes)
|
data/lib/dr/base/uri.rb
CHANGED
data/lib/dr/base/utils.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module DR
|
2
2
|
module Utils
|
3
3
|
extend self
|
4
|
-
def pretty_print(string, format: nil, pretty:
|
4
|
+
def pretty_print(string, format: nil, pretty: nil)
|
5
5
|
case format.to_s
|
6
6
|
when "json"
|
7
7
|
require 'json'
|
@@ -10,14 +10,23 @@ module DR
|
|
10
10
|
require "yaml"
|
11
11
|
return pretty_print(string.to_yaml, pretty: pretty)
|
12
12
|
end
|
13
|
-
if pretty
|
13
|
+
pretty = "color" if pretty == nil or pretty == true #default
|
14
|
+
case pretty.to_s
|
15
|
+
when "ap"
|
14
16
|
begin
|
15
17
|
require 'ap'
|
16
18
|
ap string
|
17
19
|
rescue LoadError,NameError
|
18
|
-
pretty_print(string,pretty:
|
20
|
+
pretty_print(string,pretty: :pp)
|
19
21
|
end
|
20
|
-
|
22
|
+
when "color", "pp_color"
|
23
|
+
begin
|
24
|
+
require 'pry'
|
25
|
+
Pry::ColorPrinter.pp string
|
26
|
+
rescue LoadError,NameError
|
27
|
+
pretty_print(string,pretty: :pp)
|
28
|
+
end
|
29
|
+
when "pp"
|
21
30
|
require 'pp'
|
22
31
|
pp string
|
23
32
|
else
|
@@ -49,4 +58,22 @@ module DR
|
|
49
58
|
end
|
50
59
|
end
|
51
60
|
end
|
61
|
+
|
62
|
+
# Include this to get less output from pp
|
63
|
+
module PPHelper
|
64
|
+
# less verbose for pretty_print
|
65
|
+
def pretty_print(pp)
|
66
|
+
info = respond_to?(:to_pp) ? to_pp : to_s
|
67
|
+
pp.object_address_group(self) { pp.text " "+info }
|
68
|
+
end
|
69
|
+
|
70
|
+
#since we hide the pp value of self, allow to inspect it
|
71
|
+
def export
|
72
|
+
r={}
|
73
|
+
instance_variables.sort.each do |var|
|
74
|
+
r[var]=instance_variable_get(var)
|
75
|
+
end
|
76
|
+
r
|
77
|
+
end
|
78
|
+
end
|
52
79
|
end
|
@@ -20,7 +20,7 @@ module DR
|
|
20
20
|
edelim= delims[1] || bdelim
|
21
21
|
keywords=@keywords.keys
|
22
22
|
keywords_r="(?:"+keywords.map {|k| "(?:"+k+")"}.join("|")+")"
|
23
|
-
reg = %r{(?<kw>#{keywords_r})(?<re>#{'\\'+bdelim}(?:(?>[^#{'\\'+bdelim}#{'\\'+edelim}]+)|\g<re>)*#{'\\'+edelim})}
|
23
|
+
reg = %r{(?<kw>#{keywords_r})(?<re>#{'\\'+bdelim}(?:(?>[^#{'\\'+bdelim}#{edelim == bdelim ? '' : '\\'+edelim}]+)|\g<re>)*#{'\\'+edelim})}
|
24
24
|
if (m=reg.match(msg))
|
25
25
|
arg=m[:re][1...m[:re].length-1]
|
26
26
|
arg=parse(arg, **opts)
|
data/lib/dr/parse/time_parse.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'chronic'
|
2
2
|
require 'chronic_duration'
|
3
|
-
require 'active_support/time'
|
3
|
+
# require 'active_support/time'
|
4
4
|
|
5
5
|
module DR
|
6
6
|
module TimeParse
|
@@ -8,17 +8,17 @@ module DR
|
|
8
8
|
def time_to_day_range(t)
|
9
9
|
return Chronic.parse(t.to_date, guess:false)
|
10
10
|
end
|
11
|
-
def parse(s, opt
|
11
|
+
def parse(s, **opt)
|
12
12
|
return s if Date===s or Time===s
|
13
13
|
|
14
|
-
|
15
|
-
first
|
16
|
-
second
|
14
|
+
!opt[:norange] and s.match(/(.*)\.\.(.*)/) do |m|
|
15
|
+
first=m[1]
|
16
|
+
second=m[2]
|
17
17
|
opt[:norange]=true
|
18
18
|
return Chronic::Span.new(self.parse(first, opt),self.parse(second,opt))
|
19
19
|
end
|
20
20
|
|
21
|
-
if
|
21
|
+
if s.match(/\A[[:space:]]*\z/) # blank
|
22
22
|
t=Time.now
|
23
23
|
elsif s[0] =~ /[+-]/
|
24
24
|
#if s=+3.years-1.minutes
|
@@ -53,19 +53,25 @@ module DR
|
|
53
53
|
|
54
54
|
end
|
55
55
|
end
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
56
|
+
|
57
|
+
=begin Examples:
|
58
|
+
|
59
|
+
DR::TimeParse.parse("+100..tomorrow") #first: +100, second: tomorrow
|
60
|
+
=> 2014-08-22 11:20:31 +0200..2014-08-23 12:00:00 +0200
|
61
|
+
|
62
|
+
DR::TimeParse.parse("now..in seven days") #first: now, second: in seven days
|
63
|
+
=> 2014-08-22 11:20:25 +0200..2014-08-29 11:20:25 +0200
|
64
|
+
|
65
|
+
DR::TimeParse.parse("today")
|
66
|
+
=> 2014-08-22 17:30:00 +0200
|
67
|
+
|
68
|
+
DR::TimeParse.parse("today",range: true)
|
69
|
+
=> 2014-08-22 11:00:00 +0200..2014-08-23 00:00:00 +0200
|
70
|
+
|
71
|
+
DR::TimeParse.parse("-3 years 2 minutes") #-94672920
|
72
|
+
=> 2011-08-22 20:01:34 +0200
|
73
|
+
|
74
|
+
require 'active_support/time'
|
75
|
+
DR::TimeParse.parse("+3.years+2.days")
|
76
|
+
=> 2017-08-24 14:04:08 +0200
|
77
|
+
=end
|
@@ -2,9 +2,9 @@ module DR
|
|
2
2
|
module CoreExt
|
3
3
|
#[Hash, Array].each {|m| m.include(Enumerable)} #to reinclude
|
4
4
|
module Enumerable
|
5
|
-
#Ex: [1,2,3,4].
|
5
|
+
#Ex: [1,2,3,4].classify({odd: [1,3], default: :even})
|
6
6
|
#=> {:odd=>[1, 3], :even=>[2, 4]}
|
7
|
-
def
|
7
|
+
def classify(h)
|
8
8
|
invh=h.inverse
|
9
9
|
default=h[:default]
|
10
10
|
r={}
|
@@ -36,7 +36,7 @@ module DR
|
|
36
36
|
|
37
37
|
# Same as +deep_merge+, but modifies +self+.
|
38
38
|
def deep_merge!(other_hash, append: :auto, &block)
|
39
|
-
return unless other_hash
|
39
|
+
return self unless other_hash
|
40
40
|
other_hash.each_pair do |k,v|
|
41
41
|
tv = self[k]
|
42
42
|
case
|
@@ -301,6 +301,8 @@ module DR
|
|
301
301
|
end
|
302
302
|
|
303
303
|
module CoreRef
|
304
|
+
# warning, this only works for methods that don't need to call other
|
305
|
+
# refined methods
|
304
306
|
CoreExt.constants.select {|c| const_get("::#{c}").is_a?(Class)}.each do |c|
|
305
307
|
refine const_get("::#{c}") do
|
306
308
|
include CoreExt.const_get(c)
|
data/lib/dr/version.rb
CHANGED
data/test/test_core_ext.rb
CHANGED
@@ -10,8 +10,8 @@ module TestCoreExt
|
|
10
10
|
require 'dr/ruby_ext/core_ext'
|
11
11
|
describe DR::CoreExt do
|
12
12
|
describe Enumerable do
|
13
|
-
it "Can
|
14
|
-
[1,2,3,4].
|
13
|
+
it "Can classify enumerable" do
|
14
|
+
[1,2,3,4].classify({odd: [1,3], default: :even}).must_equal({:odd=>[1, 3], :even=>[2, 4]})
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
data/test/test_date_parse.rb
CHANGED
@@ -3,9 +3,13 @@ require 'dr/parse/date_parse'
|
|
3
3
|
|
4
4
|
describe DR::DateRange do
|
5
5
|
before do
|
6
|
+
@tz=ENV['TZ']
|
6
7
|
ENV['TZ']='GMT'
|
7
8
|
@daterange=DR::DateRange.parse("2014-01-02 -> 2014-01-03, 2014-01-05, 2014-02 -> :now")
|
8
9
|
end
|
10
|
+
after do
|
11
|
+
ENV['TZ']=@tz
|
12
|
+
end
|
9
13
|
|
10
14
|
it "Can parse dates" do
|
11
15
|
@daterange.d.must_equal [["2014-01-02", "2014-01-03"], ["2014-01-05"], ["2014-02", :now]]
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'dr/parse/time_parse'
|
3
|
+
|
4
|
+
describe DR::TimeParse do
|
5
|
+
before do
|
6
|
+
@tz=ENV['TZ']
|
7
|
+
ENV['TZ']='GMT'
|
8
|
+
class << Time
|
9
|
+
alias _original_now now
|
10
|
+
def now
|
11
|
+
Time.new(2000)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
after do
|
16
|
+
ENV['TZ']=@tz
|
17
|
+
class << Time
|
18
|
+
alias now _original_now
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "Can parse a range" do
|
23
|
+
DR::TimeParse.parse("+100..tomorrow").must_equal(
|
24
|
+
Time.parse("2000-01-01 00:01:40")..Time.parse("2000-01-02 12:00:00")
|
25
|
+
)
|
26
|
+
DR::TimeParse.parse("now..in seven days").must_equal(
|
27
|
+
Time.parse("2000-01-01 00:00:00")..Time.parse("2000-01-08 00:00:00")
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "Can parse a date" do
|
32
|
+
DR::TimeParse.parse("today").must_equal(Time.parse("2000-01-01-12:00:00"))
|
33
|
+
end
|
34
|
+
|
35
|
+
it "Can put a date in a range" do
|
36
|
+
DR::TimeParse.parse("today", range: true).must_equal(
|
37
|
+
Time.parse("2000-01-01-00:00:00")..Time.parse("2000-01-02-00:00:00")
|
38
|
+
)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
#with active_support: DR::TimeParse.parse("-3 years 2 minutes")
|
43
|
+
#=> 2011-08-22 20:01:34 +0200
|
data/test/test_uri.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'dr/base/uri'
|
3
|
+
|
4
|
+
describe DR::URIWrapper do
|
5
|
+
before do
|
6
|
+
@uri=DR::URIWrapper.new(URI.escape("http://ploum:secret@plam:443/foo bar"))
|
7
|
+
end
|
8
|
+
it "Wraps an uri element" do
|
9
|
+
@uri.scheme.must_equal "http"
|
10
|
+
end
|
11
|
+
it "Auto escapes attribute" do
|
12
|
+
@uri.path.must_equal "/foo bar"
|
13
|
+
end
|
14
|
+
it "Auto escape setting elements" do
|
15
|
+
@uri.user="ploum plam"
|
16
|
+
@uri.user.must_equal "ploum plam"
|
17
|
+
end
|
18
|
+
it "Can convert to a hash" do
|
19
|
+
@uri.to_h[:user].must_equal("ploum")
|
20
|
+
end
|
21
|
+
it "Can convert to json" do
|
22
|
+
require 'json'
|
23
|
+
@uri.to_json.must_equal("{\"uri\":\"http://ploum:secret@plam:443/foo%20bar\",\"scheme\":\"http\",\"userinfo\":\"ploum:secret\",\"host\":\"plam\",\"port\":443,\"path\":\"/foo bar\",\"user\":\"ploum\",\"password\":\"secret\"}")
|
24
|
+
end
|
25
|
+
it "Can remove password" do
|
26
|
+
@uri.to_public.must_equal("http://ploum@plam:443/foo%20bar")
|
27
|
+
end
|
28
|
+
it "Can be merged" do
|
29
|
+
@uri.soft_merge("foo://plim@").to_s.must_equal("foo://plim:secret@plam:443/foo%20bar")
|
30
|
+
end
|
31
|
+
it "Can be reverse merged" do
|
32
|
+
DR::URIWrapper.parse("//user@server").reverse_merge(@uri).to_s.must_equal("http://user:secret@server:443/foo%20bar")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: drain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.4'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Damien Robert
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -68,7 +68,7 @@ dependencies:
|
|
68
68
|
version: '0.8'
|
69
69
|
description: 'Drain is a small set of libraries that I use in my other gems.
|
70
70
|
|
71
|
-
'
|
71
|
+
'
|
72
72
|
email: Damien.Olivier.Robert+gems@gmail.com
|
73
73
|
executables: []
|
74
74
|
extensions: []
|
@@ -91,6 +91,7 @@ files:
|
|
91
91
|
- lib/dr/base.rb
|
92
92
|
- lib/dr/base/bool.rb
|
93
93
|
- lib/dr/base/converter.rb
|
94
|
+
- lib/dr/base/delegate.rb
|
94
95
|
- lib/dr/base/encoding.rb
|
95
96
|
- lib/dr/base/eruby.rb
|
96
97
|
- lib/dr/base/functional.rb
|
@@ -120,6 +121,8 @@ files:
|
|
120
121
|
- test/test_meta.rb
|
121
122
|
- test/test_simple_keywords.rb
|
122
123
|
- test/test_simple_parser.rb
|
124
|
+
- test/test_time_parse.rb
|
125
|
+
- test/test_uri.rb
|
123
126
|
homepage: https://github.com/DamienRobert/drain#readme
|
124
127
|
licenses:
|
125
128
|
- MIT
|
@@ -140,8 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
143
|
- !ruby/object:Gem::Version
|
141
144
|
version: '0'
|
142
145
|
requirements: []
|
143
|
-
|
144
|
-
rubygems_version: 2.7.7
|
146
|
+
rubygems_version: 3.0.6
|
145
147
|
signing_key:
|
146
148
|
specification_version: 4
|
147
149
|
summary: Use a drain for a dryer ruby!
|