Class: Stylish::Library

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
lib/stylish/models/library.rb

Overview

A Stylish::Library is a collection of Packages.

Packages will be stored as folders underneath the root folder which is the library. Each package will have its own manifest.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

find_class_for_group

Class Attribute Details

.loadedObject

Returns the value of attribute loaded.



15
16
17
# File 'lib/stylish/models/library.rb', line 15

def loaded
  @loaded
end

Class Method Details

.currentObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/stylish/models/library.rb', line 17

def current
  first = loaded.first
  return first if first

  pwd = Pathname(Dir.pwd)

  if pwd.join("library").exist?
    Stylish::Library.load_from_disk(pwd.join("library"))
    loaded.first
  end
end

.find_by_root(root) ⇒ Object



89
90
91
92
93
# File 'lib/stylish/models/library.rb', line 89

def self.find_by_root(root)
  loaded.detect do |lib|
    lib.root.to_s == root.to_s || lib.package_roots.map(&:to_s).include?(root.to_s)
  end
end

.load(structure, options = {}) ⇒ Object

Loads a deserialized library cache, which we expect to be a Hash that contains at least an array of library records.

Example:

Stylish::Library.load({
  libraries:[{...},{...}]
})


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/stylish/models/library.rb', line 73

def self.load(structure, options={})
  structure = structure.to_mash if structure.is_a?(Hash)

  self.loaded = Set.new if options[:reset]

  # We're loading a collection of libraries
  if structure.libraries
    self.loaded += structure.libraries.map do |library|
      new(library)
    end
  # we're loading a single library
  elsif structure.packages
    self.loaded << new(structure)
  end
end

.load_from_disk(config_file_path = nil, options = {}) ⇒ Object

Loads the stylish library metadata from a file on disk



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/stylish/models/library.rb', line 96

def self.load_from_disk(config_file_path=nil, options={})
  path = "#{config_file_path || Stylish.config.config_file_path}".to_pathname

  raise "Invalid path" unless path.exist?

  if path.directory?
    library = path.join("library.json")

    if library.exist?
      structure = Stylish.util.deserialize(library)
    else
      structure = {root: path, packages: (path.children
        .select(&:directory?)
        .select {|p| p.join('manifest.json').exist? || p.join('manifest.yml').exist? }
        .map do |p|
          { root: p }
        end)
      }
    end
  else
    structure = Stylish.util.deserialize(path)
  end

  load(structure, options)
end

.load_from_github_repository(repository = nil) ⇒ Object

Loads the stylish library metadata from a file in a github repository

if the name of a repository is not passed (e.g. stylish/sample-theme-one) then we will attempt to use the default repository specified in the global configuration



127
128
129
130
131
132
# File 'lib/stylish/models/library.rb', line 127

def self.load_from_github_repository(repository=nil)
  Stylish.ghfs.repository = repository if repository
  structure = Stylish.util.deserialize(Stylish.ghfs.open("library.json").read)

  load(structure)
end

Instance Method Details

#create_package(attributes, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/stylish/models/library.rb', line 43

def create_package(attributes, options={})
  library = self

  Stylish::Package.new(attributes).tap do |p|
    p.library = library
    p.initialize_folder_structure if !p.root.exist?
  end
end

#find_package(query) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/stylish/models/library.rb', line 52

def find_package(query)
  if query.is_a?(Hash)
    query = query[:name]
  end

  packages.detect {|package| package.name == query || package.slug == query}
end

#package_rootsObject



38
39
40
41
# File 'lib/stylish/models/library.rb', line 38

def package_roots
  list = packages.map(&:root)
  block_given? ? yield(list) : list
end

#packagesObject



32
33
34
35
36
# File 'lib/stylish/models/library.rb', line 32

def packages
  super.tap do |list|
    list.map! {|p| p.library = self; p }
  end
end

#slugObject



60
61
62
# File 'lib/stylish/models/library.rb', line 60

def slug
  name.to_s.parameterize.downcase
end