Sha256: 03dc216b6a9b8b044919489fae1832d3b2366cf8405dd7e5ca3394652262f4a0

Contents?: true

Size: 1.21 KB

Versions: 15

Compression:

Stored size: 1.21 KB

Contents

module Disposable
  # Expose allows renaming properties in order to expose a different API.
  # It can be configured from any Representable schema.
  #
  #   class AlbumTwin < Disposable::Twin
  #     property :name, from: :title
  #   end
  #
  #   class AlbumExpose < Disposable::Expose
  #     from AlbumTwin
  #   end
  #
  #   AlbumExpose.new(OpenStruct.new(title: "AFI")).name #=> "AFI"
  class Expose
    class << self
      def from(representer)
        representer.representable_attrs.each do |definition|
          process_definition!(definition)
        end
        self
      end

    private
      def process_definition!(definition)
        public_name  = definition.name
        private_name = definition[:private_name] || public_name

        accessors!(public_name, private_name, definition)
      end

      def accessors!(public_name, private_name, definition)
        define_method("#{public_name}")  { @model.send("#{private_name}") }
        define_method("#{public_name}=") { |*args| @model.send("#{private_name}=", *args) }
      end
    end


    def initialize(model)
      @model = model
    end

    module Save
      def save
        @model.save # FIXME: block?
      end
    end
    include Save
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
disposable-0.1.15 lib/disposable/expose.rb
disposable-0.1.14 lib/disposable/expose.rb
disposable-0.1.13 lib/disposable/expose.rb
disposable-0.1.12 lib/disposable/expose.rb
disposable-0.1.11 lib/disposable/expose.rb
disposable-0.1.9 lib/disposable/expose.rb
disposable-0.1.8 lib/disposable/expose.rb
disposable-0.1.7 lib/disposable/expose.rb
disposable-0.1.6 lib/disposable/expose.rb
disposable-0.1.5 lib/disposable/expose.rb
disposable-0.1.4 lib/disposable/expose.rb
disposable-0.1.3 lib/disposable/expose.rb
disposable-0.1.2 lib/disposable/expose.rb
disposable-0.1.1 lib/disposable/expose.rb
disposable-0.1.0 lib/disposable/expose.rb