Class: FakerMaker::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/faker_maker/attribute.rb

Overview

Attributes describe the fields of classes

Constant Summary collapse

DEFAULT_OPTIONAL_WEIGHTING =
0.5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, block = nil, options = {}) ⇒ Attribute

Returns a new instance of Attribute.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/faker_maker/attribute.rb', line 10

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

  if options[:required].to_s.downcase.eql?('true') || options[:optional].to_s.downcase.eql?('false')
    @required = true
  else
    @optional = true
    @optional_weighting = determine_optional_weighting(options[:optional])
  end
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



6
7
8
# File 'lib/faker_maker/attribute.rb', line 6

def block
  @block
end

#embedded_factoriesObject (readonly)

Returns the value of attribute embedded_factories.



6
7
8
# File 'lib/faker_maker/attribute.rb', line 6

def embedded_factories
  @embedded_factories
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/faker_maker/attribute.rb', line 6

def name
  @name
end

#optionalObject (readonly)

Returns the value of attribute optional.



6
7
8
# File 'lib/faker_maker/attribute.rb', line 6

def optional
  @optional
end

#optional_weightingObject (readonly)

Returns the value of attribute optional_weighting.



6
7
8
# File 'lib/faker_maker/attribute.rb', line 6

def optional_weighting
  @optional_weighting
end

#requiredObject (readonly)

Returns the value of attribute required.



6
7
8
# File 'lib/faker_maker/attribute.rb', line 6

def required
  @required
end

#translationObject (readonly)

Returns the value of attribute translation.



6
7
8
# File 'lib/faker_maker/attribute.rb', line 6

def translation
  @translation
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/faker_maker/attribute.rb', line 28

def array?
  forced_array? || @array
end

#cardinalityObject



32
33
34
35
36
37
38
# File 'lib/faker_maker/attribute.rb', line 32

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

#omit?(value) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
# File 'lib/faker_maker/attribute.rb', line 44

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

#translation?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/faker_maker/attribute.rb', line 40

def translation?
  !@translation.blank?
end