Created
June 14, 2019 11:52
-
-
Save myun2/64b29b59979d995a333c1b9cfbf858cb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def int(name) | |
eval <<-EOS | |
public | |
def #{name}=(value) | |
raise '#{name} is required' if value == nil | |
raise 'Type is mismatch' unless value.is_a? Integer | |
@#{name} = value | |
end | |
EOS | |
end | |
def string(name) | |
eval <<-EOS | |
public | |
def #{name}=(value) | |
raise '#{name} is required' if value == nil | |
raise 'Type is mismatch' unless value.is_a? String | |
@#{name} = value | |
end | |
EOS | |
end | |
def string?(name) | |
eval <<-EOS | |
public | |
def #{name}=(value) | |
raise 'Type is mismatch' unless value == nil || value.is_a?(String) | |
@#{name} = value | |
end | |
EOS | |
end | |
class Example | |
int :id | |
string :name | |
string? :description | |
end | |
Example.new.id = 3 | |
Example.new.id = "3" | |
Example.new.id = nil | |
Example.new.description = 3 | |
Example.new.description = "3" | |
Example.new.description = nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment