Class: Operations::Base::Generators::GeneratedField

Inherits:
Object
  • Object
show all
Defined in:
lib/operations/base/generators/generated_field.rb

Defined Under Namespace

Classes: InvalidOptionFieldError, InvalidTypeFieldError, MalformedFieldError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = nil, options = {}) ⇒ GeneratedField

Returns a new instance of GeneratedField.



101
102
103
104
105
# File 'lib/operations/base/generators/generated_field.rb', line 101

def initialize(name, type = nil, options = {})
  @name         = name
  @type         = type || :string
  @attr_options = options
end

Instance Attribute Details

#attr_optionsObject

Returns the value of attribute attr_options.



37
38
39
# File 'lib/operations/base/generators/generated_field.rb', line 37

def attr_options
  @attr_options
end

#nameObject

Returns the value of attribute name.



37
38
39
# File 'lib/operations/base/generators/generated_field.rb', line 37

def name
  @name
end

#typeObject

Returns the value of attribute type.



37
38
39
# File 'lib/operations/base/generators/generated_field.rb', line 37

def type
  @type
end

Class Method Details

.invalid_option?(options) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/operations/base/generators/generated_field.rb', line 67

def invalid_option?(options)
  return false if options.empty?

  !AVAILABLE_OPTIONS.intersect?(options.keys.map(&:to_s))
end

.invalid_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/operations/base/generators/generated_field.rb', line 63

def invalid_type?(type)
  (AVAILABLE_TYPES + AVAILABLE_ASSOCIATIONS).exclude?(type.to_s)
end

.parse(field) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/operations/base/generators/generated_field.rb', line 40

def parse(field)
  unless field.match?(/^[a-z_]+:[a-z_]+(\{([a-z:,\s]+)\})?$/)
    raise MalformedFieldError,
          "Could not generate field '#{field}', it should be in the format 'name:type{options}'."
  end

  name, type = field.split(":", 2)

  type, attr_options = *parse_type_and_options(type)

  if invalid_type?(type)
    raise InvalidTypeFieldError,
          "Could not generate field '#{name}' with unknown type '#{type}'."
  end

  if invalid_option?(attr_options)
    raise InvalidOptionFieldError,
          "Could not generate field '#{name}' with unknown options '#{attr_options}'."
  end

  new(name, type, attr_options)
end

Instance Method Details

#class_nameObject



122
123
124
# File 'lib/operations/base/generators/generated_field.rb', line 122

def class_name
  name.classify
end

#foreign_keyObject



126
127
128
# File 'lib/operations/base/generators/generated_field.rb', line 126

def foreign_key
  "#{name}#{Operations.configuration.foreign_key_suffix}"
end

#reference?Boolean Also known as: association?

Returns:

  • (Boolean)


113
114
115
# File 'lib/operations/base/generators/generated_field.rb', line 113

def reference?
  AVAILABLE_ASSOCIATIONS.include?(type.to_s)
end

#regular?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/operations/base/generators/generated_field.rb', line 118

def regular?
  !reference?
end

#requirementObject



107
108
109
110
111
# File 'lib/operations/base/generators/generated_field.rb', line 107

def requirement
  return "required" if attr_options[:required]

  "optional"
end