lib/characterizable.rb in characterizable-0.0.10 vs lib/characterizable.rb in characterizable-0.0.11
- old
+ new
@@ -7,13 +7,15 @@
active_support/core_ext/class/attribute_accessors
active_support/core_ext/object/blank
active_support/core_ext/array/wrap
active_support/core_ext/module/aliasing
active_support/core_ext/module/delegation
+ active_support/json
}.each do |active_support_3_requirement|
require active_support_3_requirement
end if ActiveSupport::VERSION::MAJOR == 3
+require 'to_json_fix'
module Characterizable
def self.included(klass)
klass.cattr_accessor :characterizable_base
klass.extend ClassMethods
@@ -28,10 +30,16 @@
end
class BetterHash < ::Hash
# In Ruby 1.9, running select/reject/etc. gives you back a hash
if RUBY_VERSION < '1.9'
+ def to_hash
+ Hash.new.replace self
+ end
+ def to_json(*)
+ to_hash.to_json
+ end
def reject(&block)
inject(Characterizable::BetterHash.new) do |memo, ary|
unless block.call(*ary)
memo[ary[0]] = ary[1]
end
@@ -154,9 +162,12 @@
@name = name
@trumps = Array.wrap options.delete(:trumps)
@prerequisite = options.delete(:prerequisite)
@options = options
Blockenspiel.invoke block, self if block_given?
+ end
+ def to_json(*)
+ { :name => name, :trumps => trumps, :prerequisite => prerequisite, :options => options }.to_json
end
def inspect
"<Characterizable::Characteristic name=#{name.inspect} trumps=#{trumps.inspect} prerequisite=#{prerequisite.inspect} options=#{options.inspect}>"
end
def trumped_by