Skip to content

Commit

Permalink
Add deprecated: argument to attribute definition (closes #81)
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Jul 28, 2024
1 parent 7be4290 commit ee33345
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
10 changes: 10 additions & 0 deletions lib/hcloud/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ def ==(other)
id && id == other.id
end

def self.attribute(name, *args, deprecated: false, **kwargs)
super(name, *args, **kwargs)

define_method(name) do |**params|
warn "[DEPRECATION] Field \"#{name}\" on #{self.class.name} is deprecated." if deprecated

super(**params)
end
end

def self.resource_name
name.demodulize.underscore
end
Expand Down
10 changes: 1 addition & 9 deletions lib/hcloud/resources/pricing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,10 @@ class Pricing < Resource

attribute :server_types, :server_type_price, array: true, default: -> { [] }

attribute :traffic, :traffic_price
attribute :traffic, :traffic_price, deprecated: true

attribute :vat_rate

attribute :volume, :volume_price

def traffic
# FIXME: Attribute will return null on 2024-08-05
# FIXME: Attribute will be removed on 2024-11-05
warn "[DEPRECATION] Field \"traffic\" on pricing is deprecated."

super
end
end
end
20 changes: 5 additions & 15 deletions lib/hcloud/resources/server_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,19 @@ class ServerType < Resource
attribute :cores, :integer
attribute :disk, :integer
attribute :memory, :integer
attribute :included_traffic, :integer

# FIXME: Attribute will return null on 2024-08-05
# FIXME: Attribute will be removed on 2024-11-05
attribute :included_traffic, :integer, deprecated: true

attribute :cpu_type
attribute :storage_type

attribute :prices, :price, array: true, default: -> { [] }

attribute :deprecated, :boolean
attribute :deprecation, :deprecation

def included_traffic
# FIXME: Attribute will return null on 2024-08-05
# FIXME: Attribute will be removed on 2024-11-05
warn "[DEPRECATION] Field \"included_traffic\" on server types is deprecated."

super
end

def deprecated
warn "[DEPRECATION] Field \"deprecated\" on server types is deprecated."

super
end
attribute :deprecated, :boolean, deprecated: true

alias deprecated? deprecated
end
Expand Down
4 changes: 4 additions & 0 deletions spec/hcloud/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
expect(resource.foo).to eq "bar"
end

it "allows deprecated attributes" do
expect { resource.deprecated }.to output("[DEPRECATION] Field \"deprecated\" on ExampleResource is deprecated.\n").to_stderr
end

describe "#to_h" do
it "serializes the identifier" do
expect(resource.to_h).to eq({ id: resource.id })
Expand Down
2 changes: 2 additions & 0 deletions spec/support/helpers/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class ExampleResource < HCloud::Resource

attribute :sibling, :sibling

attribute :deprecated, deprecated: true

attribute :labels, default: -> { {} }

action :resize
Expand Down

0 comments on commit ee33345

Please sign in to comment.