Class: ActsAsTable::Path

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/acts_as_table/path.rb

Overview

ActsAsTable path object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, parent = nil, **options) ⇒ ActsAsTable::Path

Returns a new ActsAsTable path object for the given class, parent (optional) and options.

Parameters:

  • klass (Class)
  • parent (ActsAsTable::Path, nil) (defaults to: nil)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :macro (#to_s)
  • :method_name (#to_s)
  • :index (Integer, nil)
  • :data (Object)


65
66
67
68
69
# File 'lib/acts_as_table/path.rb', line 65

def initialize(klass, parent = nil, **options)
  options.assert_valid_keys(:data, :index, :macro, :method_name)

  @klass, @parent, @options = klass, parent, options.dup
end

Instance Attribute Details

#klassClass (readonly)

Returns the class for this ActsAsTable path object.

Returns:

  • (Class)


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, **options)
    options.assert_valid_keys(:data, :index, :macro, :method_name)

    @klass, @parent, @options = klass, parent, options.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.options.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.options.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, **options|
      options.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, **options.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, **options|
      options.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, **options.merge({
        macro: macro,
        method_name: method_name,
        index: index,
      }))
    end
  end
end

#optionsHash<Symbol, Object> (readonly)

Returns the options for this ActsAsTable path object.

Returns:

  • (Hash<Symbol, 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, **options)
    options.assert_valid_keys(:data, :index, :macro, :method_name)

    @klass, @parent, @options = klass, parent, options.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.options.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.options.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, **options|
      options.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, **options.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, **options|
      options.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, **options.merge({
        macro: macro,
        method_name: method_name,
        index: index,
      }))
    end
  end
end

#parentActsAsTable::Path? (readonly)

Returns the parent of this ActsAsTable path object or nil, if this ActsAsTable path object is the start.

Returns:



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, **options)
    options.assert_valid_keys(:data, :index, :macro, :method_name)

    @klass, @parent, @options = klass, parent, options.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.options.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.options.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, **options|
      options.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, **options.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, **options|
      options.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, **options.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.

Parameters:

  • method_name (#to_s)

Returns:

  • (Symbol)


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.options.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.

Parameters:

  • method_name (#to_s)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :data (Object)

Returns:

Raises:

  • (ArgumentError)

    If the association is invalid.



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, **options)
    options.assert_valid_keys(:data, :index, :macro, :method_name)

    @klass, @parent, @options = klass, parent, options.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.options.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.options.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, **options|
      options.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, **options.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, **options|
      options.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, **options.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.

Yield Parameters:

Yield Returns:

  • (void)

Returns:



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.

Parameters:

  • method_name (#to_s)
  • index (Integer)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :data (Object)

Returns:

Raises:

  • (ArgumentError)

    If the association is invalid.



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, **options)
    options.assert_valid_keys(:data, :index, :macro, :method_name)

    @klass, @parent, @options = klass, parent, options.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.options.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.options.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, **options|
      options.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, **options.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, **options|
      options.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, **options.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.

Parameters:

  • method_name (#to_s)
  • index (Integer)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :data (Object)

Returns:

Raises:

  • (ArgumentError)

    If the association is invalid.



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, **options)
    options.assert_valid_keys(:data, :index, :macro, :method_name)

    @klass, @parent, @options = klass, parent, options.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.options.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.options.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, **options|
      options.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, **options.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, **options|
      options.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, **options.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.

Parameters:

  • method_name (#to_s)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :data (Object)

Returns:

Raises:

  • (ArgumentError)

    If the association is invalid.



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, **options)
    options.assert_valid_keys(:data, :index, :macro, :method_name)

    @klass, @parent, @options = klass, parent, options.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.options.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.options.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, **options|
      options.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, **options.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, **options|
      options.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, **options.merge({
        macro: macro,
        method_name: method_name,
        index: index,
      }))
    end
  end
end

#to_symSymbol

Returns the symbolic name for this ActsAsTable path object.

Returns:

  • (Symbol)


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.options.values_at(:method_name, :index)
      acc
    }.reverse.flatten(1).compact.collect(&:to_s).join('_').to_sym
  end
end