Skip to content

Instantly share code, notes, and snippets.

@rsgrafx
Last active December 16, 2015 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsgrafx/5436732 to your computer and use it in GitHub Desktop.
Save rsgrafx/5436732 to your computer and use it in GitHub Desktop.
Brain dump ... Using singletons, forwardable, delegation. - Commented out code - the non ruby way. After several months of objective c development you tend to forget the elegance of Ruby. but thankfully I found my way back.
puts "#{ENV['RUBY_VERSION']}"
require 'rubygems'
require 'httparty'
require 'forwardable'
module Rulers
module Model
module HttpModel
def find(url)
"HttpModel -> #{self.class.ancestors}"
end
def self.included(base)
base.send(:include, HTTParty)
base.send(:extend, ClassMethods)
end
module ClassMethods
def find(url)
"#{self.ancestors}"
end
end
# this is the one that includes HTTParty
end
module FileModel
def find(id)
begin
puts "FileModel -> #{self.class.ancestors}"
new("db/quotes/#{id}.json")
rescue
return nil
end
end
def scream!
"yeah right!!!"
end
def self.included(base)
base.send(:include, InstanceMethods)
base.send(:extend, ClassMethods)
end
module InstanceMethods
def [](name)
@hash[name.to_s]
end
def []=(name, value)
@hash[name.to_s] = value
end
end
module ClassMethods
def find(id)
begin
puts "----"
new("db/quotes/#{id}.json")
rescue
return nil
end
end
end
end
class Base
# SOURCE = { api: HttpModel, file: FileModel }
# class << self
# extend Forwardable
# attr_accessor :functionality
# def_delegators :functionality, :find
# end
# def initialize(source)
# @source = source
# add_base_functionality
# end
# private
# def add_base_functionality
# Base.functionality = Struct.new("#{@source.capitalize}", :data)
# puts "#{Base.functionality} <<---"
# if SOURCE[@source].is_a? Array
# SOURCE[@source].each do |_mod|
# Base.functionality.send(:include, _mod)
# end
# else
# Base.functionality.send(:include, SOURCE[@source])
# end
# end
def initialize(behavior = Rulers::Model::HttpModel)
self.extend(behavior)
end
end
end
end
object1 = Rulers::Model::Base.new
object2 = Rulers::Model::Base.new(Rulers::Model::FileModel)
# begin
# puts Rulers::Model::Base.find('wow')
# rescue NoMethodError => e
# puts '-- not hit it yet--' + "#{e}"
# end
puts "--"
puts object1.find('wow')
puts object1.scream!
puts object2.find('bar')
puts object2.scream!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment