Module: AttributeDriven::ClassMethods

Defined in:
lib/attribute_driven.rb

Instance Method Summary collapse

Instance Method Details

#attributes(*args) ⇒ Object



69
70
71
72
73
74
# File 'lib/attribute_driven.rb', line 69

def attributes(*args)
  attributes = AttributeDriven.attributes_from(args)
  attr_reader *attributes
  initializes_with *attributes
  equality_based_on *args
end

#equality_based_on(*args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/attribute_driven.rb', line 41

def equality_based_on(*args)
  attributes = AttributeDriven.attributes_from(args)
  options = AttributeDriven.options_from(args)
  raise "unknown option value of :class_check => #{options[:class_check].inspect}" unless [nil, :equal?, :kind_of?].include?(options[:class_check])
  class_eval("    def eql?(other)\n      return true if equal?(other)\n      \#{options[:class_check] ? \"return false unless \#{options[:class_check] == :equal? ? 'other.class.equal?(self.class)' : 'other.kind_of?(self.class)'}\" : \"\"}\n      \#{attributes.inspect}.inject(true) do |result, attribute|\n        result &&= (self.send(attribute) == other.send(attribute))\n      end\n    end\n    alias_method :==, :eql?\n    \n    def hash\n      \#{attributes.inspect}.inject(0) do |result, attribute|\n        result += self.send(attribute).hash if self.send(attribute)\n        result\n      end\n    end\n    \n    protected\n    \#{attributes.inspect}.each do |attribute|\n      attr_reader attribute unless instance_methods.include?(attribute)\n    end\n  EOS\nend\n", __FILE__, __LINE__ + 1)

#initializes_with(*attributes) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/attribute_driven.rb', line 31

def initializes_with(*attributes)
  class_eval("    def initialize(params = {})\n      \#{attributes.inspect}.each do |attribute|\n        instance_variable_set(\"@\\\#{attribute}\", params[attribute])\n      end\n    end\n  EOS\nend\n", __FILE__, __LINE__ + 1)