Sha256: ff29d3c67b23aaf2e6eb420e513c847f9d05b40959336a03b1aff3e4a97c4350
Contents?: true
Size: 1.89 KB
Versions: 2
Compression:
Stored size: 1.89 KB
Contents
require 'representable' module Roar module Representer module Base def self.included(base) base.class_eval do include Representable extend ClassMethods class << self alias_method :property, :representable_property alias_method :collection, :representable_collection end end end module ClassMethods # Creates a representer instance and fills it with +attributes+. def from_attributes(attributes) # DISCUSS: better move to #new? how do we handle the original #new then? new.tap do |representer| yield representer if block_given? attributes.each { |p,v| representer.public_send("#{p}=", v) } end end end # Convert representer's attributes to a nested attributes hash. def to_attributes {}.tap do |attributes| self.class.representable_attrs.each do |definition| value = public_send(definition.getter) if definition.typed? value = definition.apply(value) do |v| v.to_attributes # applied to each typed attribute (even in collections). end end attributes[definition.name] = value end end end private def before_serialize(*) end # Returns block used in #from_json and #from_xml to filter incoming arguments. # This method is subject to change and might be removed, soon. def deserialize_block_for_options(options) return unless props = options[:except] || options[:include] props.collect!{ |name| name.to_s } lambda do |bind| res = props.include?(bind.definition.name) options[:include] ? res : !res end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
roar-0.8.2 | lib/roar/representer/base.rb |
roar-0.8.1 | lib/roar/representer/base.rb |