Sha256: 1773d720af1acc27efaa35c1fe3cc6c42f000f6dfe65cf90de5566bc90416037
Contents?: true
Size: 1.25 KB
Versions: 1
Compression:
Stored size: 1.25 KB
Contents
module Neo module Rails # A really simple version of exposing variables to the view. # Bases on attr_reader and helper_method. module Exposure extend ActiveSupport::Concern class UndeclaredVariableError < StandardError; end included do class_attribute :exposed_vars self.exposed_vars = Set.new end module ClassMethods # Defines the variables to be exposed. def exposes(*names) exposures = names.map(&:to_sym).reject { |name| exposed_vars.include?(name) } self.exposed_vars.merge exposures attr_reader *exposures helper_method *exposures end end # Expose an assign at the instance level. # # If the value is given with a block, just execute the block # if a value was not set yet. # # Raise UndeclaredVariableError if access variable wasn't declared before. def expose(key, value=nil) name = key.to_sym raise UndeclaredVariableError unless self.class.exposed_vars.include?(name) value = yield if block_given? self.instance_variable_set("@#{name}", value) end private def exposed?(name) instance_variable_get("@#{name}") end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
neo-rails-0.0.1 | lib/neo/rails/exposure.rb |