Class: CloudFile::CloudUri::Dsl

Inherits:
Object
  • Object
show all
Includes:
FromHash
Defined in:
lib/cloud_file/cloud_uri/dsl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#formatObject

Returns the value of attribute format.



5
6
7
# File 'lib/cloud_file/cloud_uri/dsl.rb', line 5

def format
  @format
end

#service_classObject

Returns the value of attribute service_class.



5
6
7
# File 'lib/cloud_file/cloud_uri/dsl.rb', line 5

def service_class
  @service_class
end

Class Method Details

.regexObject



7
8
9
# File 'lib/cloud_file/cloud_uri/dsl.rb', line 7

def self.regex
  /^(.+):\/\/([^\/]+)((?:\/[^\/]+)*)/
end

Instance Method Details

#matches(str) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/cloud_file/cloud_uri/dsl.rb', line 11

def matches(str)
  raise "no matches" unless str =~ self.class.regex

  res = [$1,$2]
  if $3.present?
    res += $3[1..-1].split("/")
  end
  res
end

#paramsObject



21
22
23
# File 'lib/cloud_file/cloud_uri/dsl.rb', line 21

def params
  [':provider'] + format.split("/")
end

#parse(str) ⇒ Object



53
54
55
# File 'lib/cloud_file/cloud_uri/dsl.rb', line 53

def parse(str)
  result(str)
end

#result(str) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cloud_file/cloud_uri/dsl.rb', line 25

def result(str)
  matches = matches(str)
  res = {}
  add = lambda do |k,i|
    val = matches[i]
    if val.kind_of?(Array)
      raise "bad" if val.empty?
      val = val.join("/")
    else
      raise "bad" unless val.present?
    end
    res[k] = val
  end

  params.each_with_index do |param,i|
    if param =~ /^:/
      k = param[1..-1]
      add[k,i]
    elsif param =~ /^*:/
      k = param[2..-1]
      add[k,(i..99)]
    else
      # do nothing
    end
  end

  HashWithIndifferentAccess.new(res)
end

#run!Object



57
58
59
# File 'lib/cloud_file/cloud_uri/dsl.rb', line 57

def run!
  service_class.uri_parser = self
end