Sha256: 74c9d7207fdb60ec2e00bc662d8d5c7dedc09128eed65c97c9875555ef24deb1

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module Husky

  class Entity

    class << self

      def wrap(items)
        items.map { |item| new(item) }
      end

      def new(data = nil)
        instance = allocate
        instance.initialize(data)
        instance
      end

    end

    attr_reader :data

    def initialize(data)
      if data.nil?
        # do nothing
      elsif data.is_a? Hash
        post_data.each_pair do |key, value|
          instance_variable_set("@#{key}", value)
        end

        post_data.each_pair do |key, value|
          self.class.send(:define_method, key) do
            instance_variable_get("@#{key}")
          end
        end
      else
        @data = data
      end
    end

    def set_basic_attributes_from_hash(hash)
      hash.each_pair do |key, value|
        unless value.respond_to? :each
          instance_variable_set("@#{key}", value)
        end
      end

      hash.each_pair do |key, value|
        unless value.respond_to? :each
          self.class.send(:define_method, key) do
            instance_variable_get("@#{key}")
          end
        end
      end
    end

    def method_missing(method, *args, &block)
      data.send(method, *args, &block)
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
husky-0.4.9 lib/husky/entity.rb