Skip to content

Commit

Permalink
add typecasting capability to attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
geemus committed Jun 18, 2010
1 parent a8595ea commit 279ebec
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions lib/fog/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,59 @@ def attributes
@attributes ||= []
end

def attribute(name, other_names = [])
def attribute(name, options = {})
# FIXME: handle legacy where options would have been one or more aliases
if !options.is_a?(Hash)
options = {:aliases => options}
end
class_eval <<-EOS, __FILE__, __LINE__
attr_accessor :#{name}
attr_reader :#{name}
EOS
case options[:type]
when :boolean
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
@#{name} = case new_#{name}
when 'true'
true
when 'false'
false
end
end
EOS
when :float
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
@#{name} = new_#{name}.to_f
end
EOS
when :integer
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
@#{name} = new_#{name}.to_i
end
EOS
when :string
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
@#{name} = new_#{name}.to_s
end
EOS
when :time
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
@#{name} = Time.parse(new_#{name})
end
EOS
else
class_eval <<-EOS, __FILE__, __LINE__
attr_writer :#{name}
EOS
end
@attributes ||= []
@attributes |= [name]
for other_name in [*other_names]
aliases[other_name] = name
for new_alias in [*options[:aliases]]
aliases[new_alias] = name
end
end

Expand Down

0 comments on commit 279ebec

Please sign in to comment.