1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add simple attribute implementation backed by ivars

This commit is contained in:
Joshua Peek 2009-06-17 20:36:21 -05:00
parent d5d59230f4
commit af5301089f
3 changed files with 48 additions and 0 deletions

View file

@ -26,6 +26,7 @@ $:.unshift(activesupport_path) if File.directory?(activesupport_path)
require 'active_support' require 'active_support'
module ActiveModel module ActiveModel
autoload :Attributes, 'active_model/attributes'
autoload :Base, 'active_model/base' autoload :Base, 'active_model/base'
autoload :DeprecatedErrorMethods, 'active_model/deprecated_error_methods' autoload :DeprecatedErrorMethods, 'active_model/deprecated_error_methods'
autoload :Errors, 'active_model/errors' autoload :Errors, 'active_model/errors'

View file

@ -0,0 +1,17 @@
require 'active_support/core_ext/object/instance_variables'
module ActiveModel
module Attributes
def attributes
instance_values
end
def read_attribute(attr_name)
instance_variable_get(:"@#{attr_name}")
end
def write_attribute(attr_name, value)
instance_variable_set(:"@#{attr_name}", value)
end
end
end

View file

@ -0,0 +1,30 @@
require 'cases/helper'
class AttributesTest < ActiveModel::TestCase
class Person
include ActiveModel::Attributes
attr_accessor :name
end
test "reads attribute" do
p = Person.new
assert_equal nil, p.read_attribute(:name)
p.name = "Josh"
assert_equal "Josh", p.read_attribute(:name)
end
test "writes attribute" do
p = Person.new
assert_equal nil, p.name
p.write_attribute(:name, "Josh")
assert_equal "Josh", p.name
end
test "returns all attributes" do
p = Person.new
p.name = "Josh"
assert_equal({"name" => "Josh"}, p.attributes)
end
end