Module: Rep
- Defined in:
- lib/rep.rb,
lib/rep/version.rb
Defined Under Namespace
Modules: ClassMethods, MashedSupport
Constant Summary collapse
- VERSION =
"0.1.1"
Class Method Summary collapse
-
.included(klass) ⇒ Object
All classes that ‘include Rep` are extended with `Forwardable`, given some aliases, endowned with `HashieSupport` if Hashie is loaded, and given a delegate method if it doesn’t already have one.
Instance Method Summary collapse
-
#reset_for_json! ⇒ Object
Since a goal is to be able to share instances, we need an easy way to reset a shared instance back to factory defaults.
-
#to_hash(name = :default) ⇒ Object
All the work of generating a hash from an instance is packaged up in one method.
- #to_json ⇒ Object
Class Method Details
.included(klass) ⇒ Object
All classes that ‘include Rep` are extended with `Forwardable`, given some aliases, endowned with `HashieSupport` if Hashie is loaded, and given a delegate method if it doesn’t already have one.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rep.rb', line 36 def self.included(klass) klass.extend Forwardable klass.extend ClassMethods klass.instance_eval { class << self unless defined?(forward) alias forward delegate end unless defined?(fields) alias fields json_fields end end if defined?(Mashed) include MashedSupport end } end |
Instance Method Details
#reset_for_json! ⇒ Object
Since a goal is to be able to share instances, we need an easy way to reset a shared instance back to factory defaults. If you memoize any methods that are not declared as json fields, then overried this method and set any memoized variables to nil, then super.
61 62 63 64 65 |
# File 'lib/rep.rb', line 61 def reset_for_json! self.class.all_json_methods.each do |method_name| instance_variable_set(:"@#{method_name}", nil) end end |
#to_hash(name = :default) ⇒ Object
All the work of generating a hash from an instance is packaged up in one method. Since fields can be aliases in the format ‘{ :json_key_name => :method_name }`, there is some fancy logic to determine the `field_name` and `method_name` variables.
{ :one => :foo }.to_a # => [[:one, :foo]]
Right now it will raise if either a field doesn’t have a method to provide it’s value or if there are no json fields setup for the particular set (which defaults to ‘:default`).
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rep.rb', line 76 def to_hash(name = :default) if fields = self.class.json_fields(name) fields.reduce({}) do |memo, field| field_name, method_name = field.is_a?(Hash) ? field.to_a.first : [field, field] begin memo[field_name] = send(method_name) rescue NoMethodError => e = "There is no method named '#{method_name}' for the class '#{self.class}' for the '#{name}' list of fields : #{e.}" raise NoMethodError.new(, method_name, e.args) end memo end else raise "There are no json fields under the name: #{name}" end end |
#to_json ⇒ Object
93 94 95 |
# File 'lib/rep.rb', line 93 def to_json JSON.generate(to_hash) end |