Class: Damsel::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/damsel/base.rb

Direct Known Subclasses

Data, Exec

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

.attrsObject

Returns the value of attribute attrs.



64
65
66
# File 'lib/damsel/base.rb', line 64

def attrs
  @attrs
end

.search_moduleObject

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, options={}, &block)
  name = name.to_sym
  options = {
      value: nil,
      type: :string,
      klass: nil
  }.merge(options)
  #puts "ATTR: #{name} - #{o.inspect}"

  name = name.to_s.singularize.to_sym if options[:type] == Array
  options[:block] = block if block_given?

  @attrs ||= Damsel::AttributeSet.new
  @attrs << Damsel::Attribute.new(name, options)

  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.message} 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, options={})
  singular = names.to_s.singularize
  o = {
      klass: find_class(singular),
      type: :child,
      many: true,
      named: false,
      value: nil
  }.merge(options)
  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, options={})
  o = {
      klass: find_class(name),
      type: :child,
      many: false,
      named: false,
      value: nil
  }.merge(options)
  attribute(name, o)
end

.references(name, options = {}) ⇒ Object



147
148
149
# File 'lib/damsel/base.rb', line 147

def references(name, options={})
  attribute(name, options)
end

Instance Method Details

#[](name) ⇒ Object



16
17
18
# File 'lib/damsel/base.rb', line 16

def [](name)
  @data[name.to_sym]
end

#attrsObject



12
13
14
# File 'lib/damsel/base.rb', line 12

def attrs
  self.class.attrs
end

#dataObject



28
29
30
# File 'lib/damsel/base.rb', line 28

def data
  raise "not used"
end

#to_hashObject



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.message} at #{e.backtrace.first}"
end