Class: Aggrobot::Aggrobot
- Inherits:
-
Object
- Object
- Aggrobot::Aggrobot
- Includes:
- Helper
- Defined in:
- lib/aggrobot/aggrobot.rb
Instance Method Summary collapse
-
#attr(attribute, value = nil, &block) ⇒ Object
starts aggrobot on collection and block, when block is given and adds value pair to the top level object.
-
#current_value ⇒ Object
returns top level object hash/list.
-
#default(default_val = nil, &block) ⇒ Object
(also: #set_current_value)
sets default/current values to top_level_object hash/list.
-
#default_group_attrs(opts = nil) ⇒ Object
(also: #default_values)
sets default group attrs as a hash, if opts is passed as param.
- #each_group(block_arg = nil, &block) ⇒ Object (also: #iterate, #recurse)
- #evaluate(block_arg = nil, &block) ⇒ Object
-
#get_attr(attribute) ⇒ Object
gets attribute’s value from top level object, only works when top level is hash.
-
#hash(collection = nil, opts = {}, &block) ⇒ Object
creates top level data structure as hash and call block to process further.
-
#initialize(caller_context, collection = nil) ⇒ Aggrobot
constructor
A new instance of Aggrobot.
-
#list(collection = nil, opts = {}, &block) ⇒ Object
creates top level data structure as array and call block to process further.
- #method_missing(method, *args, &block) ⇒ Object
- #run(block, args = {}) ⇒ Object
Methods included from Helper
#block_from_args, #raise_error
Constructor Details
#initialize(caller_context, collection = nil) ⇒ Aggrobot
Returns a new instance of Aggrobot.
13 14 15 16 |
# File 'lib/aggrobot/aggrobot.rb', line 13 def initialize(caller_context, collection = nil) @caller_context = caller_context @aggregator = Aggregator.new(collection) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
18 19 20 |
# File 'lib/aggrobot/aggrobot.rb', line 18 def method_missing(method, *args, &block) @caller_context.send method, *args, &block end |
Instance Method Details
#attr(attribute, value = nil, &block) ⇒ Object
starts aggrobot on collection and block, when block is given and adds value pair to the top level object
64 65 66 67 68 69 70 71 |
# 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 ⇒ Object
returns top level object hash/list
58 59 60 |
# File 'lib/aggrobot/aggrobot.rb', line 58 def current_value @top_level_object end |
#default(default_val = nil, &block) ⇒ Object Also known as: set_current_value
sets default/current values to top_level_object hash/list
37 38 39 40 41 |
# 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 |
#default_group_attrs(opts = nil) ⇒ Object Also known as: default_values
sets default group attrs as a hash, if opts is passed as param
46 47 48 49 50 51 52 53 |
# 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 |
#each_group(block_arg = nil, &block) ⇒ Object Also known as: iterate, recurse
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# 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) ⇒ Object
96 97 98 99 |
# 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) ⇒ Object
gets attribute’s value from top level object, only works when top level is hash
74 75 76 |
# File 'lib/aggrobot/aggrobot.rb', line 74 def get_attr(attribute) @top_level_object[attribute] end |
#hash(collection = nil, opts = {}, &block) ⇒ Object
creates top level data structure as hash and call block to process further
23 24 25 26 27 |
# 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) ⇒ Object
creates top level data structure as array and call block to process further
30 31 32 33 34 |
# 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 |
#run(block, args = {}) ⇒ Object
9 10 11 |
# File 'lib/aggrobot/aggrobot.rb', line 9 def run(block, args = {}) instance_exec(args, &block) end |