Class: Rubiks::NamedObject

Inherits:
Object
  • Object
show all
Defined in:
lib/rubiks/named_object.rb

Direct Known Subclasses

CalculatedMeasure, Cube, Dimension, Hierarchy, Level, Measure, Schema

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_name = nil, options = {}) ⇒ NamedObject

Returns a new instance of NamedObject.



39
40
41
42
# File 'lib/rubiks/named_object.rb', line 39

def initialize(new_name = nil, options = {})
  @name = new_name.to_s if new_name.present?
  @options = options.symbolize_keys
end

Class Method Details

.[](instance_name) ⇒ Object



35
36
37
# File 'lib/rubiks/named_object.rb', line 35

def self.[](instance_name)
  instances[instance_name.to_sym]
end

.clear!Object



23
24
25
# File 'lib/rubiks/named_object.rb', line 23

def self.clear!
  @instances = nil
end

.defaultObject



27
28
29
30
31
32
33
# File 'lib/rubiks/named_object.rb', line 27

def self.default
  if instances.has_key?('default')
    instances['default']
  elsif instances.present?
    instances.first[1]
  end
end

.define(new_name = nil, options = {}, &block) ⇒ Object



4
5
6
7
8
9
# File 'lib/rubiks/named_object.rb', line 4

def self.define(new_name=nil, options={}, &block)
  instance = new(new_name, options)
  instance.instance_eval(&block) if block_given?
  instances[instance.name.to_sym] = instance
  instance
end

.find_or_create(instance_name, options = {}, &block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/rubiks/named_object.rb', line 15

def self.find_or_create(instance_name, options={}, &block)
  return instances[instance_name.to_sym] if instances.has_key?(instance_name.to_sym)

  new_instance = new(instance_name.to_s, options)
  new_instance.instance_eval(&block) if block_given?
  new_instance
end

.instancesObject



11
12
13
# File 'lib/rubiks/named_object.rb', line 11

def self.instances
  @instances ||= Hash.new
end

Instance Method Details

#caption(new_value = nil) ⇒ Object



69
70
71
72
# File 'lib/rubiks/named_object.rb', line 69

def caption(new_value=nil)
  @caption = new_value if new_value.present?
  @caption ||= @options[:caption] || name.titleize
end

#column(new_value = nil) ⇒ Object



64
65
66
67
# File 'lib/rubiks/named_object.rb', line 64

def column(new_value=nil)
  @column = new_value.to_s if new_value.present?
  @column ||= @options[:column] || name
end

#default_json_attributesObject Also known as: json_hash



87
88
89
90
91
92
93
94
95
96
# File 'lib/rubiks/named_object.rb', line 87

def default_json_attributes
  json_attrs = {
    :name => caption,
    :description => description,
    :icon_type => icon_type
  }
  json_attrs[:hidden] = hidden if hidden.present? && hidden == 'true'
  json_attrs.delete_if { |key,value| value.nil? }
  json_attrs.stringify_keys!
end

#default_xml_attributesObject Also known as: xml_hash



99
100
101
102
103
104
105
# File 'lib/rubiks/named_object.rb', line 99

def default_xml_attributes
  xml_attrs = {
    :name => caption,
    :description => description
  }
  xml_attrs.delete_if { |key,value| value.nil? }
end

#description(new_value = nil) ⇒ Object



54
55
56
57
# File 'lib/rubiks/named_object.rb', line 54

def description(new_value=nil)
  @description = new_value.to_s if new_value.present?
  @description ||= @options[:description]
end

#hidden(new_value = nil) ⇒ Object



59
60
61
62
# File 'lib/rubiks/named_object.rb', line 59

def hidden(new_value=nil)
  @hidden = new_value.to_s unless new_value.nil?
  @hidden ||= @options.key?(:hidden) ? @options[:hidden].to_s : nil
end

#icon_type(new_value = nil) ⇒ Object



49
50
51
52
# File 'lib/rubiks/named_object.rb', line 49

def icon_type(new_value=nil)
  @icon_type = new_value.to_s if new_value.present?
  @icon_type ||= @options[:icon_type]
end

#name(new_value = nil) ⇒ Object



44
45
46
47
# File 'lib/rubiks/named_object.rb', line 44

def name(new_value=nil)
  @name = new_value.to_s if new_value.present?
  @name ||= @options[:name] || 'default'
end

#table(new_value = nil) ⇒ Object



74
75
76
77
# File 'lib/rubiks/named_object.rb', line 74

def table(new_value=nil)
  @table = new_value if new_value.present?
  @table ||= @options[:table] || name.tableize
end

#table_with_prefixObject



79
80
81
# File 'lib/rubiks/named_object.rb', line 79

def table_with_prefix
  "#{Rubiks.table_prefix}#{table}"
end

#to_json(options = {}) ⇒ Object



83
84
85
# File 'lib/rubiks/named_object.rb', line 83

def to_json(options={})
  MultiJson.encode(json_hash, options)
end

#to_xml(builder = nil) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/rubiks/named_object.rb', line 108

def to_xml(builder = nil)
  return if name.blank?

  builder = builder || new_builder

  builder.__send__(name)
end