module EasyAttrs::ClassMethods

Public Instance Methods

all_attributes() click to toggle source

`all_attributes` needs to be public for instances to call it in `intialize`.

# File lib/easy_attrs.rb, line 102
def all_attributes
  @_all_attributes ||= begin
    attributes = []

    # Yes, this is a nested loop.
    # The result is memoized so it only happens when the first instance of
    # the including class is initialized and the length of the ancestor
    # chain is rarely going to be very long (it will of course vary
    # depending on the class hierarchy of the applicaton using EasyAttrs).
    #
    easy_attrs_ancestors.each do |a|
      [:readers, :writers, :accessors, :instance_variables_only].each do |i_var|
        i_var_from_ancestor = a.instance_variable_get("@#{i_var}")

        if i_var_from_ancestor
          attributes.concat(i_var_from_ancestor)
        end
      end
    end

    attributes
  end
end

Private Instance Methods

accessors(*attrs) click to toggle source
# File lib/easy_attrs.rb, line 148
def accessors *attrs
  unless attrs.empty?
    @accessors = attrs

    class_eval do
      attr_accessor *attrs
    end
  end
end
easy_attrs_ancestors() click to toggle source

The ancestor chain includes `self`, which is exactly what we want in this case because we want to grab all the class instance variables of all the ancestors AND tose of the current class so we can find all attributes to use when an instance calls `new`.

# File lib/easy_attrs.rb, line 167
def easy_attrs_ancestors
  ancestors.select { |a| a.include? EasyAttrs }
end
instance_variables_only(*attrs) click to toggle source
# File lib/easy_attrs.rb, line 158
def instance_variables_only *attrs
  @instance_variables_only = attrs unless attrs.empty?
end
readers(*attrs) click to toggle source
# File lib/easy_attrs.rb, line 128
def readers *attrs
  unless attrs.empty?
    @readers = attrs

    class_eval do
      attr_reader *attrs
    end
  end
end
writers(*attrs) click to toggle source
# File lib/easy_attrs.rb, line 138
def writers *attrs
  unless attrs.empty?
    @writers = attrs

    class_eval do
      attr_writer *attrs
    end
  end
end