Module: ObservableAttributes

Included in:
Actor
Defined in:
lib/gamebox/lib/observable_attributes.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/gamebox/lib/observable_attributes.rb', line 80

def self.included(klass)
  klass.instance_eval do
    include Kvo
    def has_attribute(name)
      kvo_attr_accessor name
    end
  end

end

Instance Method Details

#attributesObject

Returns a new hash containing the attr keys and values



70
71
72
73
74
75
76
77
78
# File 'lib/gamebox/lib/observable_attributes.rb', line 70

def attributes
  {}.tap do |atts|
    if @evented_attributes
      @evented_attributes.each do |name|
        atts[name] = self.send name
      end
    end
  end
end

#has_attribute(name, value = nil) ⇒ Object

singular form of has_attributes takes the symbol name and an optional default value



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gamebox/lib/observable_attributes.rb', line 52

def has_attribute(name, value=nil)
  @evented_attributes ||= []
  unless has_attribute? name
    @evented_attributes << name
    self.metaclass.send :kvo_attr_accessor, name
    self.define_singleton_method "#{name}?" do
      self.send name
    end
    self.send("#{name}=", value)
  end
end

#has_attribute?(name) ⇒ Boolean

Returns true if the passed in symbol is an attr on this object

Returns:

  • (Boolean)


65
66
67
# File 'lib/gamebox/lib/observable_attributes.rb', line 65

def has_attribute?(name)
  @evented_attributes && @evented_attributes.include?(name)
end

#has_attributes(*names) ⇒ Object

Adds attributes that are not currently set. Takes a list of symbols or a hash of symbols => default values

examples:

foo.has_attributes :x, :y # adds x, x=, y, y= methods #fires events x_changed, y_changed when the values change

foo.has_attributes x: 5, y: 3 # this form provides default values if these are new attributes



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gamebox/lib/observable_attributes.rb', line 38

def has_attributes(*names)
  if names.first.is_a? Hash
    names.first.each do |name, default|
      has_attribute name, default
    end
  else
    names.each do |name|
      has_attribute name
    end
  end
end

#update_attributes(attributes) ⇒ Object

atomically update all attributes it updates all of the values; then triggers the events my_actor.update_attributes(x: 5, y: 7) will emit both x_changed and y_changed after setting both values



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gamebox/lib/observable_attributes.rb', line 7

def update_attributes(attributes)
  # TODO put this in kvo gem; there is too much internal knowlege of where
  # kvo stores stuff 
  #
  # self.kvo_set('x', new_val, silent: true)
  # self.changed('x', old_val, new_val)
  old_values = attributes.keys.inject({}) do |hash, attr_name|
    hash[attr_name] = self.send(attr_name)
    hash
  end

  attributes.each do |name, val|
    self.instance_variable_set("@kvo_#{name}", val)
  end

  attributes.each do |name, val|
    fire "#{name}_changed".to_sym, old_values[name], val
  end
end