Class: Subroutine::Fields::Configuration
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Subroutine::Fields::Configuration
show all
- Defined in:
- lib/subroutine/fields/configuration.rb
Constant Summary
collapse
- PROTECTED_GROUP_IDENTIFIERS =
%i[all original default].freeze
- NO_GROUPS =
[].freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(field_name, options) ⇒ Configuration
Returns a new instance of Configuration.
21
22
23
24
25
26
|
# File 'lib/subroutine/fields/configuration.rb', line 21
def initialize(field_name, options)
@field_name = field_name.to_sym
config = sanitize_options(options)
super(config)
validate!
end
|
Instance Attribute Details
#field_name ⇒ Object
Returns the value of attribute field_name.
19
20
21
|
# File 'lib/subroutine/fields/configuration.rb', line 19
def field_name
@field_name
end
|
Class Method Details
.from(field_name, options) ⇒ Object
10
11
12
13
14
15
16
17
|
# File 'lib/subroutine/fields/configuration.rb', line 10
def self.from(field_name, options)
case options
when Subroutine::Fields::Configuration
options.class.new(field_name, options)
else
new(field_name, options)
end
end
|
Instance Method Details
#behavior ⇒ Object
42
43
44
|
# File 'lib/subroutine/fields/configuration.rb', line 42
def behavior
nil
end
|
#bypass_indifferent_assignment? ⇒ Boolean
78
79
80
|
# File 'lib/subroutine/fields/configuration.rb', line 78
def bypass_indifferent_assignment?
config[:bypass_indifferent_assignment] == true
end
|
#field_reader? ⇒ Boolean
74
75
76
|
# File 'lib/subroutine/fields/configuration.rb', line 74
def field_reader?
config[:field_reader] != false
end
|
#field_writer? ⇒ Boolean
70
71
72
|
# File 'lib/subroutine/fields/configuration.rb', line 70
def field_writer?
config[:field_writer] != false
end
|
#get_default ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/subroutine/fields/configuration.rb', line 50
def get_default
value = config[:default]
if value.respond_to?(:call)
value = value.call
elsif value.try(:duplicable?) value = value.deep_dup end
value
end
|
#groups ⇒ Object
82
83
84
|
# File 'lib/subroutine/fields/configuration.rb', line 82
def groups
config[:groups] || NO_GROUPS
end
|
#has_default? ⇒ Boolean
46
47
48
|
# File 'lib/subroutine/fields/configuration.rb', line 46
def has_default?
config.key?(:default)
end
|
#in_group?(group_name) ⇒ Boolean
86
87
88
|
# File 'lib/subroutine/fields/configuration.rb', line 86
def in_group?(group_name)
groups.include?(group_name.to_sym)
end
|
#inheritable_options ⇒ Object
#inspect ⇒ Object
109
110
111
|
# File 'lib/subroutine/fields/configuration.rb', line 109
def inspect
"#<#{self.class}:#{object_id} name=#{field_name} config=#{config.inspect}>"
end
|
#mass_assignable? ⇒ Boolean
66
67
68
|
# File 'lib/subroutine/fields/configuration.rb', line 66
def mass_assignable?
config[:mass_assignable] != false
end
|
#merge(options = {}) ⇒ Object
30
31
32
|
# File 'lib/subroutine/fields/configuration.rb', line 30
def merge(options = {})
self.class.new(field_name, config.merge(options))
end
|
38
39
40
|
# File 'lib/subroutine/fields/configuration.rb', line 38
def related_field_names
[field_name]
end
|
#required_modules ⇒ Object
34
35
36
|
# File 'lib/subroutine/fields/configuration.rb', line 34
def required_modules
[]
end
|
#sanitize_options(options) ⇒ Object
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/subroutine/fields/configuration.rb', line 98
def sanitize_options(options)
opts = (options || {}).to_h.dup
groups = opts[:group] || opts[:groups]
groups = nil if groups == false
opts[:groups] = Array(groups).map(&:to_sym).presence
opts.delete(:group)
opts[:aka] = opts[:aka].to_sym if opts[:aka]
opts[:name] = field_name
opts
end
|
#validate! ⇒ Object
90
91
92
93
94
95
96
|
# File 'lib/subroutine/fields/configuration.rb', line 90
def validate!
PROTECTED_GROUP_IDENTIFIERS.each do |group_name|
next unless in_group?(group_name)
raise ArgumentError, "Cannot assign a field to protected group `#{group}`. Protected groups are: #{PROTECTED_GROUP_IDENTIFIERS.join(", ")}"
end
end
|