[
  {
    "path": ".document",
    "content": "README\nRakefile\ninit.rb\ninstall.rb\nlib/*.rb\nrails/init.rb\ntest/*.rb\n"
  },
  {
    "path": ".gitignore",
    "content": "*~\n*.swp\n"
  },
  {
    "path": "LICENSE",
    "content": "Copyright (c) 2011 Jack Danger Canty\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\nLIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\nWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
  },
  {
    "path": "README",
    "content": "\n\nImmutableAttributes\n===================\n\nWhen you want to prevent certain attributes from being changed once set you can declare them as immutable:\n\nclass MyModel < ActiveRecord::Base\n  attr_immutable :permalink, :param_identifier\nend\n\nWhen MyModel.find(:first).permalink = 'anything' is called it will raise an ImmutableAttributeError\nMyModel.new.permalink = 'works!' will properly set the value because the record is unsaved.\n\nIf you'd only like this to happen for certain conditions, and want to handle other attribute-writers, too (like update_attribute), you can use the validation.\n\nvalidates_immutable :permalink\n\nConfiguration options for the validation:\n* :message - A customer error message (default is: \"can't be changed\")\n* :if - Specifies a method, proc, or string to call to determine if the validation should occure (e.g., :if => :allow_validation or :if => Proc.new{|user| user.signup_step > 2}. The method, proc or string should return or evaluate to a true or false value.\n* :unless - Specifies a method, proc, or string to call to determine if the validation should not occure (e.g., :unless => :skip_validation or :unless => Proc.new{|user| user.signup_step <= 2}. The method, proc or string should return or evaluate to a true or false value.\n\nCreated by Jack Danger Canty @ http://6brand.com\nReleased under the same licence as Rails (MIT)\n\n"
  },
  {
    "path": "Rakefile",
    "content": "require 'rubygems'\nrequire 'rake'\n\nbegin\n  require 'jeweler'\n  Jeweler::Tasks.new do |gem|\n    gem.name = \"immutable_attributes\"\n    gem.summary = %Q{Selected attributes are permanent once a record is created}\n    gem.description = %Q{Allows specified attributes to be freely overwritten _until_ the record is saved for the first time}\n    gem.email = \"gemcutter@jackcanty.com\"\n    gem.homepage = \"http://github.com/JackDanger/immutable_attributes\"\n    gem.authors = [\"Jack Danger Canty\", \"Terry Heath\", \"Nicholas Mertaugh\"]\n  end\nrescue LoadError\n  puts \"Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler\"\nend\n\nrequire 'rake/testtask'\nRake::TestTask.new(:test) do |test|\n  test.libs << '.'\n  test.pattern = 'test/*_test.rb'\n  test.verbose = true\nend\ntask :default => :test\n\n"
  },
  {
    "path": "VERSION",
    "content": "1.2.0"
  },
  {
    "path": "immutable_attributes.gemspec",
    "content": "# Generated by jeweler\n# DO NOT EDIT THIS FILE DIRECTLY\n# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'\n# -*- encoding: utf-8 -*-\n\nGem::Specification.new do |s|\n  s.name = %q{immutable_attributes}\n  s.version = \"1.2.0\"\n\n  s.required_rubygems_version = Gem::Requirement.new(\">= 0\") if s.respond_to? :required_rubygems_version=\n  s.authors = [\"Jack Danger Canty\", \"Terry Heath\", \"Nicholas Mertaugh\"]\n  s.date = %q{2011-07-18}\n  s.description = %q{Allows specified attributes to be freely overwritten _until_ the record is saved for the first time}\n  s.email = %q{gemcutter@jackcanty.com}\n  s.extra_rdoc_files = [\n    \"LICENSE\",\n    \"README\"\n  ]\n  s.files = [\n    \".document\",\n    \"LICENSE\",\n    \"README\",\n    \"Rakefile\",\n    \"VERSION\",\n    \"immutable_attributes.gemspec\",\n    \"init.rb\",\n    \"install.rb\",\n    \"lib/immutable_attributes.rb\",\n    \"rails/init.rb\",\n    \"test/immutable_attributes_test.rb\"\n  ]\n  s.homepage = %q{http://github.com/JackDanger/immutable_attributes}\n  s.require_paths = [\"lib\"]\n  s.rubygems_version = %q{1.4.2}\n  s.summary = %q{Selected attributes are permanent once a record is created}\n\n  if s.respond_to? :specification_version then\n    s.specification_version = 3\n\n    if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then\n    else\n    end\n  else\n  end\nend\n\n"
  },
  {
    "path": "init.rb",
    "content": "# Include hook code here\nrequire 'immutable_attributes'"
  },
  {
    "path": "install.rb",
    "content": "puts IO.read(File.join(File.dirname(__FILE__), 'README'))"
  },
  {
    "path": "lib/immutable_attributes.rb",
    "content": "begin\n  require 'activerecord'\nrescue LoadError\n  require 'active_record'\nend\n\nmodule ImmutableErrors\n  class ImmutableAttributeError < ActiveRecord::ActiveRecordError\n  end\nend\n\nmodule ImmutableAttributes\n  VERSION = \"1.0.3\"\n  def attr_immutable(*args)\n    class_eval do\n      args.each do |attr|\n        define_method(\"#{attr}=\") do |value|\n          new_record? || read_attribute(attr).nil? ?\n            write_attribute(attr, value) :\n            raise(ActiveRecord::ImmutableAttributeError, \"#{attr} is immutable!\")\n        end\n      end\n      # handle ActiveRecord::Base#[]=\n      define_method :[]= do |attr, value|\n        return write_attribute(attr, value) unless args.include?(attr.to_sym)\n        send \"#{attr}=\", value\n      end\n    end\n  end\n\n  def validates_immutable(*attr_names)\n    config = { :on => :update, :if => lambda {|x| true}, :message => \"can't be changed\" }\n    config.update(attr_names.extract_options!)\n\n    @immutables = attr_names\n\n    attr_names.each do |attr|\n      class_eval do\n        define_method(\"original_#{attr}\") do\n          instance_variable_get(\"@original_#{attr}\")\n        end\n      end\n    end\n\n    class_eval do\n      def self.immutables\n        @immutables\n      end\n\n      def after_initialize; end;\n\n      def setup_originals\n        self.class.immutables.each do |attr_name|\n          next unless attribute_names.include? attr_name\n          instance_variable_set(\"@original_#{attr_name}\", send(attr_name.to_s))\n        end\n      end\n      \n      after_initialize :setup_originals\n    end\n\n    validates_each(attr_names, config) do |record, attr_name, value|\n      next if record.send(\"original_#{attr_name.to_s}\").nil?\n      record.errors.add(attr_name, config[:message]) if record.send(\"original_#{attr_name.to_s}\") != record.send(attr_name.to_s)\n    end\n  end\nend\n\nActiveRecord.send :include, ImmutableErrors\nActiveRecord::Base.extend ImmutableAttributes\n"
  },
  {
    "path": "rails/init.rb",
    "content": "# Include hook code here\nrequire 'immutable_attributes'"
  },
  {
    "path": "test/immutable_attributes_test.rb",
    "content": "require 'test/unit'\nrequire 'rubygems'\nrequire 'activerecord'\nrequire File.join(File.dirname(__FILE__), '..', 'lib', 'immutable_attributes')\n\nActiveRecord::Base.establish_connection(\n  :adapter  => \"sqlite3\",\n  :database => \":memory:\"\n)\nActiveRecord::Schema.define do\n  create_table :records do |table|\n    table.column :name, :string\n    table.column :body, :string\n  end\nend\n\nclass Record < ActiveRecord::Base\n  attr_immutable :name\nend\n\nclass ImmutableAttributesTest < Test::Unit::TestCase\n\n  def test_immutable_attribute_can_be_set\n    assert Record.new(:name => 'record name')\n  end\n\n  def test_immutable_attribute_cannot_be_changed_via_mass_setter\n    record = Record.create!(:name => 'record name')\n    assert_raises(ActiveRecord::ImmutableAttributeError) { record.update_attributes(:name => 'new name') }\n  end\n\n  def test_immutable_attribute_cannot_be_changed_via_bracket_setter\n    record = Record.create!(:name => 'record name')\n    assert_raises(ActiveRecord::ImmutableAttributeError) { record[:name] = 'new name' }\n  end\nend\n"
  }
]