Class: MonoFile

Inherits:
Object
  • Object
show all
Defined in:
lib/mono/base.rb

Class Method Summary collapse

Class Method Details

.exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/mono/base.rb', line 125

def self.exist?( path )
  File.exist?( real_path( path ))
end

.norm_name(path) ⇒ Object

Raises:

  • (ArgumentError)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mono/base.rb', line 103

def self.norm_name( path )
  #  turn

  #  - @yorobot/stage/one

  #  - one@yorobot/stage

  #  - stage/one@yorobot

  #      => into

  #  - yorobot/stage/one



  parts = path.split( '@' )
  raise ArgumentError, "no (required) @ found in name; got >#{path}<"               if parts.size == 1
  raise ArgumentError, "too many @ found (#{parts.size-1}) in name; got >#{path}<"  if parts.size > 2

  norm_name = String.new('')
  norm_name << parts[1]  ## add orgs path first

  if parts[0].length > 0   ## has leading repo name (w/ optional path)

    norm_name << '/'
    norm_name << parts[0]
  end
  norm_name
end

.open(path, mode = 'r:utf-8', &block) ⇒ Object

path always relative to Mono.root

todo/fix:  use File.expand_path( path, Mono.root ) - why? why not?
 or always enfore "absolut" path e.g. do NOT allow ../ or ./ or such


133
134
135
136
137
138
139
140
141
142
143
# File 'lib/mono/base.rb', line 133

def self.open( path, mode='r:utf-8', &block )
   full_path = real_path( path )
   ## make sure path exists if we open for writing/appending - why? why not?

   if mode[0] == 'w' || mode[0] == 'a'
    FileUtils.mkdir_p( File.dirname( full_path ) )  ## make sure path exists

   end

   File.open( full_path, mode ) do |file|
     block.call( file )
   end
end

.read_utf8(path) ⇒ Object



145
146
147
# File 'lib/mono/base.rb', line 145

def self.read_utf8( path )
   open( path, 'r:utf-8') { |file| file.read }
end

.real_path(path) ⇒ Object

e.g. openfootball/austria etc.

expand to to "real" absolute path

todo/check: assert name must be orgname,username/reponame



99
100
101
# File 'lib/mono/base.rb', line 99

def self.real_path( path )
  "#{Mono.root}/#{norm_name( path )}"
end