Sha256: 0f7a4a1227ea666b3bb4e58afd1912c72737469b78884c7d26d35ee26b6a341b

Contents?: true

Size: 1.6 KB

Versions: 11

Compression:

Stored size: 1.6 KB

Contents

module Flexirest
  module Associations
    module ClassMethods
      include ActiveSupport::Inflector

      def has_many(key, klass = nil)
        if klass.nil?
          klass = key.to_s.classify.constantize
        end

        @_associations ||= {}
        @_associations[key] = klass
        define_method(key) do
          unless _attributes[key].is_a?(Array) || _attributes[key].is_a?(Flexirest::ResultIterator)
            return _attributes[key]
          end

          if _attributes[key].size == 0
            return _attributes[key]
          end

          if _attributes[key][0].is_a?(klass)
            return _attributes[key]
          end

          _attributes[key].each_with_index do |v, k|
            _attributes[key][k] = klass.new(v)
          end

          _attributes[key]
        end
      end

      def has_one(key, klass = nil)
        if klass.nil?
          klass = key.to_s.classify.constantize
        end

        @_associations ||= {}
        @_associations[key] = klass
        define_method(key) do
          return nil if _attributes[key].nil?

          if _attributes[key].is_a?(klass)
            return _attributes[key]
          end

          _attributes[key] = klass.new(_attributes[key])

          _attributes[key]
        end
      end

      def parse_date(*keys)
        keys.each { |key| @_date_fields << key }
      end

      def _date_fields
        @_date_fields.uniq
      end

      def inherited(subclass)
        subclass.instance_variable_set(:@_date_fields, [])
        super
      end
    end

    def self.included(base)
      base.extend(ClassMethods)
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
flexirest-1.3.35 lib/flexirest/associations.rb
flexirest-1.3.34 lib/flexirest/associations.rb
flexirest-1.3.33 lib/flexirest/associations.rb
flexirest-1.3.32 lib/flexirest/associations.rb
flexirest-1.3.31 lib/flexirest/associations.rb
flexirest-1.3.30 lib/flexirest/associations.rb
flexirest-1.3.29 lib/flexirest/associations.rb
flexirest-1.3.28 lib/flexirest/associations.rb
flexirest-1.3.27 lib/flexirest/associations.rb
flexirest-1.3.26 lib/flexirest/associations.rb
flexirest-1.3.25 lib/flexirest/associations.rb