module SimpleExposure::Core::ClassMethods

Public Instance Methods

_exposure_extension_class(extension) click to toggle source
# File lib/simple_exposure/core.rb, line 49
def _exposure_extension_class(extension)
  Extensions.const_get(extension.camelize)
rescue NameError
  raise UnknownExtension, "Unknown extension: #{extension}"
end
_exposure_extensions() click to toggle source
# File lib/simple_exposure/core.rb, line 45
def _exposure_extensions
  @_exposure_extensions ||= HashWithIndifferentAccess.new([])
end
expose(*attributes, &block) click to toggle source
# File lib/simple_exposure/core.rb, line 35
def expose(*attributes, &block)
  options = attributes.extract_options!

  extensions = options.fetch(:extend, nil)
  extensions = Array(extensions)

  _define_exposure_accessors(attributes, &block)
  _define_exposure_extensions(attributes, extensions)
end

Private Instance Methods

_define_exposure_accessors(attributes, &block) click to toggle source
# File lib/simple_exposure/core.rb, line 57
def _define_exposure_accessors(attributes, &block)
  _define_exposure_readers(attributes, &block)
  _define_exposure_writers(attributes)
end
_define_exposure_extensions(attributes, extensions) click to toggle source
# File lib/simple_exposure/core.rb, line 91
def _define_exposure_extensions(attributes, extensions)
  attributes.each do |attribute|
    _exposure_extensions[attribute] += extensions
  end
end
_define_exposure_reader(attribute, &block) click to toggle source
# File lib/simple_exposure/core.rb, line 68
def _define_exposure_reader(attribute, &block)
  if block
    _define_exposure_reader_with_defaults(attribute, &block)
  else
    attr_reader(attribute)
  end

  helper_method(attribute)
  hide_action(attribute)
end
_define_exposure_reader_with_defaults(attribute, &block) click to toggle source
# File lib/simple_exposure/core.rb, line 79
def _define_exposure_reader_with_defaults(attribute, &block)
  define_method(attribute) do
    value = instance_variable_get("@#{attribute}")
    return value if value
    send :"#{attribute}=", instance_eval(&block)
  end
end
_define_exposure_readers(attributes, &block) click to toggle source
# File lib/simple_exposure/core.rb, line 62
def _define_exposure_readers(attributes, &block)
  attributes.each do |attribute|
    _define_exposure_reader(attribute, &block)
  end
end
_define_exposure_writers(attributes) click to toggle source
# File lib/simple_exposure/core.rb, line 87
def _define_exposure_writers(attributes)
  attr_writer(*attributes)
end
method_missing(extension, *attributes, &block) click to toggle source
Calls superclass method
# File lib/simple_exposure/core.rb, line 97
def method_missing(extension, *attributes, &block)
  extension_class = _exposure_extension_class(extension.to_s)
  if extension_class
    options = attributes.extract_options!

    extensions = options.fetch(:extend, nil)
    extensions = Array(extensions)
    extensions = extensions.prepend(extension)

    options = options.merge(extend: extensions)

    expose(*attributes, options, &block)
  end
rescue UnknownExtension
  super
end
respond_to_missing?(extension, include_private = false) click to toggle source
# File lib/simple_exposure/core.rb, line 114
def respond_to_missing?(extension, include_private = false)
  _exposure_extension_class(extension.to_s)
rescue UnknownExtension
  false
end