Module: AttributeMixin

Defined in:
lib/rubyexts/attribute.rb

Overview

default value for attr

Instance Method Summary collapse

Instance Method Details

#attribute(name, default_value = nil) ⇒ name

Post.new.title # => “Rango rulez!”

Examples:

class Post
  attribute :title, "Rango rulez!"
end

Parameters:

  • name (Symbol)

    Name of object variable which will be set. If you have attribute :title, then the @title variable will be defined. It also create #title and #title= accessors.

  • default_value (Object, @optional) (defaults to: nil)

    Default value of the variable.

Returns:

  • (name)

    Returns given default value or if default value.

See Also:

Since:

  • 0.0.1



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_namesname

Returns given default value or if default value.

Examples:

class Post
  hattribute :title, "Rango rulez!"
end
Post.new.title
# => "Rango rulez!"

Parameters:

  • name (Symbol)

    Name of attribute his accessor methods will be defined. It’s similar as #attribute, but it doesn’t define @name variable, but it will be key in @__hattributes__ hash. It’s useful when you don’t like to mess the object namespace with many variables or if you like to separate the attributes from the instance variables.

  • default_value (Object, @optional)

    Default value of the variable.

Returns:

  • (name)

    Returns given default value or if default value.

See Also:

Since:

  • 0.0.1



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) { }

Since:

  • 0.0.2



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