Class: MayI::Access

Inherits:
Object
  • Object
show all
Defined in:
lib/mayi/access.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(implementation = nil) ⇒ Access

Returns a new instance of Access.



6
7
8
# File 'lib/mayi/access.rb', line 6

def initialize(implementation = nil)
  self.implementation = implementation
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mayi/access.rb', line 29

def method_missing(meth, *args, &block)
  if meth.to_s =~ /^(may)_.*(\?|!)$/
    if meth.to_s.end_with?("!")
      if current_instance.send(meth.to_s[0..-2].to_sym,*args)
        yield if block_given?
        true
      else
        raise_error(meth)
      end
    elsif meth.to_s.end_with?("?")
      if current_instance.send(meth.to_s[0..-2].to_sym,*args)
        yield if block_given?
        true
      else
        false
      end
    end
  else
    super(meth,*args,&block)  
  end
end

Instance Attribute Details

#implementationObject

Returns the value of attribute implementation.



4
5
6
# File 'lib/mayi/access.rb', line 4

def implementation
  @implementation
end

Instance Method Details

#clearObject



21
22
23
# File 'lib/mayi/access.rb', line 21

def clear
  Thread.current["mayi_access_implementation_#{self.object_id}"] = nil
end

#current_instanceObject



25
26
27
# File 'lib/mayi/access.rb', line 25

def current_instance
  Thread.current["mayi_access_implementation_#{self.object_id}"] || refresh
end

#error_message(message) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/mayi/access.rb', line 59

def error_message(message)
  Proxy.new(self) do |state|
    if state == :before
      @error_message = message
    else
      @error_message = nil
    end
  end 
end

#refresh(data = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/mayi/access.rb', line 10

def refresh(data = {})
  raise "You have not set any implementation yet" unless self.implementation
  if self.implementation.is_a?(Class)
    Thread.current["mayi_access_implementation_#{self.object_id}"] = self.implementation.new(data)
  else
    instance = self.implementation.to_s.split('::').reduce(Object){|cls, c| cls.const_get(c) }.new(data)
    Thread.current["mayi_access_implementation_#{self.object_id}"] = instance
  end
  
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'lib/mayi/access.rb', line 51

def respond_to?(meth)
  if meth.to_s =~ /^(may)_.*(\?|!)$/
    super(meth.to_s[0..-2].to_sym)
  else
    super(meth)  
  end
end