Module: Modelling
- Defined in:
- lib/modelling.rb,
lib/modelling/version.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
"0.0.4"
Class Method Summary collapse
Instance Method Summary collapse
- #attribute_for_inspect(value) ⇒ Object
- #attributes ⇒ Object
- #initialize(args = {}) ⇒ Object
- #inspect ⇒ Object
- #members ⇒ Object
Class Method Details
.included(receiver) ⇒ Object
6 7 8 |
# File 'lib/modelling.rb', line 6 def self.included(receiver) receiver.extend ClassMethods end |
Instance Method Details
#attribute_for_inspect(value) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/modelling.rb', line 98 def attribute_for_inspect(value) if value.is_a?(String) && value.length > 50 "#{value[0..50]}...".inspect elsif value.is_a?(Date) || value.is_a?(Time) %("#{value.to_s(:db)}") elsif value.class.included_modules.include?(Modelling) "#<#{value.class.to_s}>" else value.inspect end end |
#attributes ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/modelling.rb', line 83 def attributes hash = {} self.class.accessors.each do |method_name| hash[method_name] = send(method_name) end hash end |
#initialize(args = {}) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/modelling.rb', line 72 def initialize(args = {}) members.each do |accessor, initializer| if initializer.arity > 0 send "#{accessor}=", initializer.call(self) else send "#{accessor}=", initializer.call end end args.each { |name, value| send "#{name}=", value } end |
#inspect ⇒ Object
91 92 93 94 95 96 |
# File 'lib/modelling.rb', line 91 def inspect attributes_as_nice_string = attributes.collect { |name, value| "#{name}: #{attribute_for_inspect(value)}" }.compact.join(", ") "#<#{self.class} #{attributes_as_nice_string}>" end |
#members ⇒ Object
68 69 70 |
# File 'lib/modelling.rb', line 68 def members self.class.members end |