Skip to content

Commit

Permalink
chore: use standard
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Apr 10, 2019
1 parent ed5c43a commit f1d0496
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 126 deletions.
67 changes: 35 additions & 32 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
require:
- standard/cop/semantic_blocks
- rubocop-md

inherit_gem:
standard: config/base.yml

AllCops:
# Include gemspec and Rakefile
Include:
- 'lib/**/*.rb'
- 'lib/**/*.rake'
- 'spec/**/*.rb'
Exclude:
- 'bin/**/*'
- 'spec/dummy/**/*'
- 'bin/*'
- 'tmp/**/*'
- 'Gemfile'
- 'vendor/**/*'
- 'gemfiles/**/*'
- 'bench/**/*'
DisplayCopNames: true
StyleGuideCopsOnly: false
TargetRubyVersion: 2.4

Style/AccessorMethodName:
Standard/SemanticBlocks:
Enabled: false

Style/TrivialAccessors:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: true

Style/Documentation:
Exclude:
- 'spec/**/*.rb'
Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: no_comma

Style/StringLiterals:
Enabled: false
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: no_comma

Style/SpaceInsideStringInterpolation:
EnforcedStyle: no_space
Layout/AlignParameters:
EnforcedStyle: with_first_parameter

Style/BlockDelimiters:
Lint/Void:
Exclude:
- 'spec/**/*.rb'
- '**/*.md'

Lint/AmbiguousRegexpLiteral:
Enabled: false

Metrics/MethodLength:
# See https://github.com/rubocop-hq/rubocop/issues/4222
Lint/AmbiguousBlockAssociation:
Exclude:
- 'spec/**/*.rb'
- 'spec/**/*'
- '**/*.md'

Metrics/LineLength:
Max: 100
Lint/DuplicateMethods:
Exclude:
- 'spec/**/*.rb'
- '**/*.md'

Rails/Date:
Enabled: false
Naming/FileName:
Exclude:
- '**/*.md'

Rails/TimeZone:
Enabled: false
Layout/InitialIndentation:
Exclude:
- 'CHANGELOG.md'
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

ActiveRecord extension which adds typecasting to store accessors.

Compatible with Rails 4.2 and Rails 5.
Compatible with Rails 4.2 and Rails 5+.

<a href="https://evilmartians.com/">
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
Expand All @@ -14,19 +14,19 @@ Compatible with Rails 4.2 and Rails 5.
In your Gemfile:

```ruby
# for Rails 5
gem "store_attribute", "~>0.5.0"
# for Rails 5+ (6 is supported)
gem "store_attribute", "~> 0.5.0"

# for Rails 4.2
gem "store_attribute", "~>0.4.0"
gem "store_attribute", "~> 0.4.0"
```

### Usage

You can use `store_attribute` method to add additional accessors with a type to an existing store on a model.

```ruby
store_attribute(store_name, name, type, options = {})
store_attribute(store_name, name, type, options = {})
```

Where:
Expand All @@ -49,28 +49,28 @@ class MegaUser < User
store_attribute :settings, :active, :boolean
end

u = MegaUser.new(active: false, login_at: '2015-01-01 00:01', ratio: "63.4608")
u = MegaUser.new(active: false, login_at: "2015-01-01 00:01", ratio: "63.4608")

u.login_at.is_a?(DateTime) # => true
u.login_at = DateTime.new(2015,1,1,11,0,0)
u.login_at = DateTime.new(2015, 1, 1, 11, 0, 0)
u.ratio # => 63
u.active # => false
# And we also have a predicate method
u.active? # => false
u.reload

# After loading record from db store contains casted data
u.settings['login_at'] == DateTime.new(2015,1,1,11,0,0) # => true
u.settings["login_at"] == DateTime.new(2015, 1, 1, 11, 0, 0) # => true

# If you update store explicitly then the value returned
# by accessor isn't type casted
u.settings['ratio'] = "3.141592653"
u.settings["ratio"] = "3.141592653"
u.ratio # => "3.141592653"

# On the other hand, writing through accessor set correct data within store
u.ratio = "3.141592653"
u.ratio # => 3
u.settings['ratio'] # => 3
u.settings["ratio"] # => 3
```

You can also specify type using usual `store_accessor` method:
Expand Down
8 changes: 6 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"
require "rubocop/rake_task"

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new

task :default => :spec
task default: [:rubocop, :spec]
6 changes: 4 additions & 2 deletions lib/store_attribute.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
require 'store_attribute/version'
require 'store_attribute/active_record'
# frozen_string_literal: true

require "store_attribute/version"
require "store_attribute/active_record"
4 changes: 3 additions & 1 deletion lib/store_attribute/active_record.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
require 'store_attribute/active_record/store'
# frozen_string_literal: true

require "store_attribute/active_record/store"
7 changes: 5 additions & 2 deletions lib/store_attribute/active_record/store.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'active_record/store'
require 'store_attribute/active_record/type/typed_store'
# frozen_string_literal: true

require "active_record/store"
require "store_attribute/active_record/type/typed_store"

module ActiveRecord
module Store
Expand All @@ -26,6 +28,7 @@ def store(store_name, options = {})
_orig_store(store_name, options)
store_accessor(store_name, *accessors) if accessors
end

# Adds additional accessors to an existing store on this model.
#
# +store_name+ The name of the store.
Expand Down
4 changes: 3 additions & 1 deletion lib/store_attribute/active_record/type/typed_store.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'active_record/type'
# frozen_string_literal: true

require "active_record/type"

module ActiveRecord
module Type # :nodoc:
Expand Down
4 changes: 3 additions & 1 deletion lib/store_attribute/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module StoreAttribute # :nodoc:
VERSION = "0.5.2".freeze
VERSION = "0.5.2"
end
68 changes: 35 additions & 33 deletions spec/cases/store_attribute_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'spec_helper'
# frozen_string_literal: true

require "spec_helper"

describe StoreAttribute do
before do
@connection = ActiveRecord::Base.connection

@connection.transaction do
@connection.create_table('users') do |t|
@connection.create_table("users") do |t|
t.jsonb :jparams, default: {}, null: false
t.text :custom
t.hstore :hdata, default: {}, null: false
Expand All @@ -16,23 +18,23 @@
end

after do
@connection.drop_table 'users', if_exists: true
@connection.drop_table "users", if_exists: true
end

let(:time) { DateTime.new(2015, 2, 14, 17, 0, 0) }
let(:time_str) { '2015-02-14 17:00' }
let(:time_str_utc) { '2015-02-14 17:00:00 UTC' }
let(:time_str) { "2015-02-14 17:00" }
let(:time_str_utc) { "2015-02-14 17:00:00 UTC" }

context "hstore" do
it "typecasts on build" do
user = User.new(visible: 't', login_at: time_str)
user = User.new(visible: "t", login_at: time_str)
expect(user.visible).to eq true
expect(user).to be_visible
expect(user.login_at).to eq time
end

it "typecasts on reload" do
user = User.new(visible: 't', login_at: time_str)
user = User.new(visible: "t", login_at: time_str)
user.save!
user = User.find(user.id)

Expand All @@ -54,17 +56,17 @@
expect(user.login_at).to eq time

ron = RawUser.find(user.id)
expect(ron.hdata['visible']).to eq 'false'
expect(ron.hdata['login_at']).to eq time_str_utc
expect(ron.hdata["visible"]).to eq "false"
expect(ron.hdata["login_at"]).to eq time_str_utc
end

it "handles options" do
expect { User.create!(ratio: 1024) }.to raise_error(RangeError)
end

it "YAML roundtrip" do
user = User.create!(visible: '0', login_at: time_str)
dumped = YAML.load(YAML.dump(user))
user = User.create!(visible: "0", login_at: time_str)
dumped = YAML.safe_load(YAML.dump(user))

expect(dumped.visible).to be false
expect(dumped.login_at).to eq time
Expand All @@ -74,46 +76,46 @@
context "jsonb" do
it "typecasts on build" do
jamie = User.new(
active: 'true',
active: "true",
salary: 3.1999,
birthday: '2000-01-01'
birthday: "2000-01-01"
)
expect(jamie).to be_active
expect(jamie.salary).to eq 3
expect(jamie.birthday).to eq Date.new(2000, 1, 1)
expect(jamie.jparams['birthday']).to eq Date.new(2000, 1, 1)
expect(jamie.jparams['active']).to eq true
expect(jamie.jparams["birthday"]).to eq Date.new(2000, 1, 1)
expect(jamie.jparams["active"]).to eq true
end

it "typecasts on reload" do
jamie = User.create!(jparams: { 'active' => '1', 'birthday' => '01/01/2000', 'salary' => '3.14' })
jamie = User.create!(jparams: {"active" => "1", "birthday" => "01/01/2000", "salary" => "3.14"})
jamie = User.find(jamie.id)

expect(jamie).to be_active
expect(jamie.salary).to eq 3
expect(jamie.birthday).to eq Date.new(2000, 1, 1)
expect(jamie.jparams['birthday']).to eq Date.new(2000, 1, 1)
expect(jamie.jparams['active']).to eq true
expect(jamie.jparams["birthday"]).to eq Date.new(2000, 1, 1)
expect(jamie.jparams["active"]).to eq true
end

it "works with accessors" do
john = User.new
john.active = 1

expect(john).to be_active
expect(john.jparams['active']).to eq true
expect(john.jparams["active"]).to eq true

john.jparams = { active: 'true', salary: '123.123', birthday: '01/01/2012' }
john.jparams = {active: "true", salary: "123.123", birthday: "01/01/2012"}
expect(john).to be_active
expect(john.birthday).to eq Date.new(2012, 1, 1)
expect(john.salary).to eq 123

john.save!

ron = RawUser.find(john.id)
expect(ron.jparams['active']).to eq true
expect(ron.jparams['birthday']).to eq '2012-01-01'
expect(ron.jparams['salary']).to eq 123
expect(ron.jparams["active"]).to eq true
expect(ron.jparams["birthday"]).to eq "2012-01-01"
expect(ron.jparams["salary"]).to eq 123
end

it "re-typecast old data" do
Expand All @@ -127,8 +129,8 @@
jamie.save!

ron = RawUser.find(jamie.id)
expect(ron.jparams['active']).to eq true
expect(ron.jparams['salary']).to eq 12
expect(ron.jparams["active"]).to eq true
expect(ron.jparams["salary"]).to eq 12
end
end

Expand All @@ -139,7 +141,7 @@
end

it "typecasts on reload" do
jamie = User.create!(custom: { price: '$12' })
jamie = User.create!(custom: {price: "$12"})
expect(jamie.reload.price).to eq 1200

jamie = User.find(jamie.id)
Expand All @@ -150,22 +152,22 @@

context "store subtype" do
it "typecasts on build" do
user = User.new(inner_json: { x: 1 })
expect(user.inner_json).to eq('x' => 1)
user = User.new(inner_json: {x: 1})
expect(user.inner_json).to eq("x" => 1)
end

it "typecasts on update" do
user = User.new
user.update!(inner_json: { x: 1 })
expect(user.inner_json).to eq('x' => 1)
user.update!(inner_json: {x: 1})
expect(user.inner_json).to eq("x" => 1)

expect(user.reload.inner_json).to eq('x' => 1)
expect(user.reload.inner_json).to eq("x" => 1)
end

it "typecasts on reload" do
jamie = User.create!(inner_json: { x: 1 })
jamie = User.create!(inner_json: {x: 1})
jamie = User.find(jamie.id)
expect(jamie.inner_json).to eq('x' => 1)
expect(jamie.inner_json).to eq("x" => 1)
end
end
end
Loading

0 comments on commit f1d0496

Please sign in to comment.