Module: Finexclub::Document::ClassMethods

Defined in:
lib/finexclub/document.rb

Instance Method Summary collapse

Instance Method Details

#define_attr_reader(name) ⇒ Object



20
21
22
23
24
# File 'lib/finexclub/document.rb', line 20

def define_attr_reader(name)
  define_method "#{name}" do
    instance_variable_get("@#{name}")
  end
end

#define_attr_writer(name, converter = nil) ⇒ Object



26
27
28
29
30
31
# File 'lib/finexclub/document.rb', line 26

def define_attr_writer(name, converter = nil)
  define_method "#{name}=" do |value|
    value = converter.nil?? value : converter.call(value)
    instance_variable_set("@#{name}", value)
  end
end

#define_float_fields(name) ⇒ Object



43
44
45
46
# File 'lib/finexclub/document.rb', line 43

def define_float_fields(name)
  define_attr_reader(name)
  define_attr_writer(name, lambda{|x| x.to_f})
end

#define_image_fields(name) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/finexclub/document.rb', line 78

def define_image_fields(name)
  define_attr_reader(name)
  define_attr_writer(name)

  define_method "#{name}_filename=" do |filename|
    image_uid = core.images.store(filename)
    instance_variable_set("@#{name}", image_uid)
  end

  define_method "#{name}_image" do
    image_uid = instance_variable_get("@#{name}")
    core.images.fetch(image_uid)
  end
end

#define_integer_fields(name) ⇒ Object



38
39
40
41
# File 'lib/finexclub/document.rb', line 38

def define_integer_fields(name)
  define_attr_reader(name)
  define_attr_writer(name, lambda{|x| x.to_i})
end

#define_string_fields(name) ⇒ Object



48
49
50
51
# File 'lib/finexclub/document.rb', line 48

def define_string_fields(name)
  define_attr_reader(name)
  define_attr_writer(name, lambda{|x| x.to_s})
end

#define_symbol_fields(name) ⇒ Object



33
34
35
36
# File 'lib/finexclub/document.rb', line 33

def define_symbol_fields(name)
  define_attr_reader(name)
  define_attr_writer(name, lambda{|x| x.downcase})
end

#define_timestamp_fields(name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/finexclub/document.rb', line 53

def define_timestamp_fields(name)
  define_attr_reader(name)
  define_attr_writer(name, lambda{|x| x.to_i})

  define_method "#{name}_at" do
    timestamp = instance_variable_get("@#{name}")
    Time.at(timestamp).utc
  end

  define_method "#{name}_date" do
    send("#{name}_at").strftime("%Y-%m-%d")
  end
end

#define_trade_action_fields(name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/finexclub/document.rb', line 67

def define_trade_action_fields(name)
  define_attr_reader(name)
  define_attr_writer(name, lambda{|x| x.to_s})

  %w(buy sell hold_buy hold_sell stop).each do |action|
    define_method "#{name}_#{action}?" do
      send("#{name}") == action
    end
  end
end

#doc_fields(*fields) ⇒ Object



106
107
108
# File 'lib/finexclub/document.rb', line 106

def doc_fields(*fields)
  @doc_fields = fields
end

#export_fieldsObject



102
103
104
# File 'lib/finexclub/document.rb', line 102

def export_fields
  @doc_fields ||= []
end

#field(name, field_type) ⇒ Object



93
94
95
96
# File 'lib/finexclub/document.rb', line 93

def field(name, field_type)
  meta[name] = field_type
  send "define_#{field_type}_fields", name
end

#metaObject



98
99
100
# File 'lib/finexclub/document.rb', line 98

def meta
  @meta ||= {}
end