Sha256: dc1304847263e4fad1fce7882bc6b3420e5aff1fbd563c245eca32d136560c0d

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module Husky

  class Entity

    class << self

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

      def build(data = nil)
        instance = allocate
        instance.construct(data)
        instance
      end

    end

    attr_reader :data

    def construct(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

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

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