Permalink
Browse files

Add Markable#raw_data and Markable#marks

  • Loading branch information...
1 parent 819e210 commit 0e682c08f53e4a11d6468fe903c9e20f87660e44 @blambeau committed Jul 23, 2012
Showing with 33 additions and 1 deletion.
  1. +1 −0 CHANGELOG.md
  2. +14 −1 lib/stamina-core/stamina/markable.rb
  3. +18 −0 test/unit/markable_test.rb
View
@@ -1,5 +1,6 @@
# 0.6.0 / FIX ME
+* Added Markable#marks and Markable#raw_data
* Generalized the decoration algorithm to work backwards as well as forwards as well as
accepting an output Hash-like object, defaulting to true state decorations.
* Generalized the DFA equivalence algorithm under TransitionSystem::Equivalence.
@@ -14,7 +14,9 @@ module Markable
#
# Returns user-value associated to _key_, nil if no such key in user-data.
#
- def [](key) @data[key] end
+ def [](key)
+ @data[key]
+ end
#
# Associates _value_ to _key_ in user-data. Overrides previous value if
@@ -33,6 +35,17 @@ def remove_mark(key)
state_changed(:loaded_pair, [key,oldvalue,nil]) if self.respond_to? :state_changed
end
+ # Returns the values mapped to `keys` as an array
+ def marks(*keys)
+ raw_data.values_at(*keys)
+ end
+
+ # Returns RAW data, that is without duplicating it. Returns result should not be
+ # changed.
+ def raw_data
+ @data ||= {}
+ end
+
# Extracts the copy of attributes which can subsequently be modified.
def data
@data.nil? ? {} : @data.dup
View
@@ -65,5 +65,23 @@ def test_value_assignments_statechange
assert_nil(@loaded["rr"])
assert_equal([:loaded_pair,[:myownkey,nil,"myownkey"],:loaded_pair,[12,nil,:twelve],:loaded_pair,[:myownkey,"myownkey",36]],@loaded.changes)
end
+
+ def test_marks
+ markable = MyMarkableObject.new
+ markable[:hello] = "world"
+ markable[:foo] = "bar"
+ assert_equal [], markable.marks
+ assert_equal ["world"], markable.marks(:hello)
+ assert_equal ["bar", "world"], markable.marks(:foo, :hello)
+ assert_equal [nil, "world"], markable.marks(:notthatone, :hello)
+ end
+
+ def test_raw_data
+ markable = MyMarkableObject.new
+ assert_equal({}, markable.raw_data)
+ markable[:hello] = "world"
+ markable[:foo] = "bar"
+ assert_equal({:hello => "world", :foo => "bar"}, markable.raw_data)
+ end
end
end

0 comments on commit 0e682c0

Please sign in to comment.