Module: AttributeMixin

default value for attr

Public Visibility

Public Instance Method Summary

#attribute(name, default_value = nil)

Returns: name

#default_hattribute_names

This will also define title and title= methods, but it doesn’t define @title variable, but @hattributes hash with all the attributes.

Returns: name

#hattribute(name, default_value = nil)
#private_alias(method)

class Array

  private_alias :join
  def join(char)
    puts "New join!"
    __join__(char)
  end

end.

#questionable(name, default_value)

class << self.

Public Instance Method Details

attribute

public name attribute(name, default_value = nil)

Meta Tags

Parameters:

[Symbol] name

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.

[Object, @optional] default_value (defaults to: nil)

Default value of the variable.

Returns:

[name]

Returns given default value or if default value.

Since:

0.0.1

See Also:

#hattribute
[View source]


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rango/ext/attribute.rb', line 33

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

public name default_hattribute_names

This will also define title and title= methods, but it doesn’t define @title variable, but @hattributes hash with all the attributes

Meta Tags

Parameters:

Returns:

[name]

Returns given default value or if default value.

Since:

0.0.1

See Also:

#attribute
[View source]


68
69
70
71
72
# File 'lib/rango/ext/attribute.rb', line 68

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

public hattribute(name, default_value = nil)
[View source]


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
119
# File 'lib/rango/ext/attribute.rb', line 74

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

public private_alias(method)

class Array

  private_alias :join
  def join(char)
    puts "New join!"
    __join__(char)
  end

end

[View source]


17
18
19
20
# File 'lib/rango/ext/attribute.rb', line 17

def private_alias(method)
  alias_method "__#{method}__", method
  private "__#{method}__"
end

questionable

public questionable(name, default_value)

class << self.class

  def hattributes
    raise "Y"
  end

end class Post

  questionable :updated, true

end Post.new.updated? # => true

Meta Tags

Parameters:

Since:

0.0.2

[View source]


133
134
135
136
137
138
139
140
# File 'lib/rango/ext/attribute.rb', line 133

def questionable(name, default_value)
  define_method("#{name}?") do
    unless self.instance_variables.include?(name.to_sym)
      self.instance_variable_set("@#{name}", default_value)
    end
    self.instance_variable_get("@#{name}")
  end
end