{"id":13807018,"url":"https://github.com/TPei/observable","last_synced_at":"2025-05-13T23:33:54.357Z","repository":{"id":69715609,"uuid":"60855019","full_name":"TPei/observable","owner":"TPei","description":"Implementation of the Observer pattern in crystal","archived":false,"fork":false,"pushed_at":"2023-04-10T13:22:37.000Z","size":19,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-04T01:07:06.429Z","etag":null,"topics":["crystal","design-pattern","observable","observer","observer-pattern","shard"],"latest_commit_sha":null,"homepage":null,"language":"Crystal","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TPei.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-06-10T14:45:50.000Z","updated_at":"2024-03-09T16:52:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"a33239cc-c17a-4a9f-a7cd-99dedb08e905","html_url":"https://github.com/TPei/observable","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TPei%2Fobservable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TPei%2Fobservable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TPei%2Fobservable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TPei%2Fobservable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TPei","download_url":"https://codeload.github.com/TPei/observable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225267438,"owners_count":17447154,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crystal","design-pattern","observable","observer","observer-pattern","shard"],"created_at":"2024-08-04T01:01:19.522Z","updated_at":"2024-11-18T23:30:36.194Z","avatar_url":"https://github.com/TPei.png","language":"Crystal","funding_links":[],"categories":["Misc"],"sub_categories":[],"readme":"# observable\n\n###### This project is being built weekly with the latest crystal version (works with v1.7.2 🎉)\n\nSimple implementation of the Observer pattern in Crystal. It is loosely based on the [Observable](http://ruby-doc.org/stdlib-2.0.0/libdoc/observer/rdoc/Observable.html) module from Ruby :)\n\nThe observer pattern is used to avoid tightly coupling objects that\nstand in a one-to-many relationship.\nIt allows you to notify dependents about changes that happened to your\nobject, without having hard-coded dependencies :)\n\n## Installation\n\nAdd to your shard.yml\n\n```yaml\ndependencies:\n  observable:\n    github: tpei/observable\n    branch: master\n```\n\nand then install the library into your project with\n\n```bash\n$ crystal deps\n```\n\n## Usage\n\nInclude observable in the class you want to observe:\n\n```crystal\nrequire \"observable/observable\"\n\nclass ToObserve\n  include Observable\nend\n```\nThis provides you with the following methods:\n- `add_observer(observer : Observer)`\n- `changed(state=true)`\n- `changed?`\n- `count_observers`\n- `delete_observer(observer : Observer)`\n- `delete_observers`\n- `notify_observers`\n\nand as an added bonus, because I dislike the forced `changed` call precondition:\n- `notify_observers!`, which notifies no matter if changed or not\n\n\nThen you can include observer in the class you want observing:\n\n```crystal\nrequire \"observable/observer\"\n\nclass Observing\n  include Observer\n\n  def update(observable : Observable)\n    # do whatever you want to do if your observable changes\n  end\nend\n```\nThis only gives you an `update(observable : Observable)` method you should override, because it willl raise an error otherwise.\n\nUnfortunately we don't have ruby's verbosity and dynamism here so it's all a little more static. Especially the `update` method can really only pass on the `observable` object.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTPei%2Fobservable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTPei%2Fobservable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTPei%2Fobservable/lists"}