Module: Checkin::Dsl::Roles::ClassMethods
- Defined in:
- lib/checkin/dsl/roles.rb
Instance Method Summary collapse
- #find_role_by_method(meth) ⇒ Object
- #find_role_by_name(name) ⇒ Object
- #register_role(role) ⇒ Object
- #register_role_method(meth, role) ⇒ Object
- #role(*args, &block) ⇒ Object
- #role_methods ⇒ Object
- #role_names ⇒ Object
- #roles ⇒ Object
- #symbolized_role_names ⇒ Object
Instance Method Details
#find_role_by_method(meth) ⇒ Object
83 84 85 |
# File 'lib/checkin/dsl/roles.rb', line 83 def find_role_by_method(meth) self.role_methods[meth] end |
#find_role_by_name(name) ⇒ Object
87 88 89 |
# File 'lib/checkin/dsl/roles.rb', line 87 def find_role_by_name(name) self.roles[name] end |
#register_role(role) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/checkin/dsl/roles.rb', line 67 def register_role(role) self.register_role_method(role.check_method_name, role) role.aliases.each do |a| self.register_role_method(a, role) end self.roles[role.name] = role end |
#register_role_method(meth, role) ⇒ Object
79 80 81 |
# File 'lib/checkin/dsl/roles.rb', line 79 def register_role_method(meth, role) self.role_methods[meth] = role end |
#role(*args, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/checkin/dsl/roles.rb', line 32 def role(*args, &block) opts = args. if args.empty? raise ::ArgumentError.new("wrong number of arguments") end role_name = args.first if role_name.to_s =~ /^can_([A-z]_)+\?$/ raise "bad role name: '#{role_name}'. Roles can not be in the form of 'can [...]?''" end if role_name.to_s.singularize != role_name.to_s raise "bad role name: '#{role_name}'. Roles can not be in the plural form" end role = ::Checkin::Role.new role.name = role_name role.dependencies = [ opts[:require] ].flatten.compact.uniq role.check_proc = block role.aliases = ([ opts[:alias] ] + [ opts[:aliases] ]).flatten.compact.uniq.map {|a| "#{a.to_s.gsub(/\?$/, "")}?"} role.check_method_name = "#{(opts[:method] || role_name).to_s.gsub(/\?$/, "")}?" self.register_role(role) end |
#role_methods ⇒ Object
63 64 65 |
# File 'lib/checkin/dsl/roles.rb', line 63 def role_methods @role_methods ||= ::ActiveSupport::HashWithIndifferentAccess.new end |
#role_names ⇒ Object
91 92 93 |
# File 'lib/checkin/dsl/roles.rb', line 91 def role_names self.roles.keys.map {|k| k.to_s} end |
#roles ⇒ Object
59 60 61 |
# File 'lib/checkin/dsl/roles.rb', line 59 def roles @roles ||= ::ActiveSupport::HashWithIndifferentAccess.new end |
#symbolized_role_names ⇒ Object
95 96 97 |
# File 'lib/checkin/dsl/roles.rb', line 95 def symbolized_role_names self.roles.keys.map {|k| k.to_sym} end |