{"id":15527246,"url":"https://github.com/jun0kada/context_eval","last_synced_at":"2026-04-28T08:37:23.087Z","repository":{"id":62556204,"uuid":"174965962","full_name":"Jun0kada/context_eval","owner":"Jun0kada","description":null,"archived":false,"fork":false,"pushed_at":"2019-03-12T03:00:21.000Z","size":8,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-04T10:48:17.225Z","etag":null,"topics":["rails","ruby"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","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/Jun0kada.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-11T09:20:33.000Z","updated_at":"2019-03-12T03:17:30.000Z","dependencies_parsed_at":"2022-11-03T06:00:23.962Z","dependency_job_id":null,"html_url":"https://github.com/Jun0kada/context_eval","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jun0kada%2Fcontext_eval","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jun0kada%2Fcontext_eval/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jun0kada%2Fcontext_eval/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jun0kada%2Fcontext_eval/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jun0kada","download_url":"https://codeload.github.com/Jun0kada/context_eval/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246100581,"owners_count":20723479,"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":["rails","ruby"],"created_at":"2024-10-02T11:05:11.159Z","updated_at":"2026-04-28T08:37:23.022Z","avatar_url":"https://github.com/Jun0kada.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ContextEval\n\n[![Build Status](https://travis-ci.org/Jun0kada/context_eval.svg?branch=master)](https://travis-ci.org/Jun0kada/context_eval)\n\nprovide `Object#context_eval`\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'context_eval'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install context_eval\n\n## Usage\n\n```ruby\nclass Notification \u003c ActiveRecord::Base\n  belongs_to :user\n  belongs_to :notifiable, polymorphic: true\n\n  delegate :notification_config, to: :notifiable\n\n  def title\n    notifiable.context_eval(notification_config[:title]) || raise\n  end\n\n  def message\n    notifiable.context_eval(notification_config[:message])\n  end\nend\n\nmodule Notifiable\n  extend ActiveSupport::Concern\n\n  included do\n    config_accessor :notification_config, instance_writer: false # ActiveSupport::Configurable\n\n    has_one :notification, as: :notifiable, dependent: :destroy, class_name: '::Notification'\n\n    after_create do\n      self.create_notification!(user: context_eval(notification_config[:send_to]))\n    end\n  end\nend\n\nclass Nice \u003c ActiveRecord::Base\n  include Notifiable\n\n  self.notification_config = {\n    title: 'Nice!',\n    send_to: -\u003e { article.user }\n  }\n\n  belongs_to :article\nend\n\nclass Comment \u003c ActiveRecord::Base\n  include Notifiable\n\n  self.notification_config = {\n    title: -\u003e { \"#{body[0..20]}...\" },\n    message: :body,\n    send_to: -\u003e { article.user }\n  }\n\n  belongs_to :article\nend\n```\n\n```sample.html.erb\n\u003cul\u003e\n  \u003c% @notifications.each do |notification| %\u003e\n    \u003cli\u003e\n      \u003ch3\u003e\u003c%= notification.title %\u003e\u003c/h3\u003e\n\n      \u003c% if notification.message.present? %\u003e\n        \u003cp\u003e\u003c%= notification.message %\u003e\u003c/p\u003e\n      \u003c% end %\u003e\n    \u003c/li\u003e\n  \u003c% end %\u003e\n\u003c/ul\u003e\n```\n\n\n### Detail\n\n```ruby\nclass User\n  attr_accessor :name\n\n  def initialize(name)\n    @name = name\n  end\nend\n\nuser = User.new('john')\n\n# Symbol\nuser.context_eval(:name) #=\u003e john\n\n# Proc\nproc_arg = Proc.new { \"Mr. #{name}\" }\nuser.context_eval(proc_arg) #=\u003e Mr. john\n\n# Proc and args\nproc_arg = Proc.new { |age| \"Mr. #{name}, #{age} years old.\" }\nuser.context_eval(proc_arg, 30) #=\u003e Mr. john, 30 years old.\n\n# Lambda\nlambda_arg = -\u003e { \"Mr. #{name}\" }\nuser.context_eval(lambda_arg) #=\u003e Mr. john\n\n# Lambda and args\nlambda_arg = -\u003e (age) { \"Mr. #{name}, #{age} years old.\" }\nuser.context_eval(lambda_arg, 30) #=\u003e Mr. john, 30 years old.\n\n# String\nuser.context_eval('Hey john') #=\u003e Hey John\n\n# Nil\nuser.context_eval(nil) #=\u003e nil\n```\n\n##\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/context_eval. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the ContextEval project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/context_eval/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjun0kada%2Fcontext_eval","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjun0kada%2Fcontext_eval","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjun0kada%2Fcontext_eval/lists"}