class Simp::Rake::Fixtures

Public Class Methods

new( dir ) click to toggle source
# File lib/simp/rake/fixtures.rb, line 6
def initialize( dir )
   @base_dir = dir
   ###::CLEAN.include( '.fixtures.yml.local' )
   define
end

Public Instance Methods

define() click to toggle source
# File lib/simp/rake/fixtures.rb, line 12
def define
  namespace :fixtures do
    def flatten_fixtures_hash(_f)
      _f
        .map{|k,v| v.values}
        .flatten
        .reduce({}){|h,pairs|
          pairs.each{|k,v|
            (h[k] ||= []) << v
          }; h
        }
        .keys
        .uniq
        .sort
    end

    def fixtures_yml_local(_f)
      _f_m  = flatten_fixtures_hash(_f)

      _s = Hash[_f_m.map{|k|
        v= _f['fixtures']['repositories'].key?(k) ? "#\{source_dir\}/../#{k}": _f['fixtures']['symlinks'].fetch(k)
        [k, v ]}
      ]

      {'fixtures'=> {'symlinks'=> _s }}
    end

    desc 'generate .fixtures.yml.local formm the entries in .fixtures.yml'
    task :generate do
      pwd = File.expand_path(@base_dir)
      _f  = YAML.load_file(File.join(pwd,'.fixtures.yml'))
      _l  = clean_yaml(fixtures_yml_local( _f ).to_yaml)
      _o  = File.join(pwd,'.fixtures.yml.local')
      File.open( _o,'w'){|f| puts _l; f.puts _l}
      puts
      puts "# written to '#{_o}'"
    end


    desc "check for missing .fixture modules"
    task :diff do
      require 'yaml'
      pwd = File.expand_path(@base_dir)
      _f  = YAML.load_file(File.join(pwd,'.fixtures.yml'))

      unless File.file?(File.join(pwd,'.fixtures.yml.local'))
        fail "ERROR: Can't diff fixtures without a `.fixtures.yml.local` file"
      end

      _fl = YAML.load_file(File.join(pwd,'.fixtures.yml.local'))

      # reduce modules
      _f_m  = flatten_fixtures_hash(_f)
      _fl_m = flatten_fixtures_hash(_fl)
      _f_u  = (_f_m-_fl_m)
      _fl_u = (_fl_m-_f_m)

      if (_f_u.size + _fl_u.size) > 0
        warn ''
        warn "WARNING: .fixtures.yml & .fixtures.yml.local have different files!"
        warn ''
        if _f_u.size > 0
          warn 'Unique modules to .fixtures.yml:'
          _f_u.each{|x| warn "  - #{x}"}
          warn ''
        end
        if _fl_u.size > 0
          warn 'Unique modules to .fixtures.yml.local:'
          _fl_u.each{|x| warn "  - #{x}"}
          warn ''
        end
        exit 1
      end
    end
  end
end
fixtures_yml_local(_f) click to toggle source
# File lib/simp/rake/fixtures.rb, line 28
def fixtures_yml_local(_f)
  _f_m  = flatten_fixtures_hash(_f)

  _s = Hash[_f_m.map{|k|
    v= _f['fixtures']['repositories'].key?(k) ? "#\{source_dir\}/../#{k}": _f['fixtures']['symlinks'].fetch(k)
    [k, v ]}
  ]

  {'fixtures'=> {'symlinks'=> _s }}
end
flatten_fixtures_hash(_f) click to toggle source
# File lib/simp/rake/fixtures.rb, line 14
def flatten_fixtures_hash(_f)
  _f
    .map{|k,v| v.values}
    .flatten
    .reduce({}){|h,pairs|
      pairs.each{|k,v|
        (h[k] ||= []) << v
      }; h
    }
    .keys
    .uniq
    .sort
end