Class: Operations::Base::Generators::GeneratedField
- Inherits:
-
Object
- Object
- Operations::Base::Generators::GeneratedField
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_options ⇒ Object
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
|
#name ⇒ Object
Returns the value of attribute name.
37
38
39
|
# File 'lib/operations/base/generators/generated_field.rb', line 37
def name
@name
end
|
#type ⇒ Object
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
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
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_name ⇒ Object
122
123
124
|
# File 'lib/operations/base/generators/generated_field.rb', line 122
def class_name
name.classify
end
|
#foreign_key ⇒ Object
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?
113
114
115
|
# File 'lib/operations/base/generators/generated_field.rb', line 113
def reference?
AVAILABLE_ASSOCIATIONS.include?(type.to_s)
end
|
#regular? ⇒ Boolean
118
119
120
|
# File 'lib/operations/base/generators/generated_field.rb', line 118
def regular?
!reference?
end
|
#requirement ⇒ Object
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
|