Sha256: 150aa6fa3581dd4eb2463b8efa11dde4cff74a89133f49a0b3e1cd49dd7445fc

Contents?: true

Size: 1.12 KB

Versions: 11

Compression:

Stored size: 1.12 KB

Contents

begin
  require "active_model"
rescue LoadError
end

if defined?(ActiveModel)
  module Frenchy
    # Veneer provides a friendly face on unfriendly models, allowing your Frenchy
    # models to appear as though they were of another class.
    module Veneer
      def self.included(base)
        if defined?(ActiveModel)
          base.extend(ClassMethods)
        end
      end

      module ClassMethods
        # Macro to establish a veneer for a given model
        def veneer(options={})
          options.stringify_keys!
          @model = options.delete("model") || raise(Frenchy::Error, "Veneer must specify a model")
          extend ActiveModel::Naming

          class_eval do
            def self.model_name
              ActiveModel::Name.new(self, nil, @model.to_s.camelize)
            end

            def self.table_name
              @model.to_s.pluralize
            end
          end

          define_method(:record_key) do
            raise(Frenchy::Error, "No primary key is specified") unless respond_to?(:to_param)
            "#{self.class.table_name}/#{to_param}"
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
frenchy-0.7.0 lib/frenchy/veneer.rb
frenchy-0.6.3 lib/frenchy/veneer.rb
frenchy-0.6.2 lib/frenchy/veneer.rb
frenchy-0.6.1 lib/frenchy/veneer.rb
frenchy-0.6.0 lib/frenchy/veneer.rb
frenchy-0.5.2 lib/frenchy/veneer.rb
frenchy-0.5.1 lib/frenchy/veneer.rb
frenchy-0.5.0 lib/frenchy/veneer.rb
frenchy-0.4.0 lib/frenchy/veneer.rb
frenchy-0.3.0 lib/frenchy/veneer.rb
frenchy-0.2.9 lib/frenchy/veneer.rb