Sha256: 2f8024ab65ffd370deb5ba01b0017d23ba383d5a9332f8b7c8043ebb5c0b6bec

Contents?: true

Size: 1.99 KB

Versions: 30

Compression:

Stored size: 1.99 KB

Contents

module Unpoly
  module Rails
    class Change
      module FieldDefinition

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

        # Rails 3.2 delegate generated invalid Ruby with `to: :class`.
        delegate :fields, to: :get_class

        private

        def get_class
          self.class
        end

        module ClassMethods

          def field(name, type, method: name, response_header_name: nil)
            field = type.new(name)

            define_method "#{method}_field" do
              field
            end

            define_method "#{method}_from_request_headers" do
              raw_value = request.headers[field.header_name]
              field.parse(raw_value)
            end

            define_method "#{method}_param_name" do
              field.param_name
            end

            define_method "serialized_#{method}" do
              value = send(method)
              field.stringify(value)
            end

            # define_method "#{name}_from_response_headers" do
            #   raw_value = response.headers[field.header_name]
            #   field.parse(raw_value)
            # end

            define_method "#{method}_from_params" do
              raw_value = params[field.param_name]
              field.parse(raw_value)
            end

            define_method "#{method}_from_request" do
              value = send("#{method}_from_request_headers")
              if value.nil?
                value = send("#{method}_from_params")
              end
              value
            end

            define_method "write_#{method}_to_response_headers" do
              value = send(method)
              stringified = field.stringify(value)
              if stringified.present? # app servers don't like blank header values
                header_name = response_header_name || field.header_name
                response.headers[header_name] = stringified
              end
            end
          end

        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
unpoly-rails-2.7.2.2 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.7.2.1 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.7.2 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.7.1.1 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.7.1 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.6.1 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.6.0 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.5.3 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.5.2 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.5.1 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.5.0 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.4.1 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.4.0 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.3.0 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.2.1 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.2.0 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.2.0.pre.rc1 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.1.0 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.0.1 lib/unpoly/rails/change/field_definition.rb
unpoly-rails-2.0.0 lib/unpoly/rails/change/field_definition.rb