Module: ChartMogul::Concerns::Entries

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/chartmogul/concerns/entries.rb', line 6

def self.included(base)
  base.extend ClassMethods

  base.instance_eval do
    @resource_root_key = :entries if @resource_root_key.nil?
    readonly_attr @resource_root_key, default: []

    include API::Actions::All

    include Enumerable
    def_delegators @resource_root_key, :each, :[], :<<, :size, :length, :empty?, :first, :last

    resource_root_key = @resource_root_key.to_s
    base.send :define_method, 'set_' + resource_root_key do |entries|
      objects = entries.map do |entity|
        self.class.get_entry_class.new_from_json(entity)
      end
      instance_variable_set "@#{resource_root_key}", objects
    end
  end
end