Class: Hanswurst::As
- Inherits:
-
Object
- Object
- Hanswurst::As
- Defined in:
- lib/hanswurst/as.rb
Overview
helper for easy access to role attributes
Instance Method Summary collapse
- #<<(new_role) ⇒ Object
- #_doc ⇒ Object
- #_role ⇒ Object
-
#initialize(doc, role, rolename) ⇒ As
constructor
A new instance of As.
-
#method_missing(meth, *args, &code) ⇒ Object
we suppose, we have a subrole.
Constructor Details
#initialize(doc, role, rolename) ⇒ As
Returns a new instance of As.
6 7 8 9 10 11 12 13 |
# File 'lib/hanswurst/as.rb', line 6 def initialize(doc, role, rolename) @doc = doc @role = role @rolename = rolename.to_sym if @role.respond_to? :_doc= @role._doc = @doc end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &code) ⇒ Object
we suppose, we have a subrole
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/hanswurst/as.rb', line 36 def method_missing(meth, *args, &code) rolename = meth if meth.to_s =~ /^(.*)=$/ rolename = $1 case args.first when Hanswurst::As args = [args.first._role] when Array first = args.first.collect do |e| e.class == Hanswurst::As ? e._role : e end args = [first] end end obj = @role.send(meth, *args) obj = self.class.new(@doc, @role.send(meth, *args), rolename) if obj.is_a? CouchPotato::Persistence obj.instance_eval(&code) if code # a hack around http://api.rubyonrails.org/classes/ActiveModel/Dirty.html # since ActiveModel::Dirty does not recognize when property is a hash and hashentries do change if meth.to_s =~ /^(.*)=$/ @doc.update_roles! end obj end |
Instance Method Details
#<<(new_role) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/hanswurst/as.rb', line 19 def << (new_role) if new_role.class == Hanswurst::As new_role = new_role._role end if @role.is_a? Array @role << new_role else @role = [@role, new_role] @doc.send :"#{@rolename}=", @role end end |
#_doc ⇒ Object
31 32 33 |
# File 'lib/hanswurst/as.rb', line 31 def _doc @doc end |
#_role ⇒ Object
15 16 17 |
# File 'lib/hanswurst/as.rb', line 15 def _role @role end |