Class: Shoes::Configuration

Inherits:
Object
  • Object
show all
Defined in:
shoes-core/lib/shoes/configuration.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.app_dirObject

Returns the value of attribute app_dir.



6
7
8
# File 'shoes-core/lib/shoes/configuration.rb', line 6

def app_dir
  @app_dir
end

.fail_fastObject



9
10
11
# File 'shoes-core/lib/shoes/configuration.rb', line 9

def fail_fast
  @fail_fast ||= ENV["SHOES_FAIL_FAST"]
end

Class Method Details

.backendObject



13
14
15
# File 'shoes-core/lib/shoes/configuration.rb', line 13

def backend
  @backend ||= Shoes.load_backend(backend_name)
end

.backend=(name) ⇒ Object

Set the Shoes backend to use. Can only be set once. Note the backend is not required during this method, but rather when ‘Shoes::Configuration#backend` is called.

Examples:

Shoes::Configuration.backend = :swt

Parameters:

  • name (Symbol)

    The backend’s name

Raises:

  • (RuntimeError)

    If backend has already been set.

See Also:

  • #backend


38
39
40
41
42
43
44
45
# File 'shoes-core/lib/shoes/configuration.rb', line 38

def backend=(name)
  return if defined?(@backend_name) && @backend_name == name

  if defined?(@backend) && !@backend.nil?
    raise "Can't switch backend to Shoes::#{name.capitalize}, Shoes::#{backend_name.capitalize} backend already loaded."
  end
  @backend_name ||= name
end

.backend_class(object) ⇒ Object

Finds the appropriate backend class for the given Shoes object or class

Examples:

Shoes.configuration.backend_class(shoes_button) # => Shoes::Swt::Button

Parameters:

  • object (Object)

    A Shoes object or a class

Returns:

  • (Object)

    An appropriate backend class

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
# File 'shoes-core/lib/shoes/configuration.rb', line 53

def backend_class(object)
  klazz = object.is_a?(Class) ? object : find_shoes_base_class(object)
  class_name = klazz.name.split("::").last
  # Lookup with false to not consult modules higher in the chain Object
  # because Shoes::Swt.const_defined? 'Range' => true
  raise ArgumentError, "#{object} does not have a backend class defined for #{backend}" unless backend.const_defined?(class_name, false)
  backend.const_get(class_name, false)
end

.backend_factory(shoes_object) ⇒ Object



82
83
84
85
# File 'shoes-core/lib/shoes/configuration.rb', line 82

def backend_factory(shoes_object)
  klass = backend_class(shoes_object)
  klass.respond_to?(:create) ? klass.method(:create) : klass.method(:new)
end

.backend_for(shoes_object, *args, &blk) ⇒ Object

Creates an appropriate backend object, passing along additional arguments

Examples:

Shoes.backend_for(button, args) # => <Shoes::Swt::Button:0x12345678>

Parameters:

  • shoes_object (Object)

    A Shoes object

Returns:

  • (Object)

    An appropriate backend object



75
76
77
78
79
80
# File 'shoes-core/lib/shoes/configuration.rb', line 75

def backend_for(shoes_object, *args, &blk)
  # Some element types (packager for instance) legitimately don't have
  # an app. In those cases, don't try to get it to pass along.
  args.unshift(shoes_object.app.gui) if shoes_object.respond_to?(:app)
  backend_factory(shoes_object).call(shoes_object, *args, &blk)
end

.backend_nameObject



17
18
19
# File 'shoes-core/lib/shoes/configuration.rb', line 17

def backend_name
  @backend_name ||= ENV.fetch('SHOES_BACKEND', default_backend).to_sym
end

.default_backendObject



21
22
23
24
25
26
27
# File 'shoes-core/lib/shoes/configuration.rb', line 21

def default_backend
  if caller.any? { |path| path =~ /rspec/ }
    :mock
  else
    :swt
  end
end

.find_shoes_base_class(object) ⇒ Object



62
63
64
65
# File 'shoes-core/lib/shoes/configuration.rb', line 62

def find_shoes_base_class(object)
  return object.shoes_base_class if object.respond_to?(:shoes_base_class)
  object.class
end