Skip to content

Commit

Permalink
Support floats and add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
fastdivision committed Nov 19, 2015
1 parent 6660266 commit a9387ac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/model_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require "time"

module ModelAttribute
SUPPORTED_TYPES = [:integer, :boolean, :string, :time, :json]
SUPPORTED_TYPES = [:integer, :float, :boolean, :string, :time, :json]

def self.extended(base)
base.send(:include, InstanceMethods)
Expand Down
2 changes: 2 additions & 0 deletions lib/model_attribute/casts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def cast(value, type)
float = Float(value)
raise ArgumentError, "Can't cast #{value.inspect} to an integer without loss of precision" unless int == float
int
when :float
Float(value)
when :boolean
if !!value == value
value
Expand Down
22 changes: 18 additions & 4 deletions spec/model_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class User
attribute :created_at, :time
attribute :profile, :json
attribute :reward_points, :integer, default: 0
attribute :win_rate, :float

def initialize(attributes = {})
set_attributes(attributes)
Expand All @@ -32,14 +33,14 @@ def initialize(attributes = {})
User.attribute :address, :custom_type
end.to raise_error(ModelAttribute::UnsupportedTypeError,
"Unsupported type :custom_type. " +
"Must be one of :integer, :boolean, :string, :time, :json.")
"Must be one of :integer, :float, :boolean, :string, :time, :json.")
end
end
end

describe ".attributes" do
it "returns an array of attribute names as symbols" do
expect(User.attributes).to eq([:id, :paid, :name, :created_at, :profile, :reward_points])
expect(User.attributes).to eq([:id, :paid, :name, :created_at, :profile, :reward_points, :win_rate])
end
end

Expand Down Expand Up @@ -94,6 +95,18 @@ def initialize(attributes = {})
end
end

describe "a float attribute (win_rate)" do
it "stores a float" do
user.win_rate = 35.62
expect(user.win_rate).to eq(35.62)
end

it "parses a float string" do
user.win_rate = 35.62
expect(user.win_rate).to eq(35.62)
end
end

describe "a boolean attribute (paid)" do
it "is nil when unset" do
expect(user.paid).to be_nil
Expand Down Expand Up @@ -548,7 +561,8 @@ def initialize(attributes = {})
name: "Fred",
created_at: "2014-12-25 08:00",
paid: true,
profile: {'interests' => ['coding', 'social networks'], 'rank' => 15})
profile: {'interests' => ['coding', 'social networks'], 'rank' => 15},
win_rate: 35.62)
end

it "includes integer attributes as 'name: value'" do
Expand Down Expand Up @@ -580,7 +594,7 @@ def initialize(attributes = {})
end

it "looks like '#<User id: 1, paid: true, name: ..., created_at: ...>'" do
expect(user.inspect).to eq("#<User id: 1, paid: true, name: \"Fred\", created_at: 2014-12-25 08:00:00 +0000, profile: {\"interests\"=>[\"coding\", \"social networks\"], \"rank\"=>15}, reward_points: 0>")
expect(user.inspect).to eq("#<User id: 1, paid: true, name: \"Fred\", created_at: 2014-12-25 08:00:00 +0000, profile: {\"interests\"=>[\"coding\", \"social networks\"], \"rank\"=>15}, reward_points: 0, win_rate: 35.62>")
end
end

Expand Down

0 comments on commit a9387ac

Please sign in to comment.