Module: Voynich::ActiveModel::Model::ClassMethods

Defined in:
lib/voynich/active_model/model.rb

Instance Method Summary collapse

Instance Method Details

#voynich_attribute(name, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/voynich/active_model/model.rb', line 52

def voynich_attribute(name, options = {})
  options = VOYNICH_DEFAULT_OPTIONS.merge(options)
  voynich_targets[name.to_sym] = options
  asoc_options = options.
                 merge(class_name: "::Voynich::ActiveRecord::Value").
                 reject{|k| VOYNICH_DEFAULT_OPTIONS.keys.include? k}

  belongs_to :"#{voynich_column_name(name)}", asoc_options

  private :"#{voynich_column_name(name)}="

  define_method(name) do
    value = send(voynich_column_name(name))
    iv = instance_variable_get(:"@#{name}")
    return iv unless iv.nil?
    return nil if value.nil?
    value.context = voynich_context(name)
    instance_variable_set(:"@#{name}", value.decrypt)
  end

  define_method("#{name}=") do |val|
    instance_variable_set(:"@#{name}", val)
  end

  define_method("#{name}?") do
    value = send(name)
    value.respond_to?(:empty?) ? !value.empty? : !!value
  end
end

#voynich_column_name(name) ⇒ Object



47
48
49
50
# File 'lib/voynich/active_model/model.rb', line 47

def voynich_column_name(name)
  options = voynich_targets[name.to_sym]
  "#{options[:column_prefix]}#{name}#{options[:column_suffix]}"
end

#voynich_targetsObject



43
44
45
# File 'lib/voynich/active_model/model.rb', line 43

def voynich_targets
  @voynich_targets ||= {}
end