Class: Phony::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/phony/config.rb

Overview

The Config class is only used to configure Phony and to load specific subsets of CCs.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(included_ccs, excluded_ccs) ⇒ Config

Returns a new instance of Config.


22
23
24
25
# File 'lib/phony/config.rb', line 22

def initialize included_ccs, excluded_ccs
  @included_ccs = included_ccs || []
  @excluded_ccs = excluded_ccs || []
end

Instance Attribute Details

#excluded_ccsObject (readonly)

Returns the value of attribute excluded_ccs


20
21
22
# File 'lib/phony/config.rb', line 20

def excluded_ccs
  @excluded_ccs
end

#included_ccsObject (readonly)

Returns the value of attribute included_ccs


20
21
22
# File 'lib/phony/config.rb', line 20

def included_ccs
  @included_ccs
end

Class Method Details

.load(*options) ⇒ Object

Use as follows:

require 'phony/config'

# Load only these:
Phony::Config.load(only: ['41', '44'])
# or all except these:
Phony::Config.load(except: ['41', '44'])
# or "only", in short form.
Phony::Config.load('41', '44')
# or even shorter form:
Phony::Config.load(41, 44)

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/phony/config.rb', line 60

def self.load *options
  # Extract options.
  last = options.last
  only, except = if last.respond_to?(:to_hash)
    # We have the explicit form.
    [last[:only], last[:except]]
  elsif options.respond_to?(:to_ary)
    # We have the convenience short forms.
    [options, []]
  end
  
  # Set defaults.
  only, except = [only || [], except || []]
  # Convert to expected format if possible.
  only, except = [only.map(&:to_s), except.map(&:to_s)]
  
  # Check params.
  raise "Params given to Phony::Config.load must be Array-like. The one given was: #{only}" unless only.respond_to?(:to_ary)
  raise "Params given to Phony::Config.load must be Array-like. The one given was: #{except}" unless except.respond_to?(:to_ary)
  
  # Configure Phony.
  Phony.config = new(only, except)
  
  # Load phony.
  Kernel.load File.expand_path('../../phony.rb', __FILE__)
  
  # Return the stored config data.
  Phony.config
end

Instance Method Details

#has_excluded?Boolean

Returns:

  • (Boolean)

43
44
45
# File 'lib/phony/config.rb', line 43

def has_excluded?
  !excluded_ccs.empty?
end

#has_included?Boolean

Returns:

  • (Boolean)

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

def has_included?
  !included_ccs.empty?
end

#load?(cc) ⇒ Boolean

Returns:

  • (Boolean)

27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/phony/config.rb', line 27

def load? cc
  return false if has_excluded? && excluded_ccs.include?(cc)
  
  if has_included?
    # We have to check the included_ccs, otherwise false.
    return true if included_ccs.include?(cc)
    
    false
  else
    # It's not in excluded and no included was given.
    true
  end
end