Sha256: f64898b62b6e944ea9d91d3415462677b6977a0d978ef97cc236616c565a74c1

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

require 'uri'
require 'date'

module Twfy

  class DataElement

    @@conversions = {}

    class << self

      def convert(*fields,&block)
        fields.each do |field|
          @@conversions[field] = block
        end
      end

      def convert_to_date(*fields)
        fields.each do |field|
          convert field do |d|
            Date.parse(d)
          end
        end
      end

    end

    attr_reader :client
    def initialize(client, data={})
      @client = client
      update_attributes(data)
    end

    def update_attributes(data = {})
      data.each do |field,value|
        instance_variable_set("@#{field}", convert(field, value))
        unless self.respond_to?(field)
          self.class.send(:define_method, field) do
            instance_variable_get("@#{field}")
          end
        end
      end
    end

    def with(data = {})
      update_attributes(data)
      self
    end

    def convert(field, value)
      if conversion = @@conversions[field.to_sym]
        args = [value]
        args.unshift self if conversion.arity == 2
        conversion.call(*args)
      else
        value
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twfy-1.1.1 lib/twfy/data_element.rb
twfy-1.1.0 lib/twfy/data_element.rb