Guest User

Untitled

a guest
Apr 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.89 KB | None | 0 0
  1. module Gds
  2.   module Props
  3.     def self.included(base)
  4.       base.extend ClassMethods
  5.     end
  6.  
  7.     module ClassMethods
  8.       def props(*args)
  9.         @props ||= []
  10.         @props += args
  11.  
  12.         args.each do |prop|
  13.           class_eval %{
  14.             def self.#{prop}(value)
  15.               @#{prop} = value
  16.             end
  17.           }
  18.         end
  19.  
  20.         @props
  21.       end
  22.     end
  23.   end
  24.  
  25.   class Base
  26.     include Props
  27.     props :schema, :host
  28.  
  29.     def to_url
  30.       # UGLY
  31.       @schema = self.class.instance_variable_get(:@schema)
  32.       @host   = self.class.instance_variable_get(:@host)
  33.  
  34.       "#{@schema}://#{@host}"
  35.     end
  36.  
  37.   end
  38.  
  39.   class Example < Base
  40.     schema 'http'
  41.     host   'localhost'
  42.   end
  43.  
  44.   class Other < Base
  45.     schema 'https'
  46.     host   'google.com'
  47.   end
  48. end
  49.  
  50. e = Gds::Example.new
  51. puts "#{e.to_url}"
  52.  
  53. e = Gds::Other.new
  54. puts "#{e.to_url}"
Add Comment
Please, Sign In to add comment