Sha256: 4f89ad3ecaa097dbe672736e58c502d175620d9d22d1380c4ae679038e631029

Contents?: true

Size: 1.36 KB

Versions: 7

Compression:

Stored size: 1.36 KB

Contents

module PushType
  module Customizable
    extend ActiveSupport::Concern

    def fields
      self.class.fields
    end

    def field_params
      fields.map { |k, field| field.param }
    end

    module ClassMethods

      attr_reader :fields

      def fields
        @fields ||= ActiveSupport::OrderedHash.new
      end

      def field(name, *args)
        raise ArgumentError if args.size > 2
        kind, opts = case args.size
          when 2 then args
          when 1 then args[0].is_a?(Hash) ? args.insert(0, :string) : args.insert(-1, {})
          else        [ :string, {} ]
        end

        _field = fields[name] = field_factory(kind).new(name, opts)
        store_accessor :field_store, _field.json_key

        validates _field.json_key, opts[:validates] if opts[:validates]

        override_accessor _field

        if block = _field.class.initialized_blk
          block.call(self, _field)
        end
      end

      protected

      def field_factory(kind)
        begin
          "push_type/#{ kind }_field".camelize.constantize
        rescue NameError
          "#{ kind }_field".camelize.constantize
        end
      end

      def override_accessor(f)
        define_method "#{ f.json_key }=".to_sym do |val|
          super f.to_json(val)
        end
        define_method f.json_key do
          f.from_json super()
        end
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
push_type_core-0.5.3 app/models/concerns/push_type/customizable.rb
push_type_core-0.5.2 app/models/concerns/push_type/customizable.rb
push_type_core-0.5.1 app/models/concerns/push_type/customizable.rb
push_type_core-0.5.0 app/models/concerns/push_type/customizable.rb
push_type_core-0.5.0.alpha.5 app/models/concerns/push_type/customizable.rb
push_type_core-0.5.0.alpha.4 app/models/concerns/push_type/customizable.rb
push_type_core-0.5.0.alpha.3 app/models/concerns/push_type/customizable.rb