Module: Miasma::Utils::Lazy::ClassMethods

Defined in:
lib/miasma/utils/lazy.rb

Overview

Class methods for laziness

Instance Method Summary collapse

Instance Method Details

#attribute(name, type, options = {}) ⇒ nil

Add new attributes to class

Parameters:

  • name (String)
  • type (Class, Array<Class>)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :required (TrueClass, FalseClass)

    must be provided on initialization

  • :default (Object, Proc)

    default value

  • :coerce (Proc)

Returns:

  • (nil)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/miasma/utils/lazy.rb', line 110

def attribute(name, type, options={})
  name = name.to_sym
  options = options.to_smash
  attributes[name] = Smash.new(:type => type).merge(options)
  coerce = attributes[name][:coerce]
  valid_types = [attributes[name][:type], NilClass].flatten.compact
  allowed_values = attributes[name][:allowed]
  multiple_values = attributes[name][:multiple]
  depends_on = attributes[name][:depends]
  define_method(name) do
    send(depends_on) if depends_on
    self.class.on_missing(self) unless data.has_key?(name) || dirty.has_key?(name)
    dirty[name] || data[name]
  end
  define_method("#{name}=") do |val|
    values = multiple_values && val.is_a?(Array) ? val : [val]
    values.map! do |item|
      valid_type = valid_types.detect do |klass|
        item.is_a?(klass)
      end
      if(coerce && !valid_type)
        item = coerce.arity == 2 ? coerce.call(item, self) : coerce.call(item)
      end
      valid_type = valid_types.detect do |klass|
        item.is_a?(klass)
      end
      unless(valid_type)
        raise TypeError.new("Invalid type for `#{name}` (#{item} <#{item.class}>). Valid - #{valid_types.map(&:to_s).join(',')}")
      end
      if(allowed_values)
        unless(allowed_values.include?(item))
          raise ArgumentError.new("Invalid value provided for `#{name}` (#{item.inspect}). Allowed - #{allowed_values.map(&:inspect).join(', ')}")
        end
      end
      item
    end
    if(!multiple_values && !val.is_a?(Array))
      dirty[name] = values.first
    else
      dirty[name] = values
    end
  end
  define_method("#{name}?") do
    send(depends_on) if depends_on
    self.class.on_missing(self) unless data.has_key?(name)
    !!data[name]
  end
  nil
end

#attributes(*args) ⇒ Array<Hash>

Return attributes

Parameters:

  • args (Symbol)

    :required or :optional

Returns:



164
165
166
167
168
169
170
171
172
173
# File 'lib/miasma/utils/lazy.rb', line 164

def attributes(*args)
  @attributes ||= Smash.new
  if(args.include?(:required))
    Smash[@attributes.find_all{|k,v| v[:required]}]
  elsif(args.include?(:optional))
    Smash[@attributes.find_all{|k,v| !v[:required]}]
  else
    @attributes
  end
end

#on_missing(param = nil) ⇒ Symbol

Instance method to call on missing attribute or object to call method on if set

Parameters:

  • param (Symbol, Object) (defaults to: nil)

Returns:

  • (Symbol)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/miasma/utils/lazy.rb', line 180

def on_missing(param=nil)
  if(param)
    if(param.is_a?(Symbol))
      @missing_method = param
    else
      if(@missing_method && !@calling_on_missing)
        @calling_on_missing = true
        begin
          param.send(@missing_method)
        ensure
          @calling_on_missing = false
        end
      end
      @missing_method
    end
  else
    @missing_method
  end
end

#set_attributes(attrs) ⇒ TrueClass

TODO:

need deep dup here

Directly set attribute hash

Parameters:

Returns:

  • (TrueClass)


205
206
207
208
# File 'lib/miasma/utils/lazy.rb', line 205

def set_attributes(attrs)
  @attributes = attrs.to_smash
  true
end