Class: ActsAsTable::Path
- Inherits:
-
Object
- Object
- ActsAsTable::Path
- Includes:
- Enumerable
- Defined in:
- lib/acts_as_table/path.rb
Overview
ActsAsTable path object.
Instance Attribute Summary collapse
-
#klass ⇒ Class
readonly
Returns the class for this ActsAsTable path object.
-
#options ⇒ Hash<Symbol, Object>
readonly
Returns the options for this ActsAsTable path object.
-
#parent ⇒ ActsAsTable::Path?
readonly
Returns the parent of this ActsAsTable path object or
nil
, if this ActsAsTable path object is the start.
Instance Method Summary collapse
-
#attribute(method_name) ⇒ Symbol
Terminates this ActsAsTable path object and returns its symbolic name.
-
#belongs_to(method_name, **options) ⇒ ActsAsTable::Path
Reflect on a
:belongs_to
association using this ActsAsTable path object. -
#each {|path| ... } ⇒ Enumerable<ActsAsTable::Path>
Enumerate this ActsAsTable path object, its parent, its grandparent, etc.
-
#has_and_belongs_to_many(method_name, index, **options) ⇒ ActsAsTable::Path
Reflect on a
:has_and_belongs_to_many
association using this ActsAsTable path object. -
#has_many(method_name, index, **options) ⇒ ActsAsTable::Path
Reflect on a
:has_many
association using this ActsAsTable path object. -
#has_one(method_name, **options) ⇒ ActsAsTable::Path
Reflect on a
:has_one
association using this ActsAsTable path object. -
#initialize(klass, parent = nil, **options) ⇒ ActsAsTable::Path
constructor
Returns a new ActsAsTable path object for the given class, parent (optional) and options.
-
#to_sym ⇒ Symbol
Returns the symbolic name for this ActsAsTable path object.
Constructor Details
#initialize(klass, parent = nil, **options) ⇒ ActsAsTable::Path
Returns a new ActsAsTable path object for the given class, parent (optional) and options.
65 66 67 68 69 |
# File 'lib/acts_as_table/path.rb', line 65 def initialize(klass, parent = nil, **) .assert_valid_keys(:data, :index, :macro, :method_name) @klass, @parent, @options = klass, parent, .dup end |
Instance Attribute Details
#klass ⇒ Class (readonly)
Returns the class for this ActsAsTable path object.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/acts_as_table/path.rb', line 50 class Path include ::Enumerable attr_reader :klass, :parent, :options # Returns a new ActsAsTable path object for the given class, parent (optional) and options. # # @param [Class] klass # @param [ActsAsTable::Path, nil] parent # @param [Hash<Symbol, Object>] options # @option options [#to_s] :macro # @option options [#to_s] :method_name # @option options [Integer, nil] :index # @option options [Object] :data # @return [ActsAsTable::Path] def initialize(klass, parent = nil, **) .assert_valid_keys(:data, :index, :macro, :method_name) @klass, @parent, @options = klass, parent, .dup end # Enumerate this ActsAsTable path object, its parent, its grandparent, etc. # # @yieldparam [ActsAsTable::Path] path # @yieldreturn [void] # @return [Enumerable<ActsAsTable::Path>] def each(&block) ::Enumerator.new { |enumerator| # @return [ActsAsTable::Path, nil] path = self until path.nil? enumerator << path # @return [ActsAsTable::Path, nil] path = path.parent end }.each(&block) end # Returns the symbolic name for this ActsAsTable path object. # # @return [Symbol] def to_sym if self.parent.nil? nil else self.inject([]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end end # Terminates this ActsAsTable path object and returns its symbolic name. # # @param [#to_s] method_name # @return [Symbol] def attribute(method_name) unless [:"#{method_name}", :"#{method_name}="].all? { |sym| @klass.instance_methods.include?(sym) } || @klass.column_names.include?(method_name.to_s) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.inject([[method_name]]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end %i(belongs_to has_one).each do |macro| define_method(macro) do |method_name, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: nil, })) end end %i(has_many has_and_belongs_to_many).each do |macro| define_method(macro) do |method_name, index, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: index, })) end end end |
#options ⇒ Hash<Symbol, Object> (readonly)
Returns the options for this ActsAsTable path object.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/acts_as_table/path.rb', line 50 class Path include ::Enumerable attr_reader :klass, :parent, :options # Returns a new ActsAsTable path object for the given class, parent (optional) and options. # # @param [Class] klass # @param [ActsAsTable::Path, nil] parent # @param [Hash<Symbol, Object>] options # @option options [#to_s] :macro # @option options [#to_s] :method_name # @option options [Integer, nil] :index # @option options [Object] :data # @return [ActsAsTable::Path] def initialize(klass, parent = nil, **) .assert_valid_keys(:data, :index, :macro, :method_name) @klass, @parent, @options = klass, parent, .dup end # Enumerate this ActsAsTable path object, its parent, its grandparent, etc. # # @yieldparam [ActsAsTable::Path] path # @yieldreturn [void] # @return [Enumerable<ActsAsTable::Path>] def each(&block) ::Enumerator.new { |enumerator| # @return [ActsAsTable::Path, nil] path = self until path.nil? enumerator << path # @return [ActsAsTable::Path, nil] path = path.parent end }.each(&block) end # Returns the symbolic name for this ActsAsTable path object. # # @return [Symbol] def to_sym if self.parent.nil? nil else self.inject([]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end end # Terminates this ActsAsTable path object and returns its symbolic name. # # @param [#to_s] method_name # @return [Symbol] def attribute(method_name) unless [:"#{method_name}", :"#{method_name}="].all? { |sym| @klass.instance_methods.include?(sym) } || @klass.column_names.include?(method_name.to_s) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.inject([[method_name]]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end %i(belongs_to has_one).each do |macro| define_method(macro) do |method_name, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: nil, })) end end %i(has_many has_and_belongs_to_many).each do |macro| define_method(macro) do |method_name, index, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: index, })) end end end |
#parent ⇒ ActsAsTable::Path? (readonly)
Returns the parent of this ActsAsTable path object or nil
, if this ActsAsTable path object is the start.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/acts_as_table/path.rb', line 50 class Path include ::Enumerable attr_reader :klass, :parent, :options # Returns a new ActsAsTable path object for the given class, parent (optional) and options. # # @param [Class] klass # @param [ActsAsTable::Path, nil] parent # @param [Hash<Symbol, Object>] options # @option options [#to_s] :macro # @option options [#to_s] :method_name # @option options [Integer, nil] :index # @option options [Object] :data # @return [ActsAsTable::Path] def initialize(klass, parent = nil, **) .assert_valid_keys(:data, :index, :macro, :method_name) @klass, @parent, @options = klass, parent, .dup end # Enumerate this ActsAsTable path object, its parent, its grandparent, etc. # # @yieldparam [ActsAsTable::Path] path # @yieldreturn [void] # @return [Enumerable<ActsAsTable::Path>] def each(&block) ::Enumerator.new { |enumerator| # @return [ActsAsTable::Path, nil] path = self until path.nil? enumerator << path # @return [ActsAsTable::Path, nil] path = path.parent end }.each(&block) end # Returns the symbolic name for this ActsAsTable path object. # # @return [Symbol] def to_sym if self.parent.nil? nil else self.inject([]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end end # Terminates this ActsAsTable path object and returns its symbolic name. # # @param [#to_s] method_name # @return [Symbol] def attribute(method_name) unless [:"#{method_name}", :"#{method_name}="].all? { |sym| @klass.instance_methods.include?(sym) } || @klass.column_names.include?(method_name.to_s) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.inject([[method_name]]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end %i(belongs_to has_one).each do |macro| define_method(macro) do |method_name, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: nil, })) end end %i(has_many has_and_belongs_to_many).each do |macro| define_method(macro) do |method_name, index, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: index, })) end end end |
Instance Method Details
#attribute(method_name) ⇒ Symbol
Terminates this ActsAsTable path object and returns its symbolic name.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/acts_as_table/path.rb', line 108 def attribute(method_name) unless [:"#{method_name}", :"#{method_name}="].all? { |sym| @klass.instance_methods.include?(sym) } || @klass.column_names.include?(method_name.to_s) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.inject([[method_name]]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end |
#belongs_to(method_name, **options) ⇒ ActsAsTable::Path
Reflect on a :belongs_to
association using this ActsAsTable path object.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/acts_as_table/path.rb', line 50 class Path include ::Enumerable attr_reader :klass, :parent, :options # Returns a new ActsAsTable path object for the given class, parent (optional) and options. # # @param [Class] klass # @param [ActsAsTable::Path, nil] parent # @param [Hash<Symbol, Object>] options # @option options [#to_s] :macro # @option options [#to_s] :method_name # @option options [Integer, nil] :index # @option options [Object] :data # @return [ActsAsTable::Path] def initialize(klass, parent = nil, **) .assert_valid_keys(:data, :index, :macro, :method_name) @klass, @parent, @options = klass, parent, .dup end # Enumerate this ActsAsTable path object, its parent, its grandparent, etc. # # @yieldparam [ActsAsTable::Path] path # @yieldreturn [void] # @return [Enumerable<ActsAsTable::Path>] def each(&block) ::Enumerator.new { |enumerator| # @return [ActsAsTable::Path, nil] path = self until path.nil? enumerator << path # @return [ActsAsTable::Path, nil] path = path.parent end }.each(&block) end # Returns the symbolic name for this ActsAsTable path object. # # @return [Symbol] def to_sym if self.parent.nil? nil else self.inject([]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end end # Terminates this ActsAsTable path object and returns its symbolic name. # # @param [#to_s] method_name # @return [Symbol] def attribute(method_name) unless [:"#{method_name}", :"#{method_name}="].all? { |sym| @klass.instance_methods.include?(sym) } || @klass.column_names.include?(method_name.to_s) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.inject([[method_name]]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end %i(belongs_to has_one).each do |macro| define_method(macro) do |method_name, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: nil, })) end end %i(has_many has_and_belongs_to_many).each do |macro| define_method(macro) do |method_name, index, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: index, })) end end end |
#each {|path| ... } ⇒ Enumerable<ActsAsTable::Path>
Enumerate this ActsAsTable path object, its parent, its grandparent, etc.
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/acts_as_table/path.rb', line 76 def each(&block) ::Enumerator.new { |enumerator| # @return [ActsAsTable::Path, nil] path = self until path.nil? enumerator << path # @return [ActsAsTable::Path, nil] path = path.parent end }.each(&block) end |
#has_and_belongs_to_many(method_name, index, **options) ⇒ ActsAsTable::Path
Reflect on a :has_and_belongs_to_many
association using this ActsAsTable path object.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/acts_as_table/path.rb', line 50 class Path include ::Enumerable attr_reader :klass, :parent, :options # Returns a new ActsAsTable path object for the given class, parent (optional) and options. # # @param [Class] klass # @param [ActsAsTable::Path, nil] parent # @param [Hash<Symbol, Object>] options # @option options [#to_s] :macro # @option options [#to_s] :method_name # @option options [Integer, nil] :index # @option options [Object] :data # @return [ActsAsTable::Path] def initialize(klass, parent = nil, **) .assert_valid_keys(:data, :index, :macro, :method_name) @klass, @parent, @options = klass, parent, .dup end # Enumerate this ActsAsTable path object, its parent, its grandparent, etc. # # @yieldparam [ActsAsTable::Path] path # @yieldreturn [void] # @return [Enumerable<ActsAsTable::Path>] def each(&block) ::Enumerator.new { |enumerator| # @return [ActsAsTable::Path, nil] path = self until path.nil? enumerator << path # @return [ActsAsTable::Path, nil] path = path.parent end }.each(&block) end # Returns the symbolic name for this ActsAsTable path object. # # @return [Symbol] def to_sym if self.parent.nil? nil else self.inject([]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end end # Terminates this ActsAsTable path object and returns its symbolic name. # # @param [#to_s] method_name # @return [Symbol] def attribute(method_name) unless [:"#{method_name}", :"#{method_name}="].all? { |sym| @klass.instance_methods.include?(sym) } || @klass.column_names.include?(method_name.to_s) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.inject([[method_name]]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end %i(belongs_to has_one).each do |macro| define_method(macro) do |method_name, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: nil, })) end end %i(has_many has_and_belongs_to_many).each do |macro| define_method(macro) do |method_name, index, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: index, })) end end end |
#has_many(method_name, index, **options) ⇒ ActsAsTable::Path
Reflect on a :has_many
association using this ActsAsTable path object.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/acts_as_table/path.rb', line 50 class Path include ::Enumerable attr_reader :klass, :parent, :options # Returns a new ActsAsTable path object for the given class, parent (optional) and options. # # @param [Class] klass # @param [ActsAsTable::Path, nil] parent # @param [Hash<Symbol, Object>] options # @option options [#to_s] :macro # @option options [#to_s] :method_name # @option options [Integer, nil] :index # @option options [Object] :data # @return [ActsAsTable::Path] def initialize(klass, parent = nil, **) .assert_valid_keys(:data, :index, :macro, :method_name) @klass, @parent, @options = klass, parent, .dup end # Enumerate this ActsAsTable path object, its parent, its grandparent, etc. # # @yieldparam [ActsAsTable::Path] path # @yieldreturn [void] # @return [Enumerable<ActsAsTable::Path>] def each(&block) ::Enumerator.new { |enumerator| # @return [ActsAsTable::Path, nil] path = self until path.nil? enumerator << path # @return [ActsAsTable::Path, nil] path = path.parent end }.each(&block) end # Returns the symbolic name for this ActsAsTable path object. # # @return [Symbol] def to_sym if self.parent.nil? nil else self.inject([]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end end # Terminates this ActsAsTable path object and returns its symbolic name. # # @param [#to_s] method_name # @return [Symbol] def attribute(method_name) unless [:"#{method_name}", :"#{method_name}="].all? { |sym| @klass.instance_methods.include?(sym) } || @klass.column_names.include?(method_name.to_s) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.inject([[method_name]]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end %i(belongs_to has_one).each do |macro| define_method(macro) do |method_name, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: nil, })) end end %i(has_many has_and_belongs_to_many).each do |macro| define_method(macro) do |method_name, index, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: index, })) end end end |
#has_one(method_name, **options) ⇒ ActsAsTable::Path
Reflect on a :has_one
association using this ActsAsTable path object.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/acts_as_table/path.rb', line 50 class Path include ::Enumerable attr_reader :klass, :parent, :options # Returns a new ActsAsTable path object for the given class, parent (optional) and options. # # @param [Class] klass # @param [ActsAsTable::Path, nil] parent # @param [Hash<Symbol, Object>] options # @option options [#to_s] :macro # @option options [#to_s] :method_name # @option options [Integer, nil] :index # @option options [Object] :data # @return [ActsAsTable::Path] def initialize(klass, parent = nil, **) .assert_valid_keys(:data, :index, :macro, :method_name) @klass, @parent, @options = klass, parent, .dup end # Enumerate this ActsAsTable path object, its parent, its grandparent, etc. # # @yieldparam [ActsAsTable::Path] path # @yieldreturn [void] # @return [Enumerable<ActsAsTable::Path>] def each(&block) ::Enumerator.new { |enumerator| # @return [ActsAsTable::Path, nil] path = self until path.nil? enumerator << path # @return [ActsAsTable::Path, nil] path = path.parent end }.each(&block) end # Returns the symbolic name for this ActsAsTable path object. # # @return [Symbol] def to_sym if self.parent.nil? nil else self.inject([]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end end # Terminates this ActsAsTable path object and returns its symbolic name. # # @param [#to_s] method_name # @return [Symbol] def attribute(method_name) unless [:"#{method_name}", :"#{method_name}="].all? { |sym| @klass.instance_methods.include?(sym) } || @klass.column_names.include?(method_name.to_s) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.inject([[method_name]]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end %i(belongs_to has_one).each do |macro| define_method(macro) do |method_name, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: nil, })) end end %i(has_many has_and_belongs_to_many).each do |macro| define_method(macro) do |method_name, index, **| .assert_valid_keys(:data) # @return [ActiveRecord::Reflection::MacroReflection] reflection = @klass.reflect_on_association(method_name) if reflection.nil? || (reflection.macro != macro) raise ::ArgumentError.new("method_name - #{method_name.inspect} is invalid") end self.class.new(reflection.klass, self, **.merge({ macro: macro, method_name: method_name, index: index, })) end end end |
#to_sym ⇒ Symbol
Returns the symbolic name for this ActsAsTable path object.
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/acts_as_table/path.rb', line 93 def to_sym if self.parent.nil? nil else self.inject([]) { |acc, path| acc << path..values_at(:method_name, :index) acc }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym end end |