Class: ProjectInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/reap/projectinfo.rb

Overview

– NOTE At some point get past the use of the global variable perhaps? ++

Defined Under Namespace

Classes: HashBuilder

Constant Summary collapse

INFO_FILES =
[
  'ProjectInfo',
  'ReapFile',
  'projectinfo',
  'reapfile'
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ ProjectInfo

Returns a new instance of ProjectInfo.



61
62
63
64
65
66
67
68
# File 'lib/reap/projectinfo.rb', line 61

def initialize( &block )
  @info = {}
  @info_stream = nil

  define( &block ) if block_given?

  $PROJECT_INFO = self
end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



59
60
61
# File 'lib/reap/projectinfo.rb', line 59

def info
  @info
end

#info_dirObject (readonly)

Returns the value of attribute info_dir.



59
60
61
# File 'lib/reap/projectinfo.rb', line 59

def info_dir
  @info_dir
end

#info_fileObject (readonly)

Returns the value of attribute info_file.



59
60
61
# File 'lib/reap/projectinfo.rb', line 59

def info_file
  @info_file
end

#info_streamObject (readonly)

Returns the value of attribute info_stream.



59
60
61
# File 'lib/reap/projectinfo.rb', line 59

def info_stream
  @info_stream
end

Class Method Details

.findObject

Find project information file.



42
43
44
45
46
47
48
49
50
# File 'lib/reap/projectinfo.rb', line 42

def find
  info_dir, info_file = nil, nil
  Dir.ascend(Dir.pwd) do |info_dir|
    info_file = INFO_FILES.find{ |f| File.file?( File.join( info_dir, f ) ) }
    break if info_file
  end
  return nil unless info_file
  return File.join( info_dir, info_file )
end

.load(fpath = nil, report = false) ⇒ Object

Load the project information from a file. Generally no file needs to be specified; the file will be found by ascending up the current path until a default file name is found (eg. ProjectInfo or Reapfile).



31
32
33
34
35
36
37
38
# File 'lib/reap/projectinfo.rb', line 31

def load( fpath=nil, report=false )
  if fpath
    new.read( fpath, report )
  else
    fpath = find
    new.read( fpath, report )
  end
end

Instance Method Details

#[](name) ⇒ Object

Information fetch.



154
155
156
# File 'lib/reap/projectinfo.rb', line 154

def [](name)
  info[name]
end

#[]=(name, x) ⇒ Object

Information sore.



160
161
162
# File 'lib/reap/projectinfo.rb', line 160

def []=(name, x)
  info[name] = x
end

#defaultsObject

Project information defaults.



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/reap/projectinfo.rb', line 139

def defaults
  self['title']       ||= self['name'].capitalize
  self['series']      ||= '1'
  self['date']        ||= Time.now.strftime("%Y-%m-%d")
  self['author']      ||= "Anonymous"
  self['maintainer']  ||= self['author']
  self['arch']        ||= 'Any'
  self['license']     ||= 'Ruby/GPL'
  self['status']      ||= 'Beta'
  self['project']     ||= self['rubyforge'] ? self['rubyforge']['project'] : nil
  self['homepage']    ||= self['rubyforge'] ? self['rubyforge']['homepage'] : nil
end

#define(&block) ⇒ Object

Define project information programmatically.



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/reap/projectinfo.rb', line 72

def define( &block )
  return unless block

  @info = HashBuilder.new( &block  ).to_h
  @info_stream = @info.to_yaml

  #validate
  defaults

  self
end

#exists?Boolean

Project information file exists? (may need to change to info exists?)

Returns:

  • (Boolean)


124
125
126
# File 'lib/reap/projectinfo.rb', line 124

def exists?
  @info_file
end

#read(fpath, report = true) ⇒ Object

Load project information from YAML file.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/reap/projectinfo.rb', line 86

def read( fpath, report=true )
  return unless fpath

  @info_dir = File.dirname( fpath )
  @info_file = fpath #File.basename( fpath )
  @info_stream = File.read( fpath ).strip
  @info = YAML::load( info_stream ).traverse{ |k,v| [k.to_s.downcase, v] }

  Dir.chdir(@info_dir)
  if report
    puts "(in #{Dir.pwd})" #unless dir == Dir.pwd
  end

  #validate
  defaults

  self
end

#to_hObject

Information to hash.



166
167
168
# File 'lib/reap/projectinfo.rb', line 166

def to_h
  @info
end

#update(info = nil, &block) ⇒ Object

Update project information.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/reap/projectinfo.rb', line 107

def update( info=nil, &block )
  if info
    @info.update( info.traverse{ |k,v| [k.to_s.downcase, v] } )
  end
  if block_given?
    @info.update( HashBuilder.new( &block  ).to_h )
  end
  @info_stream = @info.to_yaml

  #validate
  defaults

  self
end

#validateObject

Validate project information.



130
131
132
133
134
135
# File 'lib/reap/projectinfo.rb', line 130

def validate #( *fields )
  val = true
  (puts "NAME is required in project information file."; val=false) unless info['name']
  (puts "VERSION is required in project information file."; val=false) unless info['version']
  exit -1 unless val
end