Class: CanPlay::AbstractResource::OnlyInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/can_play/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ OnlyInstance



31
32
33
34
35
# File 'lib/can_play/resource.rb', line 31

def initialize(opts)
  @user               = opts[:user] if opts[:user]
  @instance           = opts[:instance] if opts[:instance]
  self.class.only_you = self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



76
77
78
79
80
81
# File 'lib/can_play/resource.rb', line 76

def method_missing(method, *args, &block)
  if @instance
    return @instance.send(method, *args, &block)
  end
  super(method, *args, &block)
end

Class Method Details

.after_initialize_block_arrayObject



62
63
64
# File 'lib/can_play/resource.rb', line 62

def self.after_initialize_block_array
  @after_initialize_block_array ||= []
end

.new(opts = {}.with_indifferent_access) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/can_play/resource.rb', line 37

def self.new(opts = {}.with_indifferent_access)
  o = allocate
  o.instance_eval { initialize(opts) }
  after_initialize_block_array.each do |block|
    o.instance_exec(opts[:user], &block)
  end
  o
end

.onlyObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/can_play/resource.rb', line 46

def self.only
  if only_you.blank?
    if self == CanPlay::AbstractResource::OnlyInstance
      raise 'CanPlay::Resource::OnlyInstance not set first instance'
    else
      self.only_you = new(instance: superclass.only)
    end
  else
    only_you
  end
end

.only=(obj) ⇒ Object



58
59
60
# File 'lib/can_play/resource.rb', line 58

def self.only=(obj)
  self.only_you = obj
end

Instance Method Details

#klassObject



66
67
68
69
70
71
72
73
74
# File 'lib/can_play/resource.rb', line 66

def klass
  @klass ||= begin
    clazz_string = self.class.parent.name
    raise 'class name error' unless clazz_string.end_with?('Resource') || clazz_string.end_with?('ResourceOverride')
    clazz_name = self.class.parent.name.gsub(/Resource$/, '').gsub(/ResourceOverride$/, '')
    klass = clazz_name.constantize rescue nil
    klass
  end
end