Module: Shapeable
- Defined in:
- lib/shapeable.rb,
lib/shapeable/version.rb
Defined Under Namespace
Classes: InvalidShapeError
Constant Summary collapse
- VERSION =
'0.2.1'
Instance Method Summary collapse
Instance Method Details
#acts_as_shapeable(**opts) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/shapeable.rb', line 5 def acts_as_shapeable(**opts) (opts) acts_as_shapeable_opts = opts || {} define_method(:shape) do |opts| opts = acts_as_shapeable_opts.merge(opts) default_shape = opts[:default_shape] default_version = opts[:default_version] path = opts[:path] raise ArgumentError, "specify a default shape" unless default_shape raise ArgumentError, "specify a default version" unless default_version raise ArgumentError, "specify a path" unless path resource = path.name.split('::').last.constantize if request.accept version_str = request.accept[/version\s?=\s?(\d+)/, 1] version = version_str.nil? ? default_version : version_str.to_i shape = request.accept[/shape\s?=\s?(\w+)/, 1] || default_shape begin serializer = path.const_get("V#{version}").const_get("#{resource}#{shape.camelize}Serializer") rescue NameError raise InvalidShapeError, "Invalid shape. Tried to find version #{version} with shape #{shape}" end end serializer end end |