Class: Ixtlan::Babel::Context

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Context

Returns a new instance of Context.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ixtlan/babel/context.rb', line 25

def initialize( options )
  @only = options[ :only ].collect { |o| o.to_s } if options[ :only ]
  @except = ( options[:except] || [] ).collect { |e| e.to_s }

  opts = options[ :include ]
  @include = case opts
             when Array
               opts.collect { |i| i.to_s }
             when Hash
               Hash[ opts.collect { |k,v| [ k.to_s, v ] } ]
             else
               []
             end
  @methods = (options[:methods] || []).collect { |m| m.to_s }
end

Instance Method Details

#[](key) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/ixtlan/babel/context.rb', line 57

def []( key )
  if @include.include?( key )
    self.class.new( @include.is_a?( Array ) ? {} : @include[ key ] )
  else
    self.class.new( @methods.is_a?( Array ) ? {} : @methods[ key ] )
  end
end

#allowed?(key) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ixtlan/babel/context.rb', line 45

def allowed?( key )
  ( @only && @only.include?( key ) ) || ( @only.nil? && !@except.include?( key ) ) || @methods.include?( key )
end

#array?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/ixtlan/babel/context.rb', line 53

def array?
  @include.is_a?( Array )
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ixtlan/babel/context.rb', line 49

def include?( key )
  @include.include?( key ) || @methods.include?( key ) 
end

#methodsObject



41
42
43
# File 'lib/ixtlan/babel/context.rb', line 41

def methods
  @methods + ( @include.is_a?( Array ) ? @include : @include.keys ) 
end