项目作者: cpanery

项目描述 :
Minimalist Object Persistence
高级语言: Perl
项目地址: git://github.com/cpanery/nano.git
创建时间: 2020-12-29T03:01:24Z
项目社区:https://github.com/cpanery/nano

开源协议:Other

下载


NAME

Nano - Object Persistence

ABSTRACT

Minimalist Object Persistence

SYNOPSIS

  1. package Person;
  2. use Moo;
  3. extends 'Nano::Node';
  4. has name => (
  5. is => 'ro',
  6. required => 1,
  7. );
  8. has friends => (
  9. is => 'ro',
  10. default => sub { People->new }
  11. );
  12. sub extroverted {
  13. my ($self) = @_;
  14. ($self->friends->count > 1) ? 1 : 0
  15. }
  16. sub introverted {
  17. my ($self) = @_;
  18. ($self->friends->count < 2) ? 1 : 0
  19. }
  20. package People;
  21. use Moo;
  22. extends 'Nano::Nodes';
  23. sub new_type {
  24. 'Person'
  25. }
  26. sub extroverted {
  27. my ($self) = @_;
  28. $self->scope(sub {
  29. my ($person) = @_;
  30. $person->extroverted
  31. });
  32. }
  33. sub introverted {
  34. my ($self) = @_;
  35. $self->scope(sub {
  36. my ($person) = @_;
  37. $person->introverted
  38. });
  39. }
  40. package main;
  41. my $rachel = Person->new(
  42. id => 'rachel',
  43. name => 'rachel',
  44. );
  45. my $monica = Person->new(
  46. id => 'monica',
  47. name => 'monica',
  48. );
  49. my $phoebe = Person->new(
  50. id => 'phoebe',
  51. name => 'phoebe',
  52. );
  53. $rachel->friends->set($monica);
  54. $rachel->friends->set($phoebe);
  55. $monica->friends->set($rachel);
  56. $monica->friends->set($phoebe);
  57. $phoebe->friends->set($rachel);
  58. $phoebe->friends->set($monica);
  59. $rachel->save;
  60. $monica->save;
  61. $phoebe->save;
  62. $phoebe->friends->count; # 2
  63. $phoebe->friends->extroverted->count; # 2
  64. $phoebe->friends->introverted->count; # 0
  65. my $nano = Nano->new;
  66. my $friend = $nano->find('rachel');

DESCRIPTION

This package provides a minimalist framework for persisting objects (i.e.
class instances
) with as little effort as possible. This framework relies on
the Zing toolkit which provides pluggable storage and serialization options.

LIBRARIES

This package uses type constraints from:

Nano::Types

ATTRIBUTES

This package has the following attributes:

env

  1. env(Env)

This attribute is read-only, accepts (Env) values, and is optional.

METHODS

This package implements the following methods:

domain

  1. domain(Str $name) : Domain

The domain method returns a Zing::Domain object for the ID provided.

  • domain example #1

    1. my $nano = Nano->new;
    2. my $domain = $nano->domain('changelog');

dump

  1. dump(Object $object) : HashRef

The dump method returns a serialized hash representation for the object
provided.

  • dump example #1

    1. my $nano = Nano->new;
    2. my $rachel = $nano->find('rachel');
    3. my $dump = $nano->dump($rachel);

find

  1. find(Str $name) : Node

The find method finds, inflates, and returns a prior persisted object for the
ID provided.

  • find example #1

    1. my $nano = Nano->new;
    2. my $phoebe = $nano->find('phoebe');

hash

  1. hash(Str $name) : Str

The hash method returns a SHA-1 digest for the string provided.

  • hash example #1

    1. my $nano = Nano->new;
    2. my $email = 'me@example.com';
    3. $nano->hash($email);

keyval

  1. keyval(Str $name) : KeyVal

The keyval method returns a Zing::KeyVal object for the ID provided.

  • keyval example #1

    1. my $nano = Nano->new;
    2. my $keyval = $nano->keyval('rachel');

name

  1. name(Object $object) : Str

The name method returns the class name for the object provided.

  • name example #1

    1. my $nano = Nano->new;
    2. my $rachel = $nano->find('rachel');
    3. my $name = $nano->name($rachel);

object

  1. object(HashRef $object) : Object

The object method returns an object derived from a prior serialization
representation.

  • object example #1

    1. my $nano = Nano->new;
    2. my $new_rachel = $nano->object({
    3. '$type' => 'node',
    4. '$name' => 'Person',
    5. '$data' => {
    6. 'id' => 'rachel',
    7. 'name' => 'rachel',
    8. 'nano' => {
    9. '$skip' => 1
    10. },
    11. 'friends' => {
    12. '$skip' => 1
    13. },
    14. },
    15. });

reify

  1. reify(Str $name, HashRef $data) : Object

The reify method constructs an object from the class name and data provided.

  • reify example #1

    1. my $nano = Nano->new;
    2. my $new_rachel = $nano->reify('Person', {
    3. id => 'rachel',
    4. name => 'rachel',
    5. });

table

  1. table(Str $name) : Table

The table method returns a Zing::Table object for the ID provided.

  • table example #1

    1. my $nano = Nano->new;
    2. my $rachel = $nano->find('rachel');
    3. my $table = $nano->table($rachel->friends->id);

AUTHOR

Al Newkirk, awncorp@cpan.org

LICENSE

Copyright (C) 2011-2019, Al Newkirk, et al.

This is free software; you can redistribute it and/or modify it under the terms
of the The Apache License, Version 2.0, as elucidated in the “license
file”
.

PROJECT

Wiki

Project

Initiatives

Milestones

Contributing

Issues