Sha256: d08cac7b249cd9b2855045b52c7b1623ec3e9f948bf0c26065420464a966d3b6

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/string/inflections'

module ActiveCopy
  # Attribute storage and handling for +ActiveCopy+ models.
  module Attributes
    extend ActiveSupport::Concern

    DEFAULT_PATH = "public/#{self.class.name.parameterize.pluralize}"
    DEFAULT_ATTRS = [:layout]

    included do
      class_attribute :_accessible_attributes
      class_attribute :_deployment_path
    end

    module ClassMethods
      def attr_accessible(*args)
        if self._accessible_attributes.nil?
          self._accessible_attributes = []
        end

        args.each do |attribute|
          self._accessible_attributes << attribute
        end
      end

      def deploy_to file_path
        self._deployment_path = file_path
      end

      def accessible_attrs
        return DEFAULT_ATTRS if self._accessible_attributes.nil?
        self._accessible_attributes += DEFAULT_ATTRS
      end

      def deployment_path
        self._deployment_path || "#{DEFAULT_PATH}/#{self.id}.html"
      end
    end

    # Take YAML front matter given by id.
    def attributes
      @attributes ||= yaml_front_matter.with_indifferent_access
    end

    protected
    def attribute? key
      self.class.accessible_attrs.include? key.to_sym
    end

    private
    def yaml_front_matter
      HashWithIndifferentAccess.new \
        YAML::load(raw_source.split("---\n")[1])
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_copy-1.0.0 lib/active_copy/attributes.rb
active_copy-1.0.0.pre lib/active_copy/attributes.rb