File Coverage

blib/lib/Rubyish/Object.pm
Criterion Covered Total %
statement 18 21 85.7
branch n/a
condition n/a
subroutine 6 7 85.7
pod n/a
total 24 28 85.7


line stmt bran cond sub pod time code
1 17     17   518 use strict;
  17         32  
  17         914  
2              
3             =head1 NAME
4              
5             Rubyish::Object - the parent class of all classes in Rubyish
6              
7             =head1 DESCRIPTION
8              
9             This implements the "Object" class in ruby.
10              
11             Baiscally if you are defining a Rubyish class, you should not use this
12             class as the base class, but just say C in your code.
13             This is almost all for the internals of Rubyish Perl.
14              
15             =cut
16              
17             package Rubyish::Object;
18 17     17   915 use feature qw(switch);
  17         34  
  17         3958  
19              
20 17     17   18850 use UNIVERSAL::isa;
  17         62114  
  17         154  
21 17     17   670 use Scalar::Util qw(refaddr);
  17         30  
  17         908  
22 17     17   11890 use Rubyish::Syntax::def;
  17         72  
  17         117  
23 17     17   19042 use Rubyish::Kernel;
  9         20  
  9         110  
24              
25             =head1 FUNCTIONS
26              
27             =head2 new()
28              
29             The default constructor for all sub-classes.
30              
31             =cut
32              
33             sub new {
34 0     0     my ($class) = @_;
35 0           my $self = {};
36 0           return bless $self, $class;
37             }
38              
39             =head2 object_id(), __id__()
40              
41             Returns the internal address of the object_id
42              
43             =cut
44              
45             def object_id {
46             refaddr $self;
47             };
48              
49             { no strict; *__id__ = *object_id; }
50              
51             # overwrite the same method in Class
52             def superclass {
53             my $class = ref($self) || $self;
54             no strict;
55             return ${"${class}::ISA"}[-1];
56             };
57              
58             # overwrite the same method in Class
59             def class {
60             return ref($self) || "Rubyish::Class";
61             };
62              
63             sub is_a {
64             my ($self, $class) = @_;
65             if (ref($self)) {
66             return 1 if $self->class eq $class;
67             return ref($self)->isa($class);
68             }
69              
70             return $self->isa($class);
71             }
72              
73             { no strict; *kind_of = *is_a; }
74              
75             =head2 __send__($name, @args)
76              
77             Invokes method by string
78              
79             package Animal;
80             use base Rubyish::Object;
81              
82             def hello {
83             my ($self, $arg) = @_;
84             "hello, $arg.";
85             }
86              
87             1;
88              
89             my $dog = Animal->new;
90             print $dog->__send__("hello", "perl"); # "hello, perl."
91              
92             =cut
93              
94             sub __send__ {
95             my ($self, $name, @args) = @_;
96             if (my $sub = $self->can($name)) {
97             $sub->($self, @args);
98             }
99             }
100              
101             {
102             no strict;
103             *send = *__send__;
104             }
105              
106             =head2 to_yaml()
107              
108             Serialize the object to YAML.
109              
110             =cut
111              
112             use YAML;
113             sub to_yaml {
114             return YAML::Dump(@_);
115             }
116              
117             =head2 methods()
118              
119             Return a list of names of instance methods.
120              
121             =cut
122              
123             use Class::Inspector;
124             def methods {
125             my $methods = Class::Inspector->methods(ref($self), "public");
126             Rubyish::Kernel::Array($methods);
127             };
128              
129             =head2 inpsect()
130              
131             Returns a string containing a human-readable representation of obj.
132              
133             =cut
134              
135             def inspect {
136             scalar($self) =~ /\w+=\w+(\((.*)\))/;
137             "#<" . ref($self) . ":" . $2 . ">";
138             };
139              
140             =head2 ancestors()
141              
142             All ancestors of this object.
143              
144             =cut
145              
146             def ancestors {
147             no strict;
148             Array([@{ref($self) . "::ISA"}]);
149             # not completed
150             };
151              
152             =head2 clone
153              
154             Produces a shollow copy a object. New object would not have the same memory address as ordinary object.
155              
156             =cut
157              
158             use Clone;
159             *clone = *Clone::clone;
160              
161             1;
162              
163             =head1 AUTHOR
164              
165             Kang-min Liu C<< >>, shelling C
166              
167             =head1 LICENCE AND COPYRIGHT
168              
169             Copyright (c) 2008,2009, Kang-min Liu C<< >>.
170              
171             This is free software, licensed under:
172              
173             The MIT (X11) License
174              
175             =head1 DISCLAIMER OF WARRANTY
176              
177             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
178             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
179             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
180             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
181             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
182             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
183             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
184             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
185             NECESSARY SERVICING, REPAIR, OR CORRECTION.
186              
187             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
188             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
189             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
190             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
191             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
192             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
193             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
194             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
195             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
196             SUCH DAMAGES.