Module: Vignette

Defined in:
lib/vignette.rb,
lib/vignette/version.rb

Defined Under Namespace

Modules: Errors

Constant Summary collapse

VERSION =
"0.0.14"

Class Method Summary collapse

Class Method Details

.active?Boolean

Is Vignette active for this thread (i.e. do we have a repo?)

Returns:

  • (Boolean)


61
62
63
# File 'lib/vignette.rb', line 61

def self.active?
  !Thread.current[:vignette_repo].nil?
end

.clear_repoObject

Clears the current repo on this thread



45
46
47
# File 'lib/vignette.rb', line 45

def self.clear_repo
  set_repo(nil)
end

.init(opts = {}) ⇒ Object

Set any initializers



33
34
35
36
37
# File 'lib/vignette.rb', line 33

def self.init(opts={})
  opts.each do |k,v|
    Vignette.send("#{k}=", v)
  end
end

.repoObject

Get the repo for this thread



66
67
68
69
70
# File 'lib/vignette.rb', line 66

def self.repo
  raise Errors::ConfigError.new("Repo not active, please call Vignette.set_repo before using Vignette (or use around_filter in Rails)") if !active?

  Thread.current[:vignette_repo]
end

.set_repo(repo) ⇒ Object

Sets the current repo to be used to get and store tests for this thread



40
41
42
# File 'lib/vignette.rb', line 40

def self.set_repo(repo)
  Thread.current[:vignette_repo] = repo
end

.set_vig(vig) ⇒ Object

For this repo, store an update Vig



80
81
82
# File 'lib/vignette.rb', line 80

def self.set_vig(vig)
  repo[:v] = vig.to_json
end

.tests(vig = nil) ⇒ Object

Pull all the tests for this current repo



85
86
87
88
89
90
91
92
93
# File 'lib/vignette.rb', line 85

def self.tests(vig=nil)
  vig ||= Vignette.vig

  name_values = vig.values.map { |v| [ v['n'], [ v['t'], v['v'] ] ] }.group_by { |el| el[0] }

  arr = name_values.map { |k,v| [ k.to_sym, v.sort { |a,b| b[1][0] <=> a[1][0] }.first[1][1] ] }

  Hash[arr]
end

.vig(repo = nil) ⇒ Object

From the repo (default whatever is set for the thread), grab Vignettes’ repo and unpack



73
74
75
76
77
# File 'lib/vignette.rb', line 73

def self.vig(repo=nil)
  repo ||= Vignette.repo # allow using existing

  repo && repo[:v].present? ? JSON(repo[:v]) : {}
end

.with_repo(repo) ⇒ Object

Performs block with repo set to ‘repo` for this thread



50
51
52
53
54
55
56
57
58
# File 'lib/vignette.rb', line 50

def self.with_repo(repo)
  begin
    Vignette.set_repo(repo)

    yield
  ensure
    Vignette.clear_repo
  end
end