Sha256: 237b923b160819d9f734e47328b983e690e32bece7725fe3b89dfddf53a86656

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module FakerMaker
  # Attributes describe the fields of classes
  class Attribute
    attr_reader :name, :block, :translation

    def initialize( name, block = nil, options = {} )
      assert_valid_options options
      @name = name
      @block = block || proc { nil }
      @cardinality = options[:has] || 1
      @translation = options[:json]
      @omit = *options[:omit]
      @array = options[:array] == true
    end

    def array?
      forced_array? || @array
    end

    def cardinality
      if @cardinality.is_a? Range
        rand( @cardinality )
      else
        @cardinality
      end
    end

    def translation?
      !@translation.blank?
    end

    def omit?( value )
      case value
      when nil
        @omit.include? :nil
      when '', [], {}
        @omit.include? :empty
      else
        @omit.include?( :always ) || @omit.include?( value )
      end
    end

    private

    def forced_array?
      @cardinality.is_a?( Range ) || @cardinality > 1
    end

    def assert_valid_options( options )
      options.assert_valid_keys :has, :array, :json, :omit
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
faker_maker-1.2.0 lib/faker_maker/attribute.rb
faker_maker-1.1.10 lib/faker_maker/attribute.rb
faker_maker-1.1.9 lib/faker_maker/attribute.rb
faker_maker-1.1.8 lib/faker_maker/attribute.rb