Module: AttrExtras::ModuleMethods

Included in:
Module
Defined in:
lib/attr_extras.rb

Instance Method Summary collapse

Instance Method Details

#attr_id_query(*names) ⇒ Object



61
62
63
# File 'lib/attr_extras.rb', line 61

def attr_id_query(*names)
  AttrQuery.define_with_suffix(self, "_id", *names)
end

#attr_implement(*names) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/attr_extras.rb', line 65

def attr_implement(*names)
  names.each do |name|
    define_method(name) do |*_|
      raise "Implement a '#{name}' method"
    end
  end
end

#attr_initialize(*names) ⇒ Object



8
9
10
# File 'lib/attr_extras.rb', line 8

def attr_initialize(*names)
  AttrInitialize.new(self, names).apply
end

#attr_private(*names) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/attr_extras.rb', line 12

def attr_private(*names)
  # Need this to avoid "private attribute?" warnings when running
  # the full test suite; not sure why exactly.
  public

  attr_reader(*names)
  private(*names)
end

#attr_query(*names) ⇒ Object



57
58
59
# File 'lib/attr_extras.rb', line 57

def attr_query(*names)
  AttrQuery.define_with_suffix(self, "", *names)
end

#attr_value(*names) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/attr_extras.rb', line 21

def attr_value(*names)
  attr_reader(*names)

  define_method(:==) do |other|
    return false unless other.is_a?(self.class)

    names.all? { |attr| self.public_send(attr) == other.public_send(attr) }
  end

  # Both #eql? and #hash are required for hash identity.

  alias_method :eql?, :==

  define_method(:hash) do
    [ self.class, *names.map { |attr| public_send(attr) } ].hash
  end
end

#method_object(method_name, *names) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/attr_extras.rb', line 49

def method_object(method_name, *names)
  define_singleton_method(method_name) do |*values|
    new(*values).public_send(method_name)
  end

  pattr_initialize(*names)
end

#pattr_initialize(*names) ⇒ Object



39
40
41
42
# File 'lib/attr_extras.rb', line 39

def pattr_initialize(*names)
  attr_initialize(*names)
  attr_private(*Utils.flat_names(names))
end

#vattr_initialize(*names) ⇒ Object



44
45
46
47
# File 'lib/attr_extras.rb', line 44

def vattr_initialize(*names)
  attr_initialize(*names)
  attr_value(*Utils.flat_names(names))
end