Module: Rep
- Defined in:
- lib/rep.rb,
lib/rep/version.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
"1.0.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
- #to_mash(name = :default) ⇒ 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.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rep.rb', line 37 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 } 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.
58 59 60 61 62 |
# File 'lib/rep.rb', line 58 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`).
73 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 |
# File 'lib/rep.rb', line 73 def to_hash(name = :default) fields = if name.is_a?(Hash) hash = name if hash.keys.include?(:fields) fields = hash[:fields] else raise "Argument must be either `fields: [...]` or `:symbol`" end else self.class.json_fields(name) end if fields fields.each_with_object({}) do |field, memo| field_name, method_name = field.is_a?(Hash) ? field.to_a.first : [field, field] if self.respond_to?(method_name) memo[field_name] = send(method_name) else = "There is no method named '#{method_name}' for the class '#{self.class}' for the '#{name}' list of fields" raise NoMethodError.new(, method_name) end end else raise "There are no json fields under the name: #{name}" end end |
#to_json ⇒ Object
104 105 106 |
# File 'lib/rep.rb', line 104 def to_json JSON.generate(to_hash) end |
#to_mash(name = :default) ⇒ Object
100 101 102 |
# File 'lib/rep.rb', line 100 def to_mash(name = :default) Mashed::Mash.new(to_hash(name)) end |