Sha256: c75808b8042cccc0c1bb05c25701aefe0578cd122bd7d603171f1c66717c8f75

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module Xpose
  module Controller
    extend ActiveSupport::Concern

    module ClassMethods
      def _expose(**args)
        ::Xpose::Exposed.new(args).tap do |inst|
          @@exposed ||= {}
          @@exposed[inst.conf.name] = inst
          define_method inst.conf.name do
            if instance_variable_defined?(inst.conf.ivar_name)
              instance_variable_get(inst.conf.ivar_name)
            else
              instance_variable_set(inst.conf.ivar_name, inst.exposed_value(self))
            end
          end
          helper_method(inst.conf.name)
        end
      end

      def expose(names, value = nil, **args, &block)
        value = value || args.fetch(:value, nil) || block
        [names].flatten.each { |name| _expose({ name: name, value: value }.merge(args)) }
      end

      def expose!(names, value = nil, **args, &block)
        expose(name, value, args, &block)
        [names].flatten.each { |name| before_action(name) }
      end

      def exposed
        @@exposed ||= {}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xpose-0.1.6 lib/xpose/controller.rb