Skip to content

Instantly share code, notes, and snippets.

@psylone
Last active August 29, 2015 14:03
Show Gist options
  • Save psylone/d98b55b1422257f68d66 to your computer and use it in GitHub Desktop.
Save psylone/d98b55b1422257f68d66 to your computer and use it in GitHub Desktop.
Gears - the way to control your logic
require 'active_support/all'
module Gears
class Base
private_class_method :new
def initialize *methods
attrs = methods.extract_options!
self.class_eval{ attr_accessor *attrs.keys }
attrs.each{ |attr, value| send "#{attr}=", value }
methods.each { |name| send name }
end
def self.perform influence, attrs
new *influence, attrs
end
def self.inherited subclass
create_storage(subclass)
end
private
def self.create_storage subclass
subclass.const_set( 'Storage', Module.new do
class << self
def set key, *values
unless respond_to? key
singleton_class.instance_eval{ attr_accessor key }
# TODO make different initial values dependent on their class.
send "#{key}=", []
end
send(key).push *values
end
def get key
send(key)
end
end
end)
end
end
end
class SocialGear < Gears::Base
attr_reader :app_parameters
def self.gather_app_parameters attrs = {}
perform([ :authenticate,
:fetch_account,
:fill_facts,
:fetch_application,
:prepare_parameters ], attrs).app_parameters
end
private
def authenticate
puts "[Authenticate user form social network with params #{params}]"
end
def fetch_account
puts "[Get user account]"
end
def fill_facts
puts "[Store social facts]"
end
def fetch_application
puts "[Get loading application]"
end
# Prepare the result of chain action
def prepare_parameters
@app_parameters = {}
end
end
network_params = { app_id: 1234, user_id: 'id13434343' }
SocialGear.gather_app_parameters params: network_params
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment