Sha256: f5907acc9aec54cc794dcd42c7e74aff02d7a369b135893600dbc45a513a604e

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 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

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

        validates name, opts[:validates] if opts[:validates]

        override_accessor fields[name]

        if block = field_factory(kind).initialized_blk
          block.call(self, fields[name])
        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.name}=".to_sym do |val|
          super f.to_json(val)
        end
        define_method f.name do
          f.from_json super()
        end
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
push_type_core-0.5.0.alpha.2 app/models/concerns/push_type/customizable.rb
push_type_core-0.5.0.alpha.1 app/models/concerns/push_type/customizable.rb
push_type_core-0.4.0 app/models/concerns/push_type/customizable.rb
push_type_core-0.4.0.beta.3 app/models/concerns/push_type/customizable.rb
push_type_core-0.3.3 app/models/concerns/push_type/customizable.rb
push_type_core-0.3.1 app/models/concerns/push_type/customizable.rb
push_type_core-0.2.1 app/models/concerns/push_type/customizable.rb
push_type_core-0.2.0 app/models/concerns/push_type/customizable.rb
push_type_core-0.2.0.beta2 app/models/concerns/push_type/customizable.rb