class MonoFile
Constants
- NAMES
- RUBY_NAMES
- TXT_NAMES
- YML_NAMES
note: yaml always requires an extension
Public Class Methods
find()
click to toggle source
# File lib/monofile/monofile.rb, line 89 def self.find RUBY_NAMES.each do |name| return "./#{name}" if File.exist?( "./#{name}") end TXT_NAMES.each do |name| return "./#{name}" if File.exist?( "./#{name}") end YML_NAMES.each do |name| return "./#{name}" if File.exist?( "./#{name}") end nil ## no monofile found; return nil end
load( code )
click to toggle source
# File lib/monofile/monofile.rb, line 124 def self.load( code ) monofile = new monofile.load( code ) monofile end
load_file( path )
click to toggle source
# File lib/monofile/monofile.rb, line 130 def self.load_file( path ) ## keep (or add load_yaml to or such) - why? why not? code = File.open( path, 'r:utf-8') { |f| f.read } load( code ) end
new( obj={} )
click to toggle source
# File lib/monofile/monofile.rb, line 141 def initialize( obj={} ) ## todo/fix: change default to obj=[] @projects = [] ## puts "[debug] obj.class=#{obj.class.name}" add( obj ) end
read( path )
click to toggle source
# File lib/monofile/monofile.rb, line 106 def self.read( path ) txt = File.open( path, 'r:utf-8') { |f| f.read } ## check for yml or yaml extension; ## or for txt extension; otherwise assume ruby extname = File.extname( path ).downcase if ['.yml', '.yaml'].include?( extname ) hash = YAML.load( txt ) new( hash ) elsif ['.txt'].include?( extname ) new( txt ) else ## assume ruby code (as text in string) new().load( txt ) end end
Public Instance Methods
add( obj )
click to toggle source
# File lib/monofile/monofile.rb, line 155 def add( obj ) ## todo/check: check for proc too! and use load( proc/block ) - possible? if obj.is_a?( String ) puts "sorry add String - to be done!!!" exit 1 elsif obj.is_a?( Array ) puts "sorry add Array- to be done!!!" exit 1 elsif obj.is_a?( Hash ) add_hash( obj ) else ## assume text (evaluate/parse) puts "sorry add Text - to be done!!!" exit 1 end self ## note: return self for chaining end
add_hash( hash )
click to toggle source
# File lib/monofile/monofile.rb, line 173 def add_hash( hash ) hash.each do |org_with_counter, names| ## remove optional number from key e.g. ## mrhydescripts (3) => mrhydescripts ## footballjs (4) => footballjs ## etc. ## todo/check: warn about duplicates or such - why? why not? org = org_with_counter.sub( /\([0-9]+\)/, '' ).strip.to_s names.each do |name| @projects << Project.new( org, name ) end end self ## note: return self for chaining end
each( &block )
click to toggle source
# File lib/monofile/monofile.rb, line 195 def each( &block ) ## puts "[debug] arity: #{block.arity}" ## for backwards compatibility support "old" each with/by org & names ## add deprecated warnings and use to_h or such - why? why not? if block.arity == 2 puts "!! DEPRECATED - please, use Monofile#to_h or Monofile.each {|proj| ...}" to_h.each do |org, names| block.call( org, names ) end else ## assume just regular @projects.each do |project| block.call( project ) end end end
each_with_index( &block )
click to toggle source
# File lib/monofile/monofile.rb, line 213 def each_with_index( &block ) @projects.each_with_index do |project,i| block.call( project, i ) end end
load( code )
click to toggle source
# File lib/monofile/monofile.rb, line 148 def load( code ) ## note: code is text as a string builder = Builder.new( self ) builder.instance_eval( code ) self ## note: for chaining always return self end
projects()
click to toggle source
attr readers
# File lib/monofile/monofile.rb, line 137 def projects() @projects; end
size()
click to toggle source
# File lib/monofile/monofile.rb, line 138 def size() @projects.size; end
to_a()
click to toggle source
# File lib/monofile/monofile.rb, line 242 def to_a ## todo/check: ## - sort all entries a-z - why? why not? ## - always start name with @ marker - why? why not? @projects.map {|project| project.to_s } end
to_h()
click to toggle source
for backward compat(ibility) add a hash in the form e.g:
geraldb:
-
austria
-
catalog
-
geraldb.github.io
openfootball:
-
england
-
europe
-
south-america
-
world-cup
# File lib/monofile/monofile.rb, line 233 def to_h h = {} @projects.each do |project| h[ project.org ] ||= [] h[ project.org ] << project.name end h end