module Bigqueryid::Attributes

Define behaviour to attributes of entity.

Public Instance Methods

add_field(name, options) click to toggle source
# File lib/bigqueryid/attributes.rb, line 56
def add_field(name, options)
  attributes[name] = options
  create_accessors(name)
end
attributes=(attributes) click to toggle source
# File lib/bigqueryid/attributes.rb, line 15
def attributes=(attributes)
  if attributes.is_a? ::Hash
    attributes.each_pair { |key, value| send("#{key}=", value) }
  else
    raise Bigqueryid::Errors::BigqueryError.new 'Attributes params must be a Hash'
  end
rescue
  raise Bigqueryid::Errors::BigqueryError.new 'Attribute invalid'
end
attributes_names() click to toggle source
# File lib/bigqueryid/attributes.rb, line 25
def attributes_names
  attributes.keys
end
create_accessors(name) click to toggle source

www.leighhalliday.com/ruby-metaprogramming-creating-methods

# File lib/bigqueryid/attributes.rb, line 62
def create_accessors(name)
  define_method(name) do # Define get method
    instance_variable_get("@#{name}")
  end

  define_method("#{name}=") do |value| # Define set method
    instance_variable_set("@#{name}", coercer.coerce(name, value))
  end
end
field(name, options = {}) click to toggle source
# File lib/bigqueryid/attributes.rb, line 52
def field(name, options = {})
  add_field(name.to_s, options)
end
set_default_values() click to toggle source
# File lib/bigqueryid/attributes.rb, line 29
def set_default_values
  attributes.each_pair do |name, options|
    next unless options.key? :default

    default = options[:default]
    # Default might be a lambda
    value = default.respond_to?(:call) ? default.call : default
    send("#{name}=", value)
  end
end
to_hash() click to toggle source
# File lib/bigqueryid/attributes.rb, line 40
def to_hash
  hash = {}
  attributes_names.each do |property|
    hash[property] = send(property)
  end
  hash.sort.to_h
end