Sha256: b3f09b78af5ea021c7752ba81065195d81c61c9885232a84e3b63ffa3ee6e417

Contents?: true

Size: 1.54 KB

Versions: 13

Compression:

Stored size: 1.54 KB

Contents

module Afterbanks
  class Resource
    def initialize(data)
      generate_attr_readers
      set_data(data)
    end

    def fields_information
      self.class.fields_information
    end

    private

    def generate_attr_readers
      fields_information.each do |field, _|
        define_singleton_method(field) do
          instance_variable_get("@#{field}")
        end
      end
    end

    def set_data(data)
      fields_information.each do |field, type|
        next unless data.key?(field.to_s) || data[field.to_sym]

        raw_value = data[field.to_s] || data[field.to_sym]
        value = value_for(raw_value, type)
        instance_variable_set("@#{field}", value)
      end
    end

    def value_for(raw_value, type)
      case type
      when :boolean
        [true, "1", 1].include?(raw_value)
      when :date
        if raw_value.is_a?(Date)
          raw_value
        else
          Date.parse(raw_value)
        end
      else
        raw_value
      end
    end

    def marshal_dump
      dump = {}

      fields_information.each do |field, _|
        dump[field] = send(field)
      end

      dump
    end

    def marshal_load(serialized_resource)
      keys = serialized_resource.keys

      keys.each do |key|
        serialized_resource[key.to_s] = serialized_resource.delete(key)
      end

      initialize(serialized_resource)
    end

    class << self
      def fields_information
        @fields_information
      end

      def has_fields(fields_information)
        @fields_information = fields_information
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
afterbanks-api-ruby-0.4.2 lib/afterbanks/resource.rb
afterbanks-api-ruby-0.4.1 lib/afterbanks/resource.rb
afterbanks-api-ruby-0.4.0 lib/afterbanks/resource.rb
afterbanks-api-ruby-0.3.4 lib/afterbanks/resource.rb
afterbanks-api-ruby-0.3.3 lib/afterbanks/resource.rb
afterbanks-api-ruby-0.3.2 lib/afterbanks/resource.rb
afterbanks-api-ruby-0.3.1 lib/afterbanks/resource.rb
afterbanks-api-ruby-0.3.0 lib/afterbanks/resource.rb
afterbanks-api-ruby-0.2.2 lib/afterbanks/resource.rb
afterbanks-api-ruby-0.2.1 lib/afterbanks/resource.rb
afterbanks-api-ruby-0.2.0 lib/afterbanks/resource.rb
afterbanks-api-ruby-0.1.1 lib/afterbanks/resource.rb
afterbanks-api-ruby-0.1.0 lib/afterbanks/resource.rb