Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typecast on writing #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/active_attr/attribute_defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ module AttributeDefaults
def apply_defaults(defaults=attribute_defaults)
@attributes ||= {}
defaults.each do |name, value|
# instance variable is used here to avoid any dirty tracking in attribute setter methods
@attributes[name] = value unless @attributes.has_key? name
write_attribute(name, value) unless @attributes.has_key? name
end
end

Expand Down
14 changes: 8 additions & 6 deletions lib/active_attr/typecasted_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ module TypecastedAttributes
#
# @since 0.5.0
def attribute_before_type_cast(name)
@attributes ||= {}
@attributes[name.to_s]
@before_type_cast_attributes ||= {}
@before_type_cast_attributes[name.to_s]
end

private

# Reads the attribute and typecasts the result
# Write the attribute and typecasts the result
#
# @since 0.5.0
def attribute(name)
typecast_attribute(_attribute_typecaster(name), super)
# @since 0.9.0
def attribute=(name, value)
@before_type_cast_attributes ||= {}
@before_type_cast_attributes[name] = value
super(name, typecast_attribute(_attribute_typecaster(name), value))
end

# Calculates an attribute type
Expand Down
3 changes: 1 addition & 2 deletions spec/functional/active_attr/typecasted_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def self.to_s
end

it "an attribute with no known typecaster raises" do
model.unknown = nil
expect { model.unknown }.to raise_error Typecasting::UnknownTypecasterError, "Unable to cast to type Unknown"
expect { model.unknown = nil }.to raise_error Typecasting::UnknownTypecasterError, "Unable to cast to type Unknown"
end

it "an attribute with an inline typecaster returns nil" do
Expand Down