pion 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pion.rb +58 -21
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d488fdce446400ab2ebc04a1a49467b2e4581217
4
- data.tar.gz: 202de5134d57fb642ed01c2a1decf751d8c26572
3
+ metadata.gz: 84a7550fcbc0110049a63b42b3fc1c715c11660d
4
+ data.tar.gz: e726c4dc3f929192d4046749ce3855fa708d6427
5
5
  SHA512:
6
- metadata.gz: 20b0985951ee2799029e3bcb78e386cdfd01805c0981799ede57eaa62896cb965875c6570f1059677eb6b3f212d2c10a7ffde69ee2ac76d00c7f5ddeff05f5f7
7
- data.tar.gz: 2d75ffb4cb653ac648fbf66d02618e28efba5b4624eec6376c1ed2f7d7462a3f350eb24d03fbdec427f6763dbd3a6ab79396feb789f5ae9faf571f2a0759411c
6
+ metadata.gz: d622f4258b7f3cde7451d896863e2aba88de7e5c19306b92698310649ba790b02bbf9d2583d52e32d1e91fad248ef308edb228de8c798ae7992ad119d2bf7294
7
+ data.tar.gz: c434e48b6c03c92704895aba3d5b5fc46a9a40c85612c5b1edc81f2bfee318acb0e116e309bf58bf8105b3a31da797c5fe1dee40b4a689ab12f06bff2a835c41
@@ -1,23 +1,28 @@
1
+ # Pion!
1
2
  class Pion
3
+ # Attributes
4
+ attr_accessor :options
5
+
2
6
  # Version number for Pion
3
- VERSION = "0.2.0"
7
+ VERSION = "0.3.0"
4
8
 
5
9
  # Initialize a new Pion object
6
- def initialize(directory, extension = "html", separator = "/")
7
- raise ArgumentError, "Extension name must be a string." unless extension.is_a? String
8
- raise ArgumentError, "Directory separator must be a string." unless separator.is_a? String
10
+ def initialize(options)
11
+ @options = method(:symbolize).call options
12
+
13
+ fail ArgumentError, "No directory given!" unless options.key? :directory
9
14
 
10
- @directory = method(:get_directory).call directory
11
- @extension = extension.gsub /\A\./, ""
12
- @separator = separator
15
+ method(:set_options).call
13
16
  end
14
17
 
15
18
  # Generate urls from files
16
19
  def generate
17
20
  urls = []
18
21
 
19
- Dir.glob "#{@directory}/**/*.#{@extension}" do |file|
20
- urls << {file: file, url: method(:get_url).call(file)} unless file == "." or file == ".."
22
+ Dir.glob "#{options[:directory]}/**/*.#{options[:extension]}" do |file|
23
+ hash = { file: file, url: method(:get_url).call(file) }
24
+
25
+ urls << hash unless file == "." || file == ".."
21
26
  end
22
27
 
23
28
  urls
@@ -25,20 +30,52 @@ class Pion
25
30
 
26
31
  private
27
32
 
28
- # Get directory or raise errors if it's faulty
29
- def get_directory(directory)
30
- return directory if directory.is_a? String and File.directory? directory
31
-
32
- raise ArgumentError, "Directory name must be a string." unless directory.is_a? String
33
- raise ArgumentError, "Directory must be a valid directory." unless File.directory? directory
34
- end
35
-
36
33
  # Convert filename to clean url
37
34
  def get_url(file)
38
- url = file.gsub /\A#{@directory}/, ""
39
- url = url.gsub /(\/index)?\.#{@extension}\z/, ""
40
- url = url.gsub /#{Regexp.escape(@separator)}/, "/"
35
+ url = file.gsub(/\A#{options[:directory]}/, "")
36
+ url = url.gsub(/(\/index)?\.#{options[:extension]}\z/, "")
37
+ url = url.gsub(/#{Regexp.escape(options[:separator])}/, "/")
41
38
 
42
39
  url == "" ? "/" : url
43
40
  end
44
- end
41
+
42
+ # Set directory or fail errors if it's faulty
43
+ def set_directory
44
+ dir = options[:directory]
45
+
46
+ return if dir.is_a?(String) && File.directory?(dir)
47
+
48
+ if !dir.is_a? String
49
+ fail ArgumentError, "Directory must be a string."
50
+ elsif !File.directory? dir
51
+ fail ArgumentError, "Directory must be a valid directory."
52
+ end
53
+ end
54
+
55
+ def set_default(type, default)
56
+ options[type] = default unless options.key? type
57
+
58
+ name = type.to_s.capitalize
59
+
60
+ unless options[type].is_a? String
61
+ fail ArgumentError, "#{name} must be a string"
62
+ end
63
+ end
64
+
65
+ def set_options
66
+ method(:set_directory).call
67
+
68
+ method(:set_default).call :extension, "html"
69
+ method(:set_default).call :separator, "/"
70
+ end
71
+
72
+ def symbolize(hash)
73
+ fail ArgumentError, "No Hash given!" unless hash.is_a? Hash
74
+
75
+ hash.reduce({}) do |memo, (key, value)|
76
+ memo[key.to_sym] = value
77
+
78
+ memo
79
+ end
80
+ end
81
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Palmér