Module: CanPlay::ClassMethods

Included in:
AbstractResource
Defined in:
lib/can_play/class_method.rb

Instance Method Summary collapse

Instance Method Details

#add_resource(group, verb, object, type, behavior, opts) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/can_play/class_method.rb', line 91

def add_resource(group, verb, object, type, behavior, opts)
  name     = "#{verb}_#{group.name}"
  resource = ResourcePermission.new(
    my_module_name: my_module_name,
    name:        name,
    group:       group,
    verb:        verb,
    object:      object,
    type:        type,
    behavior:    behavior,
    opts:        opts
  )
  CanPlay.resources.keep_if { |i| i.name != name }
  CanPlay.resources << resource
end

#collection(verb_or_verbs, opts = {}, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/can_play/class_method.rb', line 47

def collection(verb_or_verbs, opts={}, &block)

  raise "Need define group first" if current_group.nil?
  opts = OpenStruct.new opts
  group    = current_group
  behavior = nil
  clazz = self
  if block
    behavior = Proc.new do
      result = clazz::OnlyInstance.only.instance_eval(&block)
      result
    end
  end

  if verb_or_verbs.kind_of?(Array)
    verb_or_verbs.each do |verb|
      add_resource(group, verb, group.klass, 'collection', behavior, opts)
    end
  else
    add_resource(group, verb_or_verbs, group.klass, 'collection', behavior, opts)
  end
end

#group(*args, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/can_play/class_method.rb', line 14

def group(*args, &block)
  opts = args.extract_options!.with_indifferent_access
  clazz = args.first
  if clazz.is_a?(Module)
    name  = clazz.try(:table_name).presence || clazz.to_s.underscore.gsub('/', '_').pluralize
    group = NameImportantGroup.new(name: name, klass: clazz, defined_class_wrapper: self)
  elsif clazz.blank? &&  opts.key?(:name) &&  opts.key?(:klass)
    opts  = opts.with_indifferent_access
    group = NameImportantGroup.new(name: opts.delete(:name).to_s, klass: opts.delete(:klass), defined_class_wrapper: self)
  else
    raise "group klass need set"
  end
  group.opts = OpenStruct.new opts
  CanPlay.groups << group
  CanPlay.groups = CanPlay.groups.uniq(&:name)
  self.temp_current_group = self.current_group
  self.current_group = group
  if block
    block.call(group.klass)
    self.current_group = self.temp_current_group
    self.temp_current_group = nil
  end
end

#limit(name = nil, &block) ⇒ Object



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

def limit(name=nil, &block)
  clazz = self
  wrap_block = Proc.new do |*args|
    clazz::OnlyInstance.only.instance_exec(*args, &block)
  end

  CanPlay::Power.power(name||current_group.name, &wrap_block)
end

#member(verb_or_verbs, opts = {}, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/can_play/class_method.rb', line 70

def member(verb_or_verbs, opts={}, &block)
  raise "Need define group first" if current_group.nil?
  opts = OpenStruct.new opts
  group    = current_group
  behavior = nil
  clazz = self
  if block
    behavior = Proc.new do |obj|
      clazz::OnlyInstance.only.instance_exec(obj, &block)
    end
  end

  if verb_or_verbs.kind_of?(Array)
    verb_or_verbs.each do |verb|
      add_resource(group, verb, group.klass, 'member', behavior, opts)
    end
  else
    add_resource(group, verb_or_verbs, group.klass, 'member', behavior, opts)
  end
end