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

Default value lambda improvements #72

Merged
merged 4 commits into from
May 29, 2020
Merged
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
12 changes: 9 additions & 3 deletions lib/aws-record/record/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def initialize(name, options = {})
@marshaler = options[:marshaler] || DefaultMarshaler
@persist_nil = options[:persist_nil]
dv = options[:default_value]
@default_value_or_lambda = type_cast(dv) unless dv.nil?
unless dv.nil?
@default_value_or_lambda = _is_lambda?(dv) ? dv : type_cast(dv)
end
end

# Attempts to type cast a raw value into the attribute's type. This call
Expand Down Expand Up @@ -92,8 +94,8 @@ def extract(dynamodb_item)

# @api private
def default_value
if @default_value_or_lambda.respond_to?(:call)
@default_value_or_lambda.call
if _is_lambda?(@default_value_or_lambda)
type_cast(@default_value_or_lambda.call)
else
_deep_copy(@default_value_or_lambda)
end
Expand All @@ -104,6 +106,10 @@ def _deep_copy(obj)
Marshal.load(Marshal.dump(obj))
end

def _is_lambda?(obj)
obj.respond_to?(:call)
end

end

# This is an identity marshaler, which performs no changes for type casting
Expand Down
4 changes: 2 additions & 2 deletions lib/aws-record/record/item_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ def build_save_hash

def populate_default_values
@model_attributes.attributes.each do |name, attribute|
unless attribute.default_value.nil?
unless (default_value = attribute.default_value).nil?
if @data[name].nil? && @data[name].nil?
@data[name] = attribute.default_value
@data[name] = default_value
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/aws-record/record/attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ module Record
expect(a.default_value).to eq(5)
end

it 'does not type_cast lambdas' do
m = Marshalers::DateTimeMarshaler.new
a = Attribute.new(:foo, marshaler: m, default_value: -> { Time.now })
dv = a.instance_variable_get("@default_value_or_lambda")
expect(dv.respond_to?(:call)).to eq(true)
end

it 'type casts result of calling a default_value lambda' do
m = Marshalers::StringMarshaler.new
a = Attribute.new(:foo, marshaler: m, default_value: -> { :huzzah })
expect(a.default_value).to be_a(String)
end

it 'uses a deep copy' do
a = Attribute.new(:foo, default_value: {})
a.default_value['greeting'] = 'hi'
Expand Down