class Accessory::Accessors::AttributeAccessor

Traverses an abstract “attribute” of an arbitrary object, represented by a named getter/setter method pair.

For example, AttributeAccessor.new(:foo) will traverse through the getter/setter pair .foo and .foo=.

The abstract “attribute” does not have to correspond to an actual attr_accessor; the AttributeAccessor will work as long as the relevant named getter/setter methods exist on the receiver.

Aliases

Default constructor used by predecessor accessor

Public Class Methods

new(attr_name, default: nil) click to toggle source

@param attr_name [Symbol] the attribute name (i.e. name of the getter method) @param default [Object] the default to use if the predecessor accessor passes nil data

Calls superclass method Accessory::Accessor::new
# File lib/accessory/accessors/attribute_accessor.rb, line 25
def initialize(attr_name, default: nil)
  super(default)
  @getter_method_name = :"#{attr_name}"
  @setter_method_name = :"#{attr_name}="
end

Public Instance Methods

ensure_valid(traversal_result) click to toggle source

@!visibility private

# File lib/accessory/accessors/attribute_accessor.rb, line 50
def ensure_valid(traversal_result)
  if traversal_result
    traversal_result
  else
    require 'ostruct'
    OpenStruct.new
  end
end
get(data) { |value| ... } click to toggle source

Finds data.send(:"#{attr_name}"), feeds it down the accessor chain, and returns the result. @param data [Object] the object to traverse @return [Object] the value derived from the rest of the accessor chain

# File lib/accessory/accessors/attribute_accessor.rb, line 68
def get(data)
  value = traverse_or_default(data)

  if block_given?
    yield(value)
  else
    value
  end
end
get_and_update(data) { |value| ... } click to toggle source

Finds data.send(:"#{attr_name}"), feeds it down the accessor chain, and uses data.send(:"#{attr_name}=") to overwrite the stored value with the returned result.

If :pop is returned from the accessor chain, the stored value will be overwritten with `nil`.

@param data [Object] the object to traverse @return [Array] a two-element array containing 1. the original value found; and 2. the result value from the accessor chain

# File lib/accessory/accessors/attribute_accessor.rb, line 87
def get_and_update(data)
  value = traverse_or_default(data)

  case yield(value)
  in [:clean, result, _]
    [:clean, result, data]
  in [:dirty, result, new_value]
    data.send(@setter_method_name, new_value)
    [:dirty, result, data]
  in :pop
    data.send(@setter_method_name, nil)
    [:dirty, value, data]
  end
end
inspect(format: :long) click to toggle source

@!visibility private

Calls superclass method Accessory::Accessor#inspect
# File lib/accessory/accessors/attribute_accessor.rb, line 40
def inspect(format: :long)
  case format
  when :long
    super()
  when :short
    ".#{@getter_method_name}"
  end
end
inspect_args() click to toggle source

@!visibility private

# File lib/accessory/accessors/attribute_accessor.rb, line 35
def inspect_args
  @getter_method_name.inspect
end
name() click to toggle source

@!visibility private

# File lib/accessory/accessors/attribute_accessor.rb, line 32
def name; "attr"; end
traverse(data) click to toggle source

@!visibility private

# File lib/accessory/accessors/attribute_accessor.rb, line 60
def traverse(data)
  data.send(@getter_method_name)
end