Sha256: 62f02b592defa9927d5f90572561a6e3e21d5ce3b04308b36d0646c138f678e0

Contents?: true

Size: 1.94 KB

Versions: 14

Compression:

Stored size: 1.94 KB

Contents

module Spreedly
  module Fields

    def self.included(base)
      base.extend ClassMethods
    end

    def initialize_fields(xml_doc)
      self.class.fields.each do |field|
        value = xml_doc.at_xpath(".//#{field}").inner_html.strip
        instance_variable_set("@#{field}", value)
      end
    end

    def field_hash
      self.class.fields.inject({}) do |hash, each|
        hash[each] = send(each)
        hash
      end
    end

    module ClassMethods
      def field(*fields_to_add)
        options = fields_to_add.extract_options!
        @fields ||= []
        fields_to_add.each do |f|
          @fields += [ f ]
          add_accessor_for(f, options[:type])
        end
      end

      def fields
        @fields ||= []
      end

      def inherited(subclass)
        subclass.instance_variable_set("@fields", instance_variable_get("@fields"))
      end

      def add_accessor_for(f, field_type)
        case field_type
        when :boolean
          add_boolean_accessor(f)
        when :date_time
          add_date_time_accessor(f)
        when :integer
          add_integer_accessor(f)
        when nil
          attr_reader f
        else
          raise "Unknown field type '#{options[:type]}' for field '#{f}'"
        end
      end

      def add_boolean_accessor(f)
        define_method(f) do
          return nil unless instance_variable_get("@#{f}")
          "true" == instance_variable_get("@#{f}")
        end
        alias_method "#{f}?", f
      end

      def add_date_time_accessor(f)
        define_method(f) do
          Time.parse(instance_variable_get("@#{f}")) if instance_variable_get("@#{f}")
        end
      end

      def add_integer_accessor(f)
        define_method(f) do
          return nil unless instance_variable_get("@#{f}")
          instance_variable_get("@#{f}").to_i
        end
      end
    end

  end
end

class Array
  def extract_options!
    if last.is_a?(Hash)
      pop
    else
      {}
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
spreedly-2.0.15 lib/spreedly/common/fields.rb
spreedly-2.0.14 lib/spreedly/common/fields.rb
spreedly-2.0.13 lib/spreedly/common/fields.rb
spreedly-2.0.11 lib/spreedly/common/fields.rb
spreedly-2.0.10 lib/spreedly/common/fields.rb
spreedly-2.0.9 lib/spreedly/common/fields.rb
spreedly-2.0.8 lib/spreedly/common/fields.rb
spreedly-2.0.7 lib/spreedly/common/fields.rb
spreedly-2.0.6 lib/spreedly/common/fields.rb
spreedly-2.0.5 lib/spreedly/common/fields.rb
spreedly-2.0.4 lib/spreedly/common/fields.rb
spreedly-2.0.3 lib/spreedly/common/fields.rb
spreedly-2.0.2 lib/spreedly/common/fields.rb
spreedly-2.0.1 lib/spreedly/common/fields.rb