Sha256: 3cd4fd7eb846e58a2bb0b3cb582359b4ff7aaff1e007b22ac478ad2875fd13ca

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

module Resource::Attributes
  def self.included(base)
    base.class_eval do
      extend ClassMethods
      include InstanceMethods
      attr_reader :attributes
    end
  end
  
  module ClassMethods

    def attributes(*names)
      options = names.extract_options!
      
      names.each do |name|
        attribute name, options unless name.blank?
      end
    end

    def attribute(name, options)
      options.reverse_merge!(:type => 'nil')
      raise "Name can't be empty" if name.blank?
      
      class_eval <<-EOS
        def #{name}
          self.load unless self.loaded?
          @#{name} ||= decode_raw_attribute(@attributes['#{name}'], #{options[:type]}) if @attributes
        end
      EOS
    end
  
  end
  
  module InstanceMethods
    def attributes=(value) 
      @attributes = value.with_indifferent_access
    end
    
    private
    def decode_raw_attribute(value, type)
      if type.respond_to?(:decode_sfn)
        type.decode_sfn(value, self.satisfaction)
      else
        value
      end
    end
  end
end


class Time
  def self.decode_sfn(value, satisfaction)
    parse(value)
  end
end

class Resource
  def self.decode_sfn(value, satisfaction)
    case value
    when Hash
      id = value['id']
      returning(new(id, satisfaction)){|r| r.attributes = value}
    else
      new(value, satisfaction)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-satisfaction-0.1.0 lib/satisfaction/resource/attributes.rb
ruby-satisfaction-0.2.0 lib/satisfaction/resource/attributes.rb