Last active
August 29, 2015 13:57
-
-
Save psahni/9739500 to your computer and use it in GitHub Desktop.
Custom Accessor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Trait | |
def traits(*arr) | |
arr.each do |trait| | |
class_eval do | |
define_method trait.to_s + "=" do |val| | |
@trait||={} | |
@trait[trait] = val | |
end | |
end | |
end | |
class_eval do | |
define_method "traits" do | |
@trait | |
end | |
end | |
end | |
end | |
class TraitImplement | |
extend Trait | |
traits :message, :status | |
end | |
trait = TraitImplement.new | |
trait.message = ("Ny Being") | |
trait.status = '404' | |
# Message and Status methods are available as hash keys and values. | |
p trait.traits | |
#AasanHai |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment