Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom attributes serialization and access #243

Closed
wants to merge 2 commits into from
Closed

Custom attributes serialization and access #243

wants to merge 2 commits into from

Conversation

leightonshank
Copy link

Updated the ActiveRecord\Serialization method check_methods() so that it will check for a getter method with the name of the method passed. This way, custom attributes defined as getter methods, e.g. get_full_name() can be passed directly to a serialization method in the methods option.

Example:

<?php
class Person extends ActiveRecord\Model {
    function get_full_name() {
        return "{$this->first_name} {$this->last_name}";
    }
}

$person = Person::find(1);
$person->to_json(array("methods" => "full_name"));

# {"first_name":"John", "last_name":"Smith", "full_name": "John Smith"}

In the same vein, updated the ActiveRecord\Model __get() magic method so that custom attributes can be accessed through a method of the same name. For example, if I have a method in my model named initials() then that method can be accessed as a property or a method. If the method takes arguments, then calling as a property is the same as calling with no arguments (using defaults). If the method requires arguments, then obviously it cannot be called as a property.

Example:

<?php
class Person extends ActiveRecord\Model{
    function initials($punctuate=false) {
        return $punctuate
            ? $this->first_name[0] . "." . $this->last_name[1] . "."
            : $this->first_name[0] . $this->last_name[1];
    }
}

$person = Person::find(1);
print $person->initials;     # JS
print $person->initials();  # JS
print $person->initials(true);  # J.S.

Changed ActiveRecord\Model so that the __get() method will look for
a method with the same name as the property requested, in addition
to looking for the get_property_name() getter method.

Changed ActiveRecord\Serialization so that the check_methods() method
will look for a getter method in the form of "get_method_name" if it
can't find a method "method_name" in the class. This allows custom
attributes to be easily included in serialization.
@koenpunt
Copy link
Collaborator

koenpunt commented Feb 4, 2013

+1

@tuupola
Copy link
Contributor

tuupola commented Sep 30, 2013

+1

@tuupola tuupola mentioned this pull request Sep 30, 2013
@koenpunt
Copy link
Collaborator

Closing in favor of #431

@koenpunt koenpunt closed this Jul 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants