Module: ActsAsTable::ValueProvider::InstanceMethods

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/acts_as_table/value_provider.rb

Overview

Note:

The design of this module is inspired by the the Haskell package, lens.

ActsAsTable value provider instance methods (concern).

Instance Method Summary collapse

Instance Method Details

#get_value(base = nil, **options) ⇒ ActsAsTable::ValueProvider::WrappedValue

Reads the value for the given record using the given options.

Parameters:

  • base (ActiveRecord::Base, nil) (defaults to: nil)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :default (Boolean)

Returns:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/concerns/acts_as_table/value_provider.rb', line 111

def get_value(base = nil, **options)
  options.assert_valid_keys(:default)

  if base.nil?
    return ActsAsTable.adapter.wrap_value_for(self, base, nil, nil)
  end

  # @return [Object, nil]
  source_value = _get_value(base)

  # @return [Object, nil]
  target_value = _modify_get_value_before_default(source_value)

  default = \
    if options[:default].try { |boolean_or_proc| _should_default?(target_value, boolean_or_proc) } && self.respond_to?(:default_value)
      target_value = self.default_value

      true
    else
      false
    end

  ActsAsTable.adapter.wrap_value_for(self, base, source_value, target_value, default: default)
end

#set_value(base = nil, new_value = nil, **options) ⇒ ActsAsTable::ValueProvider::WrappedValue

Writes the new value for the given record using the given options.

Parameters:

  • base (ActiveRecord::Base, nil) (defaults to: nil)
  • new_value (Object, nil) (defaults to: nil)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :default (Boolean)

Returns:



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/models/concerns/acts_as_table/value_provider.rb', line 143

def set_value(base = nil, new_value = nil, **options)
  options.assert_valid_keys(:default)

  if base.nil?
    return ActsAsTable.adapter.wrap_value_for(self, base, nil, nil)
  end

  # @return [Object, nil]
  target_value = _modify_set_value_before_default(new_value)

  changed = false

  default = \
    if options[:default].try { |boolean_or_proc| _should_default?(target_value, boolean_or_proc) } && self.respond_to?(:default_value)
      target_value = self.default_value

      true
    else
      false
    end

  target_value, changed = *_set_value(base, target_value)

  ActsAsTable.adapter.wrap_value_for(self, base, new_value, target_value, changed: changed, default: default)
end