Class: BqFactory::Record

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bq_factory/record.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema, params = {}) ⇒ Record

Returns a new instance of Record.

Raises:

  • (ArgumentError.new)


9
10
11
12
13
14
15
16
17
18
# File 'lib/bq_factory/record.rb', line 9

def initialize(schema, params = {})
  raise ArgumentError.new, "Schema is not Array" unless schema.is_a? Array

  schema.each do |hash|
    column = OpenStruct.new(hash)
    items[column.name] = Attribute.new(column.name, column.type)
  end

  params.each { |key, value| send(:"#{key}=", value) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



33
34
35
36
37
38
# File 'lib/bq_factory/record.rb', line 33

def method_missing(method_name, *args, &block)
  name = trim_equal(method_name)
  return super unless respond_to?(name)
  return items[name].value = args.first if include_equal?(method_name)
  items[name].value
end

Instance Method Details

#each(&block) ⇒ Object



29
30
31
# File 'lib/bq_factory/record.rb', line 29

def each(&block)
  items.values.uniq.each(&block)
end

#find(name) ⇒ Object Also known as: []



20
21
22
23
24
25
26
# File 'lib/bq_factory/record.rb', line 20

def find(name)
  if include?(name)
    items[name].value
  else
    raise ArgumentError.new, "#{name} is not attribute"
  end
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/bq_factory/record.rb', line 40

def respond_to?(method_name)
  include? trim_equal(method_name) || super
end

#to_sqlObject



44
45
46
# File 'lib/bq_factory/record.rb', line 44

def to_sql
  "SELECT #{items.values.map(&:to_sql).join(', ')}"
end