Sha256: 8ca036b119fc102d5cbc16a4992cd16ca727a8e24abc970ce70310561c0e0e8c

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require 'ostruct'
require 'delegate'
require 'active_support/concern'

class Frenetic
  module ResourceMockery
    extend Forwardable
    extend ActiveSupport::Concern

    def_delegators :@params, :to_json

    included do
      # I'm sure this violates some sort of CS principle or best practice,
      # but it solves the problem for now.
      superclass.send :instance_variable_set, '@mock_class', self
    end

    def attributes
      @params
    end

    def properties
      @params.each_with_object({}) do |(k,v), props|
        props[k] = v.class.to_s.underscore
      end
    end

    def default_attributes
      self.class.default_attributes
    end

    module ClassMethods
      def api_client
        superclass.api_client
      end

      # Provides a place for a Resources that are mocked to declare reasonable
      # default values for Mock Resources
      def default_attributes
        {}
      end
    end

  private

    def build_params( p )
      defaults = default_attributes.with_indifferent_access
      @params  = defaults.merge( (p || {}).with_indifferent_access )
    end

    def build_structure
      @structure = OpenStruct.new( @attrs )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
frenetic-1.0.0.alpha.1 lib/frenetic/resource_mockery.rb