How can I remove multiple references to the property statement in my Link class? I have done something similar with the require statement for gems with
require 'sinatra'
require 'data_mapper'
require 'shotgun'
becoming..
%w{sinatra data_mapper shotgun}.each { |lib| require lib}
%w{sinatra data_mapper shotgun}.each { |lib| require lib}
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/mini.db")
class Link
include DataMapper::Resource
property :id, Serial
property :title, Text, :required => true
property :url, String, :required => true
property :points, Integer, :default => 0
end
DataMapper.auto_upgrade!
Part of being a good developer is knowing when not to try to reduce duplication. Technically speaking, having multiple
propertystatements is a form of duplication, But it is so innocuous as to be not worth your time to remove. That’s especially true when considered in light of the alternative approach implied here, which I find confusing and difficult to read.Ultimately this is an unnecessary idiom that will make your code harder to maintain. The
propertystatements should be left alone, and I advise you against pursuing a different approach.If you absolutely must do this for some reason, you can use: