module ActiveHouse::Modeling::Attributes

Public Class Methods

new(params = {}) click to toggle source
# File lib/active_house/modeling/attributes.rb, line 60
def initialize(params = {})
  @_attributes = {}
  assign_attributes(params) unless params.nil?
end

Public Instance Methods

[](key) click to toggle source
# File lib/active_house/modeling/attributes.rb, line 73
def [](key)
  get_attribute(key.to_sym)
end
[]=(key, value) click to toggle source
# File lib/active_house/modeling/attributes.rb, line 77
def []=(key, value)
  set_attribute(key.to_sym, value)
end
as_json(*_args) click to toggle source
# File lib/active_house/modeling/attributes.rb, line 65
def as_json(*_args)
  to_h
end
assign_attributes(params) click to toggle source
# File lib/active_house/modeling/attributes.rb, line 81
def assign_attributes(params)
  params.each do |key, val|
    public_send("#{key}=", val)
  end
end
attribute(name, options = {}) click to toggle source
# File lib/active_house/modeling/attributes.rb, line 37
def attribute(name, options = {})
  name = name.to_sym
  self._attribute_opts = _attribute_opts.merge(name => options)
  define_method(name) do
    get_attribute(name)
  end
  define_method("#{name}=") do |value|
    set_attribute(name, value)
  end
end
attribute_method?(name, is_setter, *args) click to toggle source
# File lib/active_house/modeling/attributes.rb, line 21
def attribute_method?(name, is_setter, *args)
  (_attribute_opts.key?(name) || @_attributes.key?(name)) && (is_setter ? args.size == 1 : true)
end
attributes(*names) click to toggle source
# File lib/active_house/modeling/attributes.rb, line 48
def attributes(*names)
  options = names.extract_options!
  names.each { |name| attribute(name, options.dup) }
end
get_attribute(name) click to toggle source
# File lib/active_house/modeling/attributes.rb, line 25
def get_attribute(name)
  @_attributes[name]
end
load!(params) click to toggle source
# File lib/active_house/modeling/attributes.rb, line 53
def load!(params)
  new.tap do |model|
    params.each { |name, value| model[name] = value }
  end
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/active_house/modeling/attributes.rb, line 92
def method_missing(method_name, *args, &block)
  name, is_setter = parse_attribute_method_name(method_name)
  if attribute_method?(name, is_setter, *args)
    is_setter ? set_attribute(name, args.first) : get_attribute(name)
  else
    super
  end
end
parse_attribute_method_name(method_name) click to toggle source
# File lib/active_house/modeling/attributes.rb, line 14
def parse_attribute_method_name(method_name)
  name, is_setter = method_name.to_s.match(/\A(.+)?(=)?\z/).captures
  name = name.to_sym
  is_setter = !is_setter.nil?
  [name, is_setter]
end
respond_to_missing?(method_name, *args) click to toggle source
# File lib/active_house/modeling/attributes.rb, line 87
def respond_to_missing?(method_name, *args)
  name, is_setter = parse_attribute_method_name(method_name)
  attribute_method?(name, is_setter, *args)
end
set_attribute(name, value) click to toggle source
# File lib/active_house/modeling/attributes.rb, line 29
def set_attribute(name, value)
  opts = _attribute_opts.fetch(name, {})
  value = opts[:cast].call(value) if opts[:cast]
  @_attributes[name] = value
end
to_h() click to toggle source
# File lib/active_house/modeling/attributes.rb, line 69
def to_h
  @_attributes.dup
end