Sha256: de0648f93eecdb3a26d2f56a7aca10d0a947e8804eeb12ebf2b3fbc855325d22

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

module TMDBParty
  module Attributes
    # based on http://github.com/nullstyle/ruby-satisfaction
    
    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.replace({:type => 'nil', :lazy=>false}.merge(options))
        raise "Name can't be empty" if name.blank?

        lazy_load = "self.#{options[:lazy]} unless self.loaded?" if options[:lazy]
        class_eval <<-EOS
  def #{name}
    #{lazy_load}
    @#{name} ||= decode_raw_attribute(@attributes['#{name}'], #{options[:type]}) if @attributes
  end
  EOS
      end

    end

    module InstanceMethods
      def attributes=(value)
        @attributes = value
      end
      
      def loaded?
        @loaded
      end

      private
      def decode_raw_attribute(value, type)
        return nil unless value
        type.respond_to?(:parse) ? type.parse(value) : value
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
maddox-tmdb_party-0.3.0 lib/tmdb_party/attributes.rb
tmdb_party-0.3.1 lib/tmdb_party/attributes.rb
tmdb_party-0.3.0 lib/tmdb_party/attributes.rb