Sha256: ea185eb2892f58fe92da8d0c77c57b9ea9fd2a0d1e2a62031a3d0e3ffc784327

Contents?: true

Size: 1.32 KB

Versions: 225

Compression:

Stored size: 1.32 KB

Contents

module Effective
  class ModelReader
    DATATYPES = [
      :binary, :boolean, :date, :datetime, :decimal, :float, :hstore, :inet, :integer,
      :string, :text, :uuid, :permitted_param
    ]

    attr_reader :attributes

    def initialize(&block)
      @attributes = {}
    end

    # For EffectiveLogging. This is a protected keyword I think.
    def message(*args)
      method_missing(:message, args)
    end

    def read(&block)
      instance_exec(&block)
    end

    def method_missing(m, *args, &block)
      if m == :timestamps
        attributes[:created_at] = [:datetime]
        attributes[:updated_at] = [:datetime]
        return
      end

      # Not really an attribute, just a permitted param.
      # something permitted: true
      if args.first.kind_of?(Hash) && args.first.key?(:permitted)
        args.unshift(:permitted_param)
      end

      # Specifying permitted param attributes
      # invitation [:name, :email], permitted: true
      if args.first.kind_of?(Array)
        options = args.find { |arg| arg.kind_of?(Hash) } || { permitted: true }
        args = [:permitted_param, options.merge(permitted_attributes: args.first)]
      end

      unless DATATYPES.include?(args.first)
        raise "expected first argument to be a datatype. Try name :string"
      end

      attributes[m] = args
    end

  end
end

Version data entries

225 entries across 225 versions & 1 rubygems

Version Path
effective_resources-2.25.7 app/models/effective/model_reader.rb
effective_resources-2.25.6 app/models/effective/model_reader.rb
effective_resources-2.25.5 app/models/effective/model_reader.rb
effective_resources-2.25.4 app/models/effective/model_reader.rb
effective_resources-2.25.3 app/models/effective/model_reader.rb
effective_resources-2.25.2 app/models/effective/model_reader.rb
effective_resources-2.25.1 app/models/effective/model_reader.rb
effective_resources-2.25.0 app/models/effective/model_reader.rb
effective_resources-2.24.0 app/models/effective/model_reader.rb
effective_resources-2.23.0 app/models/effective/model_reader.rb
effective_resources-2.22.2 app/models/effective/model_reader.rb
effective_resources-2.22.1 app/models/effective/model_reader.rb
effective_resources-2.22.0 app/models/effective/model_reader.rb
effective_resources-2.20.5 app/models/effective/model_reader.rb
effective_resources-2.20.4 app/models/effective/model_reader.rb
effective_resources-2.20.3 app/models/effective/model_reader.rb
effective_resources-2.20.2 app/models/effective/model_reader.rb
effective_resources-2.20.1 app/models/effective/model_reader.rb
effective_resources-2.20.0 app/models/effective/model_reader.rb
effective_resources-2.19.13 app/models/effective/model_reader.rb