Class: ActiveRepresenter::Base
- Inherits:
-
Object
- Object
- ActiveRepresenter::Base
- Defined in:
- lib/active_representer/base.rb
Instance Attribute Summary collapse
-
#wrapped ⇒ Object
readonly
Returns the value of attribute wrapped.
Class Method Summary collapse
- .attr_collection(name, **options) ⇒ Object
- .collection_names ⇒ Object
- .guess_representrer_name(name) ⇒ Object
Instance Method Summary collapse
-
#initialize(wrapped) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(wrapped) ⇒ Base
Returns a new instance of Base.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/active_representer/base.rb', line 14 def initialize(wrapped) @wrapped = wrapped self.class.collection_names.each do |collection_name| next if wrapped[collection_name].nil? representer = self.class.collections[collection_name] collection_value = \ if representer wrapped[collection_name].map { |item| representer.new(item) } else wrapped[collection_name] end instance_variable_set("@#{collection_name}", collection_value) end end |
Instance Attribute Details
#wrapped ⇒ Object (readonly)
Returns the value of attribute wrapped.
9 10 11 |
# File 'lib/active_representer/base.rb', line 9 def wrapped @wrapped end |
Class Method Details
.attr_collection(name, **options) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/active_representer/base.rb', line 30 def attr_collection(name, **) unless name.is_a?(Symbol) || name.is_a?(String) raise ArgumentError.new("collection's name must be a Symbol or a String") end representer_name = \ [:representer_name] ? [:representer_name] : guess_representrer_name(name.to_s) raise ArgumentError.new("representer_name must be a String") unless representer_name.is_a?(String) begin representer = representer_name.constantize collections[name.to_sym] = representer rescue NameError => e collections[name.to_sym] = nil end class_eval do attr_reader name.to_sym end end |
.collection_names ⇒ Object
48 49 50 |
# File 'lib/active_representer/base.rb', line 48 def collection_names collections.keys end |
.guess_representrer_name(name) ⇒ Object
52 53 54 |
# File 'lib/active_representer/base.rb', line 52 def guess_representrer_name(name) "#{name.singularize.camelize}Representer" end |