Class: Quik::Catalog

Inherits:
Object
  • Object
show all
Defined in:
lib/quik/catalog.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Catalog

Returns a new instance of Catalog.



24
25
26
27
28
29
# File 'lib/quik/catalog.rb', line 24

def initialize( text )
  recs = YAML.load( text )
  ## note: convert array to hash (lookup by name)
  ##    todo: check for missing name property? - why? why not?
  @scripts = recs.reduce( {} ) { |h,rec| h[rec['name']] = rec; h }
end

Class Method Details

.from_file(path) ⇒ Object



13
14
15
16
17
# File 'lib/quik/catalog.rb', line 13

def self.from_file( path )
  ## read in themes catalog
  text = File.open( path, 'r:utf-8' ) { |f| f.read }
  new( text )
end

.from_url(src) ⇒ Object



7
8
9
10
11
# File 'lib/quik/catalog.rb', line 7

def self.from_url( src )
  worker = Fetcher::Worker.new
  text = worker.read_utf8!( src )
  new( text )
end

Instance Method Details

#find(name) ⇒ Object



32
33
34
# File 'lib/quik/catalog.rb', line 32

def find( name )
  @scripts[ name ]
end

#list(filter = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/quik/catalog.rb', line 36

def list( filter=nil )
  ## pp filter

  @scripts.each_with_index do |(_,rec),i|
    name    = rec['name']
    summary = rec['summary']
    script  = rec['script']

    if script && script.index( '//github.com/')
      ## "beautify" / shorten github links - remove raw/master/
      script = script.gsub('raw/master/', '' )
    end

    ## pp h

    line = ''
    line << "  %3d" % (i+1)
    line << "..%-10s" % name
    line << " .:.  #{summary}"
    line << " @ (#{script})"

    if filter
      ## note: ignore case (upper/lower/downcase) for now
      ##  filter on name/title and
      ##            tags for now
      if name.downcase.index(filter.downcase)    != nil ||    ## note: 0 is match found on index 0; only nil is not found
         summary.downcase.index(filter.downcase) != nil
        puts line
      end
    else
      puts line
    end
  end
end