Class: Paramore::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/paramore/field.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  null: false,
  compact: false,
  default: nil,
  empty: false,
  required: true,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(given_type, null:, compact:, default:, empty:, required:) ⇒ Field

Returns a new instance of Field.



13
14
15
16
17
18
19
20
# File 'lib/paramore/field.rb', line 13

def initialize(given_type, null:, compact:, default:, empty:, required:)
  @given_type = given_type
  @nullable = null
  @compact = compact
  @allow_empty = empty
  @default = default
  @required = required
end

Class Method Details

.wildly_keyed_hash?(hash) ⇒ Boolean

Returns:



58
59
60
# File 'lib/paramore/field.rb', line 58

def self.wildly_keyed_hash?(hash)
  hash.keys.any? { |key| [Class, Module].include?(key.class) }
end

Instance Method Details

#allow_empty?Boolean

Returns:



26
27
28
# File 'lib/paramore/field.rb', line 26

def allow_empty?
  @allow_empty
end

#compact?Boolean

Returns:



38
39
40
# File 'lib/paramore/field.rb', line 38

def compact?
  @compact
end

#defaultObject



34
35
36
# File 'lib/paramore/field.rb', line 34

def default
  @default.is_a?(Proc) ? @default.call : @default
end

#default?Boolean

Returns:



30
31
32
# File 'lib/paramore/field.rb', line 30

def default?
  !@default.nil?
end

#nullable?Boolean

Returns:



42
43
44
# File 'lib/paramore/field.rb', line 42

def nullable?
  @nullable
end

#required?Boolean

Returns:



22
23
24
# File 'lib/paramore/field.rb', line 22

def required?
  @required
end

#typeObject



46
47
48
# File 'lib/paramore/field.rb', line 46

def type
  @given_type
end

#validate!Object



54
55
56
# File 'lib/paramore/field.rb', line 54

def validate!
  Paramore::Validate.run(self)
end

#wildly_keyed_hash?Boolean

Returns:



50
51
52
# File 'lib/paramore/field.rb', line 50

def wildly_keyed_hash?
  type.is_a?(Hash) && self.class.wildly_keyed_hash?(type)
end