Skip to content

Commit

Permalink
removed array and hash types, bumped version
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Crismali committed Feb 10, 2015
1 parent f81de11 commit 8e7db4f
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 72 deletions.
2 changes: 1 addition & 1 deletion hstore_accessor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rubocop"
spec.add_development_dependency "shoulda-matchers"

spec.post_install_message = "Please note that the `array` and `hash` types will no longer be supported in version 1.0.0"
spec.post_install_message = "Please note that the `array` and `hash` types are no longer supported in version 1.0.0"
end
2 changes: 1 addition & 1 deletion lib/hstore_accessor/active_record_4.2/type_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def cast(type, value)
return nil if value.nil?

case type
when :string, :hash, :array, :decimal
when :string, :decimal
value
when :integer, :float, :datetime, :date, :boolean
TYPES[type].new.type_cast_from_user(value)
Expand Down
2 changes: 1 addition & 1 deletion lib/hstore_accessor/active_record_<_4.2/type_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def cast(type, value)
column_class = ActiveRecord::ConnectionAdapters::Column

case type
when :string, :hash, :array, :decimal
when :string, :decimal
value
when :integer
column_class.value_to_integer(value)
Expand Down
3 changes: 0 additions & 3 deletions lib/hstore_accessor/macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ def hstore_accessor(hstore_attribute, fields)
when :boolean
send(:scope, "is_#{key}", -> { where(eq_query_field, "true") })
send(:scope, "not_#{key}", -> { where(eq_query_field, "false") })
when :array
send(:scope, "#{key}_eq", -> value { where("#{query_field} = ?", value.join(Serialization::SEPARATOR)) })
send(:scope, "#{key}_contains", -> value { where("string_to_array(#{query_field}, '#{Serialization::SEPARATOR}') @> string_to_array(?, '#{Serialization::SEPARATOR}')", Array[value].flatten.join(Serialization::SEPARATOR)) })
end
end

Expand Down
8 changes: 0 additions & 8 deletions lib/hstore_accessor/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,30 @@ module Serialization
InvalidDataTypeError = Class.new(StandardError)

VALID_TYPES = [
:array,
:boolean,
:date,
:datetime,
:decimal,
:float,
:hash,
:integer,
:string
]

DEFAULT_SERIALIZER = ->(value) { value.to_s }
DEFAULT_DESERIALIZER = DEFAULT_SERIALIZER

SEPARATOR = "||;||"

SERIALIZERS = {
array: -> value { (value && value.join(SEPARATOR)) || nil },
boolean: -> value { (value.to_s == "true").to_s },
date: -> value { value && value.to_s },
hash: -> value { (value && value.to_json) || nil },
datetime: -> value { value && value.to_i }
}
SERIALIZERS.default = DEFAULT_SERIALIZER

DESERIALIZERS = {
array: -> value { (value && value.split(SEPARATOR)) || nil },
boolean: -> value { TypeHelpers.cast(:boolean, value) },
date: -> value { value && Date.parse(value) },
decimal: -> value { value && BigDecimal.new(value) },
float: -> value { value && value.to_f },
hash: -> value { (value && JSON.parse(value)) || nil },
integer: -> value { value && value.to_i },
datetime: -> value { value && Time.at(value.to_i).in_time_zone }
}
Expand Down
2 changes: 1 addition & 1 deletion lib/hstore_accessor/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module HstoreAccessor
VERSION = "0.9.0"
VERSION = "1.0.0"
end
60 changes: 5 additions & 55 deletions spec/hstore_accessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
weight: { data_type: :float, store_key: "w" },
popular: :boolean,
build_timestamp: :datetime,
tags: :array,
reviews: :hash,
released_at: :date,
miles: :decimal
}
Expand Down Expand Up @@ -77,7 +75,7 @@ class FakeModel
let!(:timestamp) { Time.now }
let!(:datestamp) { Date.today }
let(:product) { Product.new }
let(:persisted_product) { Product.create!(color: "green", price: 10, weight: 10.1, tags: %w(tag1 tag2 tag3), popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days), miles: BigDecimal.new("9.133790001")) }
let(:persisted_product) { Product.create!(color: "green", price: 10, weight: 10.1, popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days), miles: BigDecimal.new("9.133790001")) }

FIELDS.keys.each do |field|
it "responds with nil when #{field} is not set" do
Expand Down Expand Up @@ -147,9 +145,9 @@ class FakeModel
describe "scopes" do
let!(:timestamp) { Time.now }
let!(:datestamp) { Date.today }
let!(:product_a) { Product.create(color: "green", price: 10, weight: 10.1, tags: %w(tag1 tag2 tag3), popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days), miles: BigDecimal.new("10.113379001")) }
let!(:product_b) { Product.create(color: "orange", price: 20, weight: 20.2, tags: %w(tag2 tag3 tag4), popular: false, build_timestamp: (timestamp - 5.days), released_at: (datestamp - 4.days), miles: BigDecimal.new("20.213379001")) }
let!(:product_c) { Product.create(color: "blue", price: 30, weight: 30.3, tags: %w(tag3 tag4 tag5), popular: true, build_timestamp: timestamp, released_at: datestamp, miles: BigDecimal.new("30.313379001")) }
let!(:product_a) { Product.create(color: "green", price: 10, weight: 10.1, popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days), miles: BigDecimal.new("10.113379001")) }
let!(:product_b) { Product.create(color: "orange", price: 20, weight: 20.2, popular: false, build_timestamp: (timestamp - 5.days), released_at: (datestamp - 4.days), miles: BigDecimal.new("20.213379001")) }
let!(:product_c) { Product.create(color: "blue", price: 30, weight: 30.3, popular: true, build_timestamp: timestamp, released_at: datestamp, miles: BigDecimal.new("30.313379001")) }

context "for string fields support" do
it "equality" do
Expand Down Expand Up @@ -223,20 +221,7 @@ class FakeModel
end
end

context "for array fields support" do
it "equality" do
expect(Product.tags_eq(%w(tag1 tag2 tag3)).to_a).to eq [product_a]
end

it "contains" do
expect(Product.tags_contains("tag2").to_a).to eq [product_a, product_b]
expect(Product.tags_contains(%w(tag2 tag3)).to_a).to eq [product_a, product_b]
expect(Product.tags_contains(%w(tag1 tag2 tag3)).to_a).to eq [product_a]
expect(Product.tags_contains(%w(tag1 tag2 tag3 tag4)).to_a).to eq []
end
end

context "for time fields support" do
context "for datetime fields support" do
it "before" do
expect(Product.build_timestamp_before(timestamp)).to eq [product_a, product_b]
end
Expand Down Expand Up @@ -323,8 +308,6 @@ def self.it_returns_the_properly_typed_column(type, attribute_name, cast_type_cl
it_returns_the_properly_typed_column :datetime, :build_timestamp, ActiveRecord::Type::DateTime
it_returns_the_properly_typed_column :date, :released_at, ActiveRecord::Type::Date
it_returns_the_properly_typed_column :decimal, :miles, ActiveRecord::Type::Decimal
it_returns_the_properly_typed_column :array, :tags, ActiveRecord::Type::Value
it_returns_the_properly_typed_column :hash, :reviews, ActiveRecord::Type::Value
else
def self.it_returns_the_properly_typed_column(hstore_type, attribute_name, active_record_type)
context "#{hstore_type}" do
Expand Down Expand Up @@ -388,39 +371,6 @@ def self.it_returns_the_properly_typed_column(hstore_type, attribute_name, activ
expect(product.weight).to eq 93.45
end

context "array values" do
it "correctly stores nothing" do
product.tags = nil
product.save
product.reload
expect(product.tags).to be_nil
end

it "correctly stores strings" do
product.tags = ["household", "living room", "kitchen"]
product.save
product.reload
expect(product.tags).to eq ["household", "living room", "kitchen"]
end
end

context "hash values" do
it "correctly stores nothing" do
product.reviews = nil
product.save
product.reload
expect(product.reviews).to be_nil
end

it "correctly stores hash values as json" do
hash = product.reviews = { "user_123" => "4 stars", "user_994" => "3 stars" }
product.save
product.reload
expect(product.reviews).to eq("user_123" => "4 stars", "user_994" => "3 stars")
expect(product.options["reviews"]).to eq(hash.to_json)
end
end

context "multipart values" do
it "stores multipart dates correctly" do
product.update_attributes!(
Expand Down
2 changes: 0 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ def create_database
t.boolean :boolean_type
t.float :float_type
t.time :time_type
t.string :array_type, array: true
t.date :date_type
t.datetime :datetime_type
t.decimal :decimal_type
t.hstore :hash_type
end
end

0 comments on commit 8e7db4f

Please sign in to comment.