Class: ActsPresentably::Presenters::DefaultPresenter
- Inherits:
-
Object
- Object
- ActsPresentably::Presenters::DefaultPresenter
- Defined in:
- lib/acts_presentably/presenters/default_presenter.rb
Class Method Summary collapse
- .as_json_using(object, options, presenter_key) ⇒ Object
- .associations(*associations) ⇒ Object
- .fields(*fields) ⇒ Object
- .parse_options(attributes) ⇒ Object
Instance Method Summary collapse
- #add_root(hash) ⇒ Object
- #as_json(options = {}) ⇒ Object
- #associations ⇒ Object
- #build_hash ⇒ Object
- #fields ⇒ Object
- #include_related_in(hash) ⇒ Object
- #include_root ⇒ Object
-
#initialize(object) ⇒ DefaultPresenter
constructor
A new instance of DefaultPresenter.
- #object_name ⇒ Object
Constructor Details
#initialize(object) ⇒ DefaultPresenter
31 32 33 |
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 31 def initialize object @object = object end |
Class Method Details
.as_json_using(object, options, presenter_key) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 83 def self.as_json_using object, , presenter_key if && .has_key?(presenter_key) [presenter_key].new(object).as_json() else self.new(object).as_json() end end |
.associations(*associations) ⇒ Object
12 13 14 |
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 12 def self.associations(*associations) associations.any? ? (@associations = associations) : @associations end |
.fields(*fields) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 4 def self.fields(*fields) if fields.any? @fields = fields else @fields end end |
.parse_options(attributes) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 16 def self.(attributes) attributes.collect do || case when Hash key = [:key] display_as = [:display_as] else key = display_as = end {:key => key, :display_as => display_as} end end |
Instance Method Details
#add_root(hash) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 55 def add_root(hash) if include_root custom_root = && [:root] hash = { custom_root || @object.class.model_name.element => hash } end hash end |
#as_json(options = {}) ⇒ Object
43 44 45 46 |
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 43 def as_json(={}) = add_root( build_hash) end |
#associations ⇒ Object
39 40 41 |
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 39 def associations self.class.associations || [] end |
#build_hash ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 67 def build_hash hash = {} fields.each do |field| hash[field] = @object.send field end hash end |
#fields ⇒ Object
35 36 37 |
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 35 def fields self.class.fields || [:id] end |
#include_related_in(hash) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 75 def hash hash.tap do |hash| associations.each do |assoc| hash[assoc] = @object.send(assoc).as_json() end end end |
#include_root ⇒ Object
48 49 50 51 52 53 |
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 48 def include_root unless defined?(@include_root) @include_root = @object.respond_to?(:include_root_in_json) && @object.include_root_in_json end @include_root end |
#object_name ⇒ Object
63 64 65 |
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 63 def object_name @object.class.name end |