Sha256: 9e3c757fcbb08c6fa54c50bbfbe69c06ce5e54867e638079f26637630d27ca47

Contents?: true

Size: 1.19 KB

Versions: 42

Compression:

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

    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

42 entries across 42 versions & 1 rubygems

Version Path
effective_resources-1.8.21 app/models/effective/model_reader.rb
effective_resources-1.8.20 app/models/effective/model_reader.rb
effective_resources-1.8.19 app/models/effective/model_reader.rb
effective_resources-1.8.18 app/models/effective/model_reader.rb
effective_resources-1.8.17 app/models/effective/model_reader.rb
effective_resources-1.8.16 app/models/effective/model_reader.rb
effective_resources-1.8.15 app/models/effective/model_reader.rb
effective_resources-1.8.14 app/models/effective/model_reader.rb
effective_resources-1.8.13 app/models/effective/model_reader.rb
effective_resources-1.8.12 app/models/effective/model_reader.rb
effective_resources-1.8.11 app/models/effective/model_reader.rb
effective_resources-1.8.10 app/models/effective/model_reader.rb
effective_resources-1.8.9 app/models/effective/model_reader.rb
effective_resources-1.8.8 app/models/effective/model_reader.rb
effective_resources-1.8.7 app/models/effective/model_reader.rb
effective_resources-1.8.6 app/models/effective/model_reader.rb
effective_resources-1.8.5 app/models/effective/model_reader.rb
effective_resources-1.8.4 app/models/effective/model_reader.rb
effective_resources-1.8.3 app/models/effective/model_reader.rb
effective_resources-1.8.2 app/models/effective/model_reader.rb