Class: Damsel::Base
- Inherits:
-
Object
- Object
- Damsel::Base
- Defined in:
- lib/damsel/base.rb
Class Attribute Summary collapse
-
.attrs ⇒ Object
Returns the value of attribute attrs.
-
.search_module ⇒ Object
Returns the value of attribute search_module.
Class Method Summary collapse
- .attribute(name, options = {}, &block) ⇒ Object
- .find_class(name) ⇒ Object
- .find_module(name) ⇒ Object
- .has_many(names, options = {}) ⇒ Object
- .has_one(name, options = {}) ⇒ Object
- .references(name, options = {}) ⇒ Object
Instance Method Summary collapse
- #[](name) ⇒ Object
- #attrs ⇒ Object
- #data ⇒ Object
-
#initialize(name) ⇒ Base
constructor
A new instance of Base.
- #to_hash ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize(name) ⇒ Base
Returns a new instance of Base.
6 7 8 9 10 |
# File 'lib/damsel/base.rb', line 6 def initialize(name) @name = name @data = {} @names = [] end |
Class Attribute Details
.attrs ⇒ Object
Returns the value of attribute attrs.
64 65 66 |
# File 'lib/damsel/base.rb', line 64 def attrs @attrs end |
.search_module ⇒ Object
Returns the value of attribute search_module.
65 66 67 |
# File 'lib/damsel/base.rb', line 65 def search_module @search_module end |
Class Method Details
.attribute(name, options = {}, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/damsel/base.rb', line 67 def attribute(name, ={}, &block) name = name.to_sym = { value: nil, type: :string, klass: nil }.merge() #puts "ATTR: #{name} - #{o.inspect}" name = name.to_s.singularize.to_sym if [:type] == Array [:block] = block if block_given? @attrs ||= Damsel::AttributeSet.new @attrs << Damsel::Attribute.new(name, ) define_method name, ->(value=nil, &b) do attr = attrs[name] return @data[name] if value.nil? && b.nil? @data[name] ||= attr.default ? attr.default.dup : nil if attr.block valid = attr.block.call(value) raise "validation for #{name} failed: #{attr.block}" unless valid end if attr.type == Array @data[name] << value elsif attr.type == :child k = attr.klass.constantize obj = k.new(name) obj.name value if value if attr.many? if attr.named? value = value.to_sym raise "setting more than one #{name}[#{value}]: previous: #{@data[name]}" if @names.include?("#{name}#{value}") @names << "#{name}#{value}" @data[name] << obj else @data[name] << obj end else raise "setting more than one #{name}: previous: #{@data[name].inspect}" if @data[name].count > 0 @data[name] = obj end if obj.is_a?(Damsel::Data) obj.instance_eval &b if b elsif obj.is_a?(Damsel::Exec) obj.save b end else @data[name] = value end end end |
.find_class(name) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/damsel/base.rb', line 159 def find_class(name) c = "#{find_module(self.name)}::#{name.capitalize}" #puts "FINDCLASS: #{name} #{c}" c rescue => e raise "could not find class: #{c}: #{e.} at #{e.backtrace.first}" end |
.find_module(name) ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/damsel/base.rb', line 151 def find_module(name) @module ||= begin list = name.split('::') list.pop list.join('::') end end |
.has_many(names, options = {}) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/damsel/base.rb', line 135 def has_many(names, ={}) singular = names.to_s.singularize o = { klass: find_class(singular), type: :child, many: true, named: false, value: nil }.merge() attribute(singular, o) end |
.has_one(name, options = {}) ⇒ Object
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/damsel/base.rb', line 124 def has_one(name, ={}) o = { klass: find_class(name), type: :child, many: false, named: false, value: nil }.merge() attribute(name, o) end |
.references(name, options = {}) ⇒ Object
147 148 149 |
# File 'lib/damsel/base.rb', line 147 def references(name, ={}) attribute(name, ) end |
Instance Method Details
#[](name) ⇒ Object
16 17 18 |
# File 'lib/damsel/base.rb', line 16 def [](name) @data[name.to_sym] end |
#attrs ⇒ Object
12 13 14 |
# File 'lib/damsel/base.rb', line 12 def attrs self.class.attrs end |
#data ⇒ Object
28 29 30 |
# File 'lib/damsel/base.rb', line 28 def data raise "not used" end |
#to_hash ⇒ Object
32 33 34 |
# File 'lib/damsel/base.rb', line 32 def to_hash recursive_hash(@data) end |
#validate! ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/damsel/base.rb', line 20 def validate! if @data[:block] #block.call(self) end rescue => e raise "validation failed: #{e.} at #{e.backtrace.first}" end |