Skip to content

Commit

Permalink
Fix #264; rename "model" filter to "object"
Browse files Browse the repository at this point in the history
  • Loading branch information
tfausak committed Mar 16, 2015
1 parent 5c4bd7c commit 26c6b91
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 45 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Read more on [the project page][] or check out [the full documentation][].
- [File](#file)
- [Hash](#hash)
- [Interface](#interface)
- [Model](#model)
- [Object](#object)
- [String](#string)
- [Symbol](#symbol)
- [Dates and times](#dates-and-times)
Expand Down Expand Up @@ -389,11 +389,11 @@ InterfaceInteraction.run!(serializer: JSON)
# => "{\"is_json\":true}"
```

### Model
### Object

Model filters allow you to require an instance of a particular class. It checks
either `#is_a?` on the instance or `.===` on the class. Because of that, it
also works with classes that have mixed modules in with `include`.
Object filters allow you to require an instance of a particular class. It
checks either `#is_a?` on the instance or `.===` on the class. Because of that,
it also works with classes that have mixed modules in with `include`.

``` rb
class Cow
Expand All @@ -403,15 +403,15 @@ class Cow
end

class ModelInteraction < ActiveInteraction::Base
model :cow
object :cow

def execute
cow.moo
end
end

ModelInteraction.run!(cow: Object.new)
# ActiveInteraction::InvalidInteractionError: Cow is not a valid model
# ActiveInteraction::InvalidInteractionError: Cow is not a valid object
ModelInteraction.run!(cow: Cow.new)
# => "Moo!"
```
Expand All @@ -421,11 +421,11 @@ name is different than your class name, use the `class` option. It can be
either the class, a string, or a symbol.

``` rb
model :dolly1,
object :dolly1,
class: Sheep
model :dolly2,
object :dolly2,
class: 'Sheep'
model :dolly3,
object :dolly3,
class: :Sheep
```

Expand Down Expand Up @@ -798,7 +798,7 @@ spot.

``` rb
class DestroyAccount < ActiveInteraction::Base
model :account
object :account

def execute
account.destroy
Expand Down Expand Up @@ -832,7 +832,7 @@ Skip to [the predicates section](#predicates) for more information about them.

``` rb
class UpdateAccount < ActiveInteraction::Base
model :account
object :account

string :first_name, :last_name,
default: nil
Expand Down Expand Up @@ -1021,7 +1021,7 @@ an item is purchased using a credit card.

``` rb
class BuyItem < ActiveInteraction::Base
model :credit_card, :item
object :credit_card, :item
hash :options do
boolean :gift_wrapped
end
Expand All @@ -1044,7 +1044,7 @@ errors.
``` rb
outcome = BuyItem.run(item: 'Thing', options: { gift_wrapped: 'yes' })
outcome.errors.messages
# => {:credit_card=>["is required"], :item=>["is not a valid model"], :options=>["has an invalid nested value (\"gift_wrapped\" => \"yes\")"]}
# => {:credit_card=>["is required"], :item=>["is not a valid object"], :options=>["has an invalid nested value (\"gift_wrapped\" => \"yes\")"]}
```

Determining the type of error based on the string is difficult if not
Expand Down Expand Up @@ -1073,7 +1073,7 @@ itself. By using the `#merge!` helper on `errors`, you can do exactly that.

``` rb
class UpdateThing < ActiveInteraction::Base
model :thing
object :thing

def execute
if thing.save
Expand Down Expand Up @@ -1190,7 +1190,7 @@ hsilgne:
hash: hsah
integer: regetni
interface: ecafretni
model: ledom
object: tcejbo
string: gnirts
symbol: lobmys
time: emit
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
hash: [Hash.new], # TODO
integer: [0, '0', 0.0],
interface: [Object.new],
model: [Object.new], # TODO: Reconstantizing.
object: [Object.new], # TODO: Reconstantizing.
string: [''], # TODO: Without strip.
symbol: [:'', ''],
time: [Time.at(0), Time.at(0).to_s, 0] # TODO: TimeWithZone
Expand Down
2 changes: 1 addition & 1 deletion lib/active_interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
require 'active_interaction/filters/hash_filter'
require 'active_interaction/filters/integer_filter'
require 'active_interaction/filters/interface_filter'
require 'active_interaction/filters/model_filter'
require 'active_interaction/filters/object_filter'
require 'active_interaction/filters/string_filter'
require 'active_interaction/filters/symbol_filter'
require 'active_interaction/filters/time_filter'
Expand Down
2 changes: 1 addition & 1 deletion lib/active_interaction/filters/hash_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Base
# hash :order
# @example
# hash :order do
# model :item
# object :item
# integer :quantity, default: 1
# end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module ActiveInteraction
class Base
# @!method self.model(*attributes, options = {})
# @!method self.object(*attributes, options = {})
# Creates accessors for the attributes and ensures that values passed to
# the attributes are the correct class.
#
Expand All @@ -11,14 +11,14 @@ class Base
# Class name used to ensure the value.
#
# @example
# model :account
# object :account
# @example
# model :account, class: User
# object :account, class: User
end

# @private
class ModelFilter < Filter
register :model
class ObjectFilter < Filter
register :object

def cast(value, reconstantize = true)
@klass ||= klass
Expand Down
2 changes: 1 addition & 1 deletion lib/active_interaction/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ en:
hash: hash
integer: integer
interface: interface
model: model
object: object
string: string
symbol: symbol
time: time
2 changes: 1 addition & 1 deletion spec/active_interaction/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def execute
end

InterruptInteraction = Class.new(TestInteraction) do
model :x, :y,
object :x, :y,
class: Object,
default: nil

Expand Down
4 changes: 2 additions & 2 deletions spec/active_interaction/filters/array_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
end
end

context 'with a nested model filter' do
let(:block) { proc { model } }
context 'with a nested object filter' do
let(:block) { proc { object } }
let(:name) { :objects }
let(:value) { [Object.new] }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

require 'spec_helper'

class Model; end
class Thing; end
class Things; end

describe ActiveInteraction::ModelFilter, :filter do
describe ActiveInteraction::ObjectFilter, :filter do
include_context 'filters'
it_behaves_like 'a filter'

before do
options.merge!(class: Model)
options.merge!(class: Thing)
end

describe '#cast' do
let(:value) { Model.new }
let(:value) { Thing.new }
let(:result) { filter.cast(value) }

context 'with class as a Class' do
Expand All @@ -25,19 +25,19 @@ class Things; end
it 'handles reconstantizing' do
expect(result).to eql value

Object.send(:remove_const, :Model)
class Model; end
value = Model.new
Object.send(:remove_const, :Thing)
class Thing; end
value = Thing.new

expect(filter.cast(value)).to eql value
end

it 'handles reconstantizing subclasses' do
filter

Object.send(:remove_const, :Model)
class Model; end
class Submodel < Model; end
Object.send(:remove_const, :Thing)
class Thing; end
class Submodel < Thing; end
value = Submodel.new

expect(filter.cast(value)).to eql value
Expand All @@ -46,7 +46,7 @@ class Submodel < Model; end
it 'does not overflow the stack' do
klass = Class.new do
def self.name
Model.name
Thing.name
end
end

Expand All @@ -56,8 +56,8 @@ def self.name
end

context 'without the class available' do
before { Object.send(:remove_const, :Model) }
after { class Model; end }
before { Object.send(:remove_const, :Thing) }
after { class Thing; end }

it 'does not raise an error on initialization' do
expect { filter }.to_not raise_error
Expand All @@ -69,7 +69,7 @@ def self.name
let(:class_equality) { false }

before do
allow(Model).to receive(:===).and_return(case_equality)
allow(Thing).to receive(:===).and_return(case_equality)
allow(value).to receive(:is_a?).and_return(class_equality)
end

Expand Down Expand Up @@ -101,7 +101,7 @@ def self.name

context 'with class as a superclass' do
before do
options.merge!(class: Model.superclass)
options.merge!(class: Thing.superclass)
end

it 'returns the instance' do
Expand All @@ -111,7 +111,7 @@ def self.name

context 'with class as a String' do
before do
options.merge!(class: Model.name)
options.merge!(class: Thing.name)
end

it 'returns the instance' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
require 'spec_helper'

describe 'ModelInteraction' do
it_behaves_like 'an interaction', :model, -> { // }, class: Regexp
it_behaves_like 'an interaction', :object, -> { // }, class: Regexp
end

0 comments on commit 26c6b91

Please sign in to comment.