path-builder 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 521f007f861ad8a9ff775ba70b651d3d18a17e2c
4
- data.tar.gz: 469021e9173cb59262c58d2aac16dd76b69be95a
3
+ metadata.gz: 613e85da0491580f620c8c69daf53e24383b885b
4
+ data.tar.gz: d7b50a0630d624b80c629b2f745b976c15b8e485
5
5
  SHA512:
6
- metadata.gz: 20d6995c38f238cdc70c653bf3f6aa4afa46d70c90f4f297acfa99f7c93259c972082138097a1d486987379b384461bc0c825ad0b05b1bf57f8bad6328269481
7
- data.tar.gz: c54c9c562b57942cec723c81666827cca0ff939cc55a3344367e6104b7deb0ae31510985779eaf54366e7d5a43ef95cfe015245925df6c59c32a2548e7929020
6
+ metadata.gz: b854201fa13b353d5ccc6b0d8934fd48c468ebc70a935545f95588804b0e1c9b963913c08d626a09c9b617aa1c5ba2f5ef89bdb31247015af080a5c664a4fadd
7
+ data.tar.gz: 5e78759dca4cb736a870ced9d3c6e1bf9d5152d8ad026dc6b660f118d923c9edd2e4a447ac17da71812f20d0bc6d55e318f0641027952716a7a168ca644b204b
data/README.md CHANGED
@@ -28,39 +28,32 @@ require 'path-builder'
28
28
 
29
29
  Make a path:
30
30
  ```ruby
31
- path = PathBuilder.new
32
- path.api.moo.to_s #=> 'api/moo/'
31
+ PathBuilder.new.api.moo.to_s #=> 'api/moo/'
33
32
  ```
34
33
 
35
34
  Make it variable:
36
35
  ```ruby
37
- path = PathBuilder.new
38
- path.api.(:version).moo.to_s('v1') #=> 'api/v1/moo/'
36
+ PathBuilder.new.api.(:version).moo.to_s('v1') #=> 'api/v1/moo/'
39
37
  ```
40
38
 
41
39
  Why is that dot there? Because Ruby. Can we remove the dot? Yes, because Ruby:
42
40
  ```ruby
43
- path = PathBuilder.new
44
- path.api(:version).moo['v1'] #=> 'api/v1/moo/'
41
+ PathBuilder.new.api(:version).moo['v1'] #=> 'api/v1/moo/'
45
42
  ```
46
43
 
47
44
  Or use `#[]` instead of `#to_s`:
48
45
  ```ruby
49
- path = PathBuilder.new
50
- path.api.(:version).moo['v1'] #=> 'api/v1/moo/'
51
- ```
52
-
53
- Use it out of the box:
54
- ```ruby
55
46
  PathBuilder.new.api(:version).moo['v1'] #=> 'api/v1/moo/'
56
47
  ```
57
48
 
58
- Put in a url:
49
+ Put in a url with a string:
59
50
  ```ruby
60
- path = PathBuilder.new
61
- path.('http://example.com').api(:version).moo[] #=> 'http://example.com/api/v1/moo/'
51
+ PathBuilder.new.('http://example.com').api(:version).moo[] #=> 'http://example.com/api/v1/moo/'
52
+ # Remove the dot:
53
+ PathBuilder.new('http://example.com').api(:version).moo[] #=> 'http://example.com/api/v1/moo/'
62
54
  ```
63
55
 
56
+
64
57
  Reuse it:
65
58
  ```ruby
66
59
  ApiPath = PathBuilder.new.path.api(:version).save!
@@ -106,7 +99,13 @@ path - 6 #=> [:oh, :nose, :this, :shouldnt, :be, :here]
106
99
  path['1,2'] #=> #=> 'api/v1/user/1/comments/2/post/'
107
100
  ```
108
101
 
109
- Curious on how it works? Read the 88 line [source].
102
+ Or add it together:
103
+
104
+ ```ruby
105
+ (PathBuilder.new('http://example.com') + UsersPath.new)['1'] #=> 'http://example.com/api/v1/users/1/'
106
+ ```
107
+
108
+ Curious on how it works? Read the < 100 line [source].
110
109
 
111
110
  Have fun.
112
111
 
@@ -1,14 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "path/builder"
3
+ lib = File.expand_path '../../lib', __FILE__
4
+ $LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
5
5
 
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
6
+ load 'path-builder.rb'
8
7
 
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
8
+ def reload!
9
+ load 'path-builder.rb'
10
+ end
12
11
 
13
12
  require "irb"
14
13
  IRB.start
@@ -1,9 +1,14 @@
1
1
  class PathBuilder
2
- def initialize
2
+ def initialize(*args)
3
3
  @path = self.class.base_path.dup
4
+ @args = self.class.base_args.dup
5
+ self.call(*args)
4
6
  end
5
7
 
6
8
  class <<self
9
+ def version
10
+ "0.1.2"
11
+ end
7
12
  def base_path=(path)
8
13
  @base_path = path
9
14
  end
@@ -25,31 +30,31 @@ class PathBuilder
25
30
  @base_args ||= []
26
31
  end
27
32
  def break_on_empty
28
- @break_on_empty
33
+ @break_on_empty.nil? ? superclass.respond_to?(:break_on_empty) && superclass.break_on_empty : @break_on_empty
29
34
  end
30
35
  def break_on_empty=(v)
31
36
  @break_on_empty = v
32
37
  end
33
38
  end
34
39
 
35
- # Add a segment (string)
40
+ # Add a static segment (string)
36
41
  def method_missing(segment, *args)
37
42
  @path << segment.to_s
38
43
  @path += args.map{ |a| a.to_sym }
39
44
  self
40
45
  end
41
46
 
42
- # Add a variable segment (symbol)
43
- def call(segment)
44
- @path << segment
47
+ # Add a segment (symbol: variable, string: static)
48
+ def call(*segments)
49
+ @path += segments
45
50
  self
46
51
  end
47
52
 
48
53
  def [](*args, break_on_empty: self.class.break_on_empty)
49
- args += self.class.base_args.dup
54
+ @args += args
50
55
  @path.map do |segment|
51
56
  if segment.is_a? Symbol
52
- args.shift || (break_on_empty ? nil : segment)
57
+ @args.shift || (break_on_empty ? nil : segment)
53
58
  else
54
59
  segment
55
60
  end
@@ -61,10 +66,24 @@ class PathBuilder
61
66
  @path.pop number
62
67
  end
63
68
 
64
- def save!(break_on_empty: self.class.break_on_empty)
69
+ def +(other)
70
+ @path += other.path!
71
+ @args += other.args!
72
+ self
73
+ end
74
+
75
+ def path!
76
+ @path
77
+ end
78
+
79
+ def args!
80
+ @args
81
+ end
82
+
83
+ def save!(break_on_empty: nil)
65
84
  klass = Class.new self.class
66
85
  klass.base_path = @path
67
- klass.break_on_empty = break_on_empty
86
+ klass.break_on_empty = break_on_empty unless break_on_empty.nil?
68
87
  klass
69
88
  end
70
89
 
Binary file
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'path-builder/version'
4
+ require 'path-builder'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "path-builder"
8
- spec.version = PathBuilder::VERSION
8
+ spec.version = PathBuilder.version
9
9
  spec.authors = ["Ben"]
10
10
  spec.email = ["ben@bensites.com"]
11
11
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: path-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben
@@ -54,8 +54,8 @@ files:
54
54
  - bin/console
55
55
  - bin/setup
56
56
  - lib/path-builder.rb
57
- - lib/path-builder/version.rb
58
57
  - path-builder-0.1.0.gem
58
+ - path-builder-0.1.1.gem
59
59
  - path-builder.gemspec
60
60
  homepage: https://github.com/penne12/path-builder
61
61
  licenses:
@@ -1,3 +0,0 @@
1
- module PathBuilder
2
- VERSION = "0.1.1"
3
- end