Skip to content

Instantly share code, notes, and snippets.

@reinh
Created January 21, 2009 20:01
Show Gist options
  • Save reinh/50153 to your computer and use it in GitHub Desktop.
Save reinh/50153 to your computer and use it in GitHub Desktop.
module Readers
# Maps key/value pairs from the data structure used to initialize a
# Endeca object. Allows attribute renaming. Use a block to perform data
# injunction on the value as it is set. Depends on an +attributes+ accessor
# for the attributes hash.
#
# ==== Examples
#
# # Specify basic attributes
# reader :title
#
# # Attribute renaming
# reader :long_desc => :description
#
# # Data injunction
# reader(:title => :upcased_title) { |title| title.upcase }
def reader(*attrs,&block)
hash = {}
hash.update(attrs.pop) if Hash === attrs.last
attrs.each do |attr|
hash[attr] = attr
end
hash.each do |variable, method|
variable = variable.to_s
if block_given?
define_method(method) do
block.call(attributes[variable])
end
else
define_method(method) { attributes[variable] }
end
end
end
# Typecasts attributes as integers.
#
# ==== Examples
# integer_reader :id, :rating
def integer_reader(*attrs)
reader(*attrs) { |value| Integer(value) }
end
# Typecasts attributes as BigDecimal
#
# ==== Examples
# decimal_reader :price
def decimal_reader(*attrs)
require 'bigdecimal' unless defined?(BigDecimal)
reader(*attrs) { |value| BigDecimal(value.to_s) }
end
# Typecasts attributes as floats
#
# ==== Examples
# float_reader :latitude, :longitude
def float_reader(*attrs)
reader(*attrs) { |value| Float(value) }
end
# Typecasts attributes as a Perly boolean ("0" == false, "1" == true")
#
# ==== Examples
# boolean_reader :price
def boolean_reader(*attrs)
reader(*attrs) { |value| value == "1" ? true : false }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment