Module: AttrExtras::ClassMethods
- Included in:
- Class
- Defined in:
- lib/attr_extras.rb
Instance Method Summary collapse
- #attr_id_query(*names) ⇒ Object
- #attr_initialize(*names) ⇒ Object
- #attr_private(*names) ⇒ Object
- #method_object(method_name, *names) ⇒ Object
- #pattr_initialize(*names) ⇒ Object
Instance Method Details
#attr_id_query(*names) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/attr_extras.rb', line 42 def attr_id_query(*names) names.each do |name| name = name.to_s raise "#{__method__} wants `#{name}?`, not `#{name}`." unless name.end_with?("?") define_method(name) do # def foo? !!send("#{name.chop}_id") # !!send("foo_id") end # end end end |
#attr_initialize(*names) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/attr_extras.rb', line 5 def attr_initialize(*names) define_method(:initialize) do |*values| unless values.length == names.length raise ArgumentError, "wrong number of arguments (#{values.length} for #{names.length})" end names.zip(values).each do |name_or_names, value| if name_or_names.is_a?(Array) name_or_names.each do |key| instance_variable_set("@#{key}", value[key]) end else instance_variable_set("@#{name_or_names}", value) end end end end |
#attr_private(*names) ⇒ Object
23 24 25 26 |
# File 'lib/attr_extras.rb', line 23 def attr_private(*names) attr_reader *names private *names end |
#method_object(method_name, *names) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/attr_extras.rb', line 33 def method_object(method_name, *names) = (class << self; self; end) .send(:define_method, method_name) do |*values| new(*values).send(method_name) end pattr_initialize *names end |
#pattr_initialize(*names) ⇒ Object
28 29 30 31 |
# File 'lib/attr_extras.rb', line 28 def pattr_initialize(*names) attr_initialize *names attr_private *names.flatten end |