Class: Gloo::Shell::Context

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Initialize the shell context.



17
18
19
20
# File 'lib/gloo/shell/context.rb', line 17

def initialize
  @done = false
  @properties = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Dynamic method access to properties. e.g. context.verbs is the same as context.get(:verbs), context.verbs = [...] is the same as context.set(:verbs, [...]).



73
74
75
76
77
78
79
80
81
# File 'lib/gloo/shell/context.rb', line 73

def method_missing( method_name, *args, &block )
  if method_name.to_s.end_with?( '=' )
    key = method_name.to_s.chomp( '=' ).to_sym
    set( key, args.first )
  else
    key = method_name.to_sym
    get( key )
  end
end

Instance Attribute Details

#doneObject

Returns the value of attribute done.



12
13
14
# File 'lib/gloo/shell/context.rb', line 12

def done
  @done
end

Instance Method Details

#add_to_list(key, item) ⇒ Object

Add an item to a property list.



43
44
45
46
47
48
49
50
51
52
# File 'lib/gloo/shell/context.rb', line 43

def add_to_list( key, item )
  key = key.to_sym
  list = get( key )
  list = [] unless list.is_a?( Array )

  list << item
  set( key, list )

  return list
end

#get(key) ⇒ Object

Get a property value. Returns an empty array if the property doesn't exist.



26
27
28
29
30
31
# File 'lib/gloo/shell/context.rb', line 26

def get( key )
  key = key.to_sym
  return @properties[ key ] if @properties.key?( key )

  return []
end

#has?(key) ⇒ Boolean

Check if a property exists.

Returns:

  • (Boolean)


57
58
59
# File 'lib/gloo/shell/context.rb', line 57

def has?( key )
  return @properties.key?( key.to_sym )
end

#keysObject

Get all property keys.



64
65
66
# File 'lib/gloo/shell/context.rb', line 64

def keys
  return @properties.keys
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Respond to missing methods for property access.

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/gloo/shell/context.rb', line 86

def respond_to_missing?( method_name, include_private = false )
  key = method_name.to_s.chomp( '=' ).to_sym
  return has?( key ) || super
end

#set(key, value) ⇒ Object

Set a property value.



36
37
38
# File 'lib/gloo/shell/context.rb', line 36

def set( key, value )
  @properties[ key.to_sym ] = value
end