Sha256: 7aedbbc3bd51619b48dded3ae42054f8e56141654316a35ca4a4e266ac74e4de

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require_relative "scrivener/validations"

class Scrivener
  VERSION = "0.1.0"

  include Validations

  # Initialize with a hash of attributes and values.
  # If extra attributes are sent, a NoMethodError exception will be raised.
  #
  # @example
  #
  #   class EditPost < Scrivener
  #     attr_accessor :title
  #     attr_accessor :body
  #
  #     def validate
  #       assert_present :title
  #       assert_present :body
  #     end
  #   end
  #
  #   edit = EditPost.new(title: "Software Tools")
  #
  #   edit.valid? #=> false
  #
  #   edit.errors[:title] #=> []
  #   edit.errors[:body]  #=> [:not_present]
  #
  #   edit.body = "Recommended reading..."
  #
  #   edit.valid? #=> true
  #
  #   # Now it's safe to initialize the model.
  #   post = Post.new(edit.attributes)
  #   post.save
  def initialize(atts)
    atts.each do |key, val|
      send(:"#{key}=", val)
    end
  end

  # Return hash of attributes and values.
  def attributes
    Hash.new.tap do |atts|
      instance_variables.each do |ivar|
        next if ivar == :@errors

        att = ivar[1..-1].to_sym
        atts[att] = send(att)
      end
    end
  end

  def slice(*keys)
    Hash.new.tap do |atts|
      keys.each do |att|
        atts[att] = send(att)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scrivener-0.1.0 lib/scrivener.rb