Module: AttributeMixin
- Defined in:
- lib/rubyexts/attribute.rb
Overview
default value for attr
Instance Method Summary collapse
-
#attribute(name, default_value = nil) ⇒ name
Post.new.title # => “Rango rulez!”.
-
#default_hattribute_names ⇒ name
Returns given default value or if default value.
- #hattribute(name, default_value = nil) ⇒ Object
-
#private_alias(method) ⇒ Object
class Array private_alias :join def join(char) puts “New join!” __join__(char) end end.
-
#questionable(name, default_value = nil, &block) ⇒ Object
class Post questionable :updated, true end Post.new.updated? # => true questionable(:testing) { }.
Instance Method Details
#attribute(name, default_value = nil) ⇒ name
Post.new.title # => “Rango rulez!”
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rubyexts/attribute.rb', line 32 def attribute(name, default_value = nil) # define reader method define_method(name) do if instance_variable_get("@#{name}").nil? # lazy loading # TODO: why is it lazy loaded? default_value = default_value.call if default_value.is_a?(Proc) instance_variable_set("@#{name}", default_value.try_dup) # dup is terribly important, otherwise all the objects will points to one object. If it is for example array, all of the objects will push into one array. end instance_variable_get("@#{name}") end # define writer method define_method("#{name}=") do |value| instance_variable_set("@#{name}", value) # TODO: here should be rewritten the reader for cases when user want to do foo.bar = nil because now it will still returns the default value end return default_value end |
#default_hattribute_names ⇒ name
Returns given default value or if default value.
67 68 69 70 71 |
# File 'lib/rubyexts/attribute.rb', line 67 def default_hattribute_names # this can't be hash because in case the value will be lambda, we can't call it, # because lambda is evaluated in scope of instance @default_hattribute_names ||= Array.new end |
#hattribute(name, default_value = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/rubyexts/attribute.rb', line 73 def hattribute(name, default_value = nil) self.default_hattribute_names.push(name) define_method(:hattributes) do @__hattributes__ ||= Hash.new # this is importat for initialization @__hattributes__ self.class.default_hattribute_names.each do |hattribute| self.send(hattribute) end return @__hattributes__ end # define reader method if default_value.is_a?(Proc) define_method(name) do properties = instance_variable_get("@__hattributes__") || Hash.new if properties[name].nil? # instance_variable_set("@#{name}", default_value) # lazy loading properties[name] = self.instance_eval(&default_value) end # instance_variable_get("@#{name}") properties[name] end else define_method(name) do properties = instance_variable_get("@__hattributes__") || Hash.new # properties = @__hattributes__ if properties[name].nil? # instance_variable_set("@#{name}", default_value) # lazy loading properties[name] = default_value.try_dup end # instance_variable_get("@#{name}") properties[name] end end # define writer method define_method("#{name}=") do |value| instance_variable_set("@__hattributes__", Hash.new) unless instance_variable_get("@__hattributes__") instance_variable_get("@__hattributes__")[name] = value end return default_value end |
#private_alias(method) ⇒ Object
class Array
private_alias :join
def join(char)
puts "New join!"
__join__(char)
end
end
16 17 18 19 |
# File 'lib/rubyexts/attribute.rb', line 16 def private_alias(method) alias_method "__#{method}__", method private "__#{method}__" end |
#questionable(name, default_value = nil, &block) ⇒ Object
class Post
questionable :updated, true
end Post.new.updated? # => true questionable(:testing) { }
133 134 135 136 137 138 139 140 141 |
# File 'lib/rubyexts/attribute.rb', line 133 def questionable(name, default_value = nil, &block) define_method("#{name}?") do unless self.instance_variables.include?(name.to_sym) default_value = block_given? ? self.instance_eval(&block) : default_value self.instance_variable_set("@#{name}", default_value) end self.instance_variable_get("@#{name}") end end |