class Aggrobot::Aggrobot
Public Class Methods
new(caller_context, collection = nil)
click to toggle source
# File lib/aggrobot/aggrobot.rb, line 13 def initialize(caller_context, collection = nil) @caller_context = caller_context @aggregator = Aggregator.new(collection) end
Public Instance Methods
attr(attribute, value = nil, &block)
click to toggle source
starts aggrobot on collection and block, when block is given and adds {attribute: value} pair to the top level object
# File lib/aggrobot/aggrobot.rb, line 64 def attr(attribute, value = nil, &block) block = block_from_args(value, block, false) raise_error 'attr can only be used with a hash type' unless @top_level_object.is_a?(Hash) raise_error 'attribute should be a symbol or a string' unless attribute.is_a?(Symbol) || attribute.is_a?(String) raise_error 'attr should receive a block or a value' if value.nil? && block.nil? value = ::Aggrobot.start(collection, &block) if block @top_level_object[attribute] = value end
current_value()
click to toggle source
returns top level object hash/list
# File lib/aggrobot/aggrobot.rb, line 58 def current_value @top_level_object end
default(default_val = nil, &block)
click to toggle source
sets default/current values to top_level_object hash/list
# File lib/aggrobot/aggrobot.rb, line 37 def default(default_val = nil, &block) block = block_from_args(default_val, block, false) default_val = ::Aggrobot.start(collection, &block) if block @top_level_object = default_val end
Also aliased as: set_current_value
default_group_attrs(opts = nil)
click to toggle source
sets default group attrs as a hash, if opts is passed as param
# File lib/aggrobot/aggrobot.rb, line 46 def default_group_attrs(opts = nil) if opts raise_error 'Arguments must be a hash' unless opts.is_a?(Hash) @default_group_attrs = ActiveSupport::HashWithIndifferentAccess.new(opts) else @default_group_attrs end end
Also aliased as: default_values
each_group(block_arg = nil, &block)
click to toggle source
# File lib/aggrobot/aggrobot.rb, line 78 def each_group(block_arg = nil, &block) block = block_from_args(block_arg, block, false) @aggregator.yield_results do |attrs, group_name, sub_collection| attrs = @default_group_attrs.merge(attrs) if @default_group_attrs block_value = ::Aggrobot.start(sub_collection) do if block instance_exec(attrs, &block) else attrs end end update_top_level_obj(group_name, block_value) end end
evaluate(block_arg = nil, &block)
click to toggle source
# File lib/aggrobot/aggrobot.rb, line 96 def evaluate(block_arg = nil, &block) block = block_from_args(block_arg, block) list(&block).first end
get_attr(attribute)
click to toggle source
gets attribute’s value from top level object, only works when top level is hash
# File lib/aggrobot/aggrobot.rb, line 74 def get_attr(attribute) @top_level_object[attribute] end
hash(collection = nil, opts = {}, &block)
click to toggle source
creates top level data structure as hash and call block to process further
# File lib/aggrobot/aggrobot.rb, line 23 def hash(collection = nil, opts = {}, &block) self.collection(collection) if collection @top_level_object = ActiveSupport::HashWithIndifferentAccess.new proceed(block, opts) end
list(collection = nil, opts = {}, &block)
click to toggle source
creates top level data structure as array and call block to process further
# File lib/aggrobot/aggrobot.rb, line 30 def list(collection = nil, opts = {}, &block) self.collection(collection) if collection @top_level_object = [] proceed(block, opts) end
method_missing(method, *args, &block)
click to toggle source
# File lib/aggrobot/aggrobot.rb, line 18 def method_missing(method, *args, &block) @caller_context.send method, *args, &block end
run(block, args = {})
click to toggle source
# File lib/aggrobot/aggrobot.rb, line 9 def run(block, args = {}) instance_exec(args, &block) end
Private Instance Methods
evaluate_opts(opts)
click to toggle source
# File lib/aggrobot/aggrobot.rb, line 103 def evaluate_opts(opts) opts.each do |method_name, arg| send(method_name, arg) end end
proceed(block, opts)
click to toggle source
# File lib/aggrobot/aggrobot.rb, line 117 def proceed(block, opts) raise_error "no block given for api" unless block evaluate_opts(opts) instance_eval(&block) @top_level_object end
update_top_level_obj(group, val)
click to toggle source
# File lib/aggrobot/aggrobot.rb, line 109 def update_top_level_obj(group, val) if @top_level_object.is_a? Hash @top_level_object[group] = val elsif @top_level_object.is_a? Array @top_level_object << val end end