Class: ComposeEnv::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/compose_env/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, current_env) ⇒ Context

Returns a new instance of Context.



6
7
8
9
# File 'lib/compose_env/context.rb', line 6

def initialize(options, current_env)
  @options = options
  @current_env = current_env
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/compose_env/context.rb', line 11

def method_missing(name, *args, &block)
  method_name = name.to_sym
  return handle_test_method(method_name) if test_method?(method_name)
  return handle_restriction_method(method_name, &block) if restriction_method?(method_name)

  super
end

Instance Attribute Details

#current_envObject (readonly)

Returns the value of attribute current_env.



4
5
6
# File 'lib/compose_env/context.rb', line 4

def current_env
  @current_env
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/compose_env/context.rb', line 4

def options
  @options
end

Instance Method Details

#envObject



19
20
21
# File 'lib/compose_env/context.rb', line 19

def env
  self
end

#env?(*env_names, &block) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
# File 'lib/compose_env/context.rb', line 23

def env?(*env_names, &block)
  return unless block_given?

  first_env = env_names.first
  env_names = first_env.is_a?(Array) ? first_env : env_names
  return unless env_names.map(&:to_sym).include?(current_env)

  block.call(self)
end

#env_methodsObject



63
64
65
66
67
68
69
70
71
# File 'lib/compose_env/context.rb', line 63

def env_methods
  return @env_methods unless @env_methods.nil?

  options.envs.each_with_object({test: [], restriction: []}) do |env, acc|
    test_method = "#{env}?".to_sym
    acc[:test] << test_method
    acc[:restriction] << env
  end
end

#get_bindingObject



33
34
35
# File 'lib/compose_env/context.rb', line 33

def get_binding
  binding
end

#handle_restriction_method(method_name, &block) ⇒ Object



41
42
43
44
45
# File 'lib/compose_env/context.rb', line 41

def handle_restriction_method(method_name, &block)
  return unless block_given? && method_name == current_env

  block.call(self)
end

#handle_test_method(method_name) ⇒ Object



37
38
39
# File 'lib/compose_env/context.rb', line 37

def handle_test_method(method_name)
  return method_name == "#{current_env}?".to_sym
end

#restriction_method?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/compose_env/context.rb', line 51

def restriction_method?(method_name)
  restriction_methods.include?(method_name)
end

#restriction_methodsObject



59
60
61
# File 'lib/compose_env/context.rb', line 59

def restriction_methods
  @restriction_methods ||= env_methods[:restriction]
end

#test_method?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/compose_env/context.rb', line 47

def test_method?(method_name)
  test_methods.include?(method_name)
end

#test_methodsObject



55
56
57
# File 'lib/compose_env/context.rb', line 55

def test_methods
  @test_methods ||= env_methods[:test]
end