module IB::BaseProperties

Module adds prop Macro and

Public Class Methods

define_property(names, body) click to toggle source
# File lib/ib/base_properties.rb, line 79
def self.define_property names, body
  aliases = [names].flatten
  name = aliases.shift
  instance_eval do

    define_property_methods name, body

    aliases.each do |ali|
      alias_method "#{ali}", name
      alias_method "#{ali}=", "#{name}="
    end
  end
end
define_property_methods(name, body={}) click to toggle source
# File lib/ib/base_properties.rb, line 93
def self.define_property_methods name, body={}
  #p name, body
  case body
  when '' # default getter and setter
    define_property_methods name

  when Array # [setter, getter, validators]
    define_property_methods name,
      :get => body[0],
      :set => body[1],
      :validate => body[2]

  when Hash # recursion base case
    getter = case # Define getter
    when body[:get].respond_to?(:call)
      body[:get]
    when body[:get]
      proc { self[name].send "to_#{body[:get]}" }
    when VALUES[name] # property is encoded
      proc { VALUES[name][self[name]] }
    else
      proc { self[name] }
    end
    define_method name, &getter if getter

    setter = case # Define setter
    when body[:set].respond_to?(:call)
      body[:set]
    when body[:set]
      proc { |value| self[name] = value.send "to_#{body[:set]}" }
    when CODES[name] # property is encoded
      proc { |value| self[name] = CODES[name][value] || value }
    else
      proc { |value| self[name] = value } # p name, value;
    end
    define_method "#{name}=", &setter if setter

    # Define validator(s)
    [body[:validate]].flatten.compact.each do |validator|
      case validator
      when Proc
        validates_each name, &validator
      when Hash
        validates name, validator.dup
      end
    end

    # TODO define self[:name] accessors for :virtual and :flag properties

  else # setter given
    define_property_methods name, :set => body, :get => body
  end
end
prop(*properties) click to toggle source

Class macros

# File lib/ib/base_properties.rb, line 72
def self.prop *properties
  prop_hash = properties.last.is_a?(Hash) ? properties.pop : {}

  properties.each { |names| define_property names, nil }
  prop_hash.each { |names, type| define_property names, type }
end

Public Instance Methods

==(other) click to toggle source

Default Model comparison

Calls superclass method
# File lib/ib/base_properties.rb, line 41
def == other
  case other
  when String # Probably a Rails URI, delegate to AR::Base
    super(other)
  else
    content_attributes.keys.inject(true) { |res, key|
      res && other.respond_to?(key) && (send(key) == other.send(key)) }
  end
end
content_attributes() click to toggle source

Comparison support

# File lib/ib/base_properties.rb, line 24
def content_attributes
  HashWithIndifferentAccess[attributes.reject do |(attr, _)|
                              attr.to_s =~ /(_count)\z/ ||
                                [:created_at, :updated_at, :type,
                                 :id, :order_id, :contract_id].include?(attr.to_sym)
  end]
end
default_attributes() click to toggle source

Default attributes support

# File lib/ib/base_properties.rb, line 53
def default_attributes
  {:created_at => Time.now,
   :updated_at => Time.now,
   }
end
set_attribute_defaults() click to toggle source
# File lib/ib/base_properties.rb, line 59
def set_attribute_defaults
  default_attributes.each do |key, val|
    self.send("#{key}=", val) if self.send(key).nil?
    # self.send("#{key}=", val) if self[key].nil? # Problems with association defaults
  end
end
to_human() click to toggle source

Default presentation

# File lib/ib/base_properties.rb, line 17
def to_human
  "<#{self.class.to_s.demodulize}: " + attributes.map do |attr, value|
    "#{attr}: #{value}" unless value.nil?
  end.compact.sort.join(' ') + ">"
end
update_missing(attrs) click to toggle source

Update nil attributes from given Hash or model

# File lib/ib/base_properties.rb, line 33
def update_missing attrs
  attrs = attrs.content_attributes unless attrs.kind_of?(Hash)

  attrs.each { |attr, val| send "#{attr}=", val if send(attr).blank? }
  self # for chaining
end