Skip to content

Commit

Permalink
Support for removing fields after they've been defined
Browse files Browse the repository at this point in the history
  • Loading branch information
loganb committed Jun 19, 2013
1 parent 6040a52 commit ee6a1d5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/dynamoid/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module ClassMethods
# @since 0.4.0
def table(options = {})
self.options = options
super if defined? super
end

def attr_readonly(*read_only_attributes)
Expand Down
8 changes: 8 additions & 0 deletions lib/dynamoid/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def range(name, type = :string)
field(name, type)
self.range_key = name
end

def remove_field(field)
field = field.to_sym
attributes.delete(field) or raise "No such field"
remove_method field
remove_method :"#{field}="
remove_method :"#{field}?"
end
end

# You can access the attributes of an object directly on its attributes method, which is by default an empty hash.
Expand Down
12 changes: 12 additions & 0 deletions spec/dynamoid/fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@
Address.new city: ("Ten chars " * 6_600)
end

context '.remove_attribute' do
subject { @address }
before(:each) do
Address.field :foobar
Address.remove_field :foobar
end

it('should not be in the attributes hash') { Address.attributes.should_not have_key(:foobar) }
it('removes the accessor') { should_not respond_to(:foobar) }
it('removes the writer') { should_not respond_to(:foobar=) }
it('removes the interrogative') { should_not respond_to(:foobar?) }
end

context 'default values for fields' do
before do
Expand Down

1 comment on commit ee6a1d5

@hlms
Copy link

@hlms hlms commented on ee6a1d5 Jan 22, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to confirm if this commit solves the issue: #185 ?

Please sign in to comment.