Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # frozen_string_literal: true
- require 'byebug'
- # BEGIN
- module Model
- def self.included(base)
- base.extend(ClassMethods)
- end
- def initialize(params = {})
- @attributes = {}
- params.each do |key, value|
- key = key.to_sym
- @attributes[key] = value
- end
- end
- def attributes
- hash = {}
- @attributes.map do |key, value|
- hash[key] = value
- end
- hash
- end
- def attributes=(attributes)
- attributes.each do |key, value|
- @attributes[key.to_sym] = value
- end
- end
- module ClassMethods
- def default_values
- @default_values ||= {}
- end
- def attributes
- default_values.keys
- end
- def attribute(name, value)
- default_values[name] = value
- initialize_setter(name, value)
- initialize_getter(name)
- end
- def initialize_setter(name, _value)
- byebug if _value[:type] == :datetime
- class_eval <<-SETTING, __FILE__, __LINE__ + 1
- def #{name}=(value)
- @#{name} = value
- @attributes[:#{name}] = @#{name}
- end
- SETTING
- end
- def initialize_getter(name)
- class_eval <<-SETTING, __FILE__, __LINE__ + 1
- def #{name}
- @attributes[:#{name}]
- end
- SETTING
- end
- end
- end
- # END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement