module Schema::Model::ClassMethods

no-doc

Public Instance Methods

add_aliases(name, options) click to toggle source
# File lib/schema/model.rb, line 91
def add_aliases(name, options)
  return unless options[:aliases]

  options[:aliases].each do |alias_name|
    add_value_to_class_method(:schema, alias_name.to_sym => options.merge(key: alias_name.to_s, alias_of: name))
    alias_method(alias_name, options[:getter])
    alias_method("#{alias_name}=", options[:setter])
  end
end
add_attribute_methods(name, options) click to toggle source
# File lib/schema/model.rb, line 77
      def add_attribute_methods(name, options)
        class_eval(
<<-STR, __FILE__, __LINE__ + 1
  def #{options[:getter]}
    #{options[:instance_variable]}
  end

  def #{options[:setter]}(v)
    #{options[:instance_variable]} = #{options[:parser]}(#{name.inspect}, parsing_errors, v)
  end
STR
        )
      end
attribute(name, type, options = {}) click to toggle source
# File lib/schema/model.rb, line 47
def attribute(name, type, options = {})
  options[:aliases] = [options[:alias]] if options.key?(:alias)

  options = ::Schema::Model.default_attribute_options(name, type)
                           .merge(
                             parser: "parse_#{type}"
                           ).merge(options)

  add_value_to_class_method(:schema, name => options)
  add_attribute_methods(name, options)
  ::Schema::Utils.add_attribute_default_methods(self, options) if options.has_key?(:default)
  add_aliases(name, options)
end
from_hash(data) click to toggle source
# File lib/schema/model.rb, line 61
def from_hash(data)
  new.update_attributes(data)
end
schema() click to toggle source
# File lib/schema/model.rb, line 28
def schema
  {}.freeze
end
schema_config() click to toggle source
# File lib/schema/model.rb, line 41
def schema_config
  {
    schema_includes: []
  }.freeze
end
schema_include(mod) click to toggle source
# File lib/schema/model.rb, line 65
def schema_include(mod)
  config = schema_config.dup
  config[:schema_includes] = config[:schema_includes] + [mod]
  redefine_class_method(:schema_config, config.freeze)
  include mod
  schema.values.each do |field_options|
    next unless field_options[:association]

    const_get(field_options[:class_name]).schema_include(mod)
  end
end
schema_with_string_keys() click to toggle source
# File lib/schema/model.rb, line 32
def schema_with_string_keys
  @schema_with_string_keys ||=
    begin
      hsh = {}
      schema.each { |field_name, field_options| hsh[field_name.to_s] = field_options }
      hsh.freeze
    end
end