Class: Mockumentary::Model
- Inherits:
-
Hashie::Mash
- Object
- Hashie::Mash
- Mockumentary::Model
- Defined in:
- lib/mockumentary/model.rb
Direct Known Subclasses
Constant Summary collapse
- DUMP_NAME =
'mockumentary.yml'
Class Method Summary collapse
- .build(klass) ⇒ Object
- .classes ⇒ Object
- .constantize(klass) ⇒ Object
- .evaluate(opts) ⇒ Object
- .fake_data(key) ⇒ Object
- .generate(klass) ⇒ Object
- .init_defaults ⇒ Object
- .init_opts ⇒ Object
- .mock(opts = {}) ⇒ Object
- .mock!(opts = {}) ⇒ Object
- .mock_defaults ⇒ Object
- .mock_opts ⇒ Object
- .overrides ⇒ Object
- .relationships ⇒ Object
- .save_defaults ⇒ Object
- .save_fields ⇒ Object
- .save_opts ⇒ Object
- .uid ⇒ Object
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Model
constructor
A new instance of Model.
- #save(opts = {}) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Model
Returns a new instance of Model.
99 100 101 102 103 104 |
# File 'lib/mockumentary/model.rb', line 99 def initialize(opts={}) super(self.class.init_opts.merge(opts)) self.class.relationships.each do |key, value| send("#{key}=", value.call) end end |
Class Method Details
.build(klass) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/mockumentary/model.rb', line 32 def self.build(klass) class_eval <<-RUBY class #{container_name}::#{klass} < #{container_name}; end classes << #{container_name}::#{klass} RUBY end |
.classes ⇒ Object
28 29 30 |
# File 'lib/mockumentary/model.rb', line 28 def self.classes @classes ||= [] end |
.constantize(klass) ⇒ Object
43 44 45 |
# File 'lib/mockumentary/model.rb', line 43 def self.constantize(klass) classes.detect{|c| c.to_s == "#{container_name}::#{klass}"} end |
.evaluate(opts) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/mockumentary/model.rb', line 73 def self.evaluate(opts) opts.inject({}) do |result, arr| result[arr.first] = fake_data(arr.last) result end end |
.fake_data(key) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mockumentary/model.rb', line 57 def self.fake_data(key) data = Mockumentary::Data.generate(key) unless data data = if key == :uid uid elsif key.respond_to?(:call) key.respond_to?(:call) else key end end data end |
.generate(klass) ⇒ Object
5 6 7 8 9 |
# File 'lib/mockumentary/model.rb', line 5 def self.generate(klass) mock_class = constantize klass build klass unless mock_class mock_class ||= constantize klass end |
.init_defaults ⇒ Object
11 12 13 14 15 |
# File 'lib/mockumentary/model.rb', line 11 def self.init_defaults @init_defaults ||= { :new_record => true } end |
.init_opts ⇒ Object
87 88 89 90 91 |
# File 'lib/mockumentary/model.rb', line 87 def self.init_opts opts = init_defaults.dup opts.merge!(overrides[:init]) if overrides && overrides[:init] evaluate(opts) end |
.mock(opts = {}) ⇒ Object
106 107 108 |
# File 'lib/mockumentary/model.rb', line 106 def self.mock(opts={}) new(mock_opts.merge(opts)) end |
.mock!(opts = {}) ⇒ Object
110 111 112 113 |
# File 'lib/mockumentary/model.rb', line 110 def self.mock!(opts={}) instance = mock(opts) instance.save(opts) end |
.mock_defaults ⇒ Object
17 18 19 |
# File 'lib/mockumentary/model.rb', line 17 def self.mock_defaults @mock_defaults ||= {} end |
.mock_opts ⇒ Object
80 81 82 83 84 85 |
# File 'lib/mockumentary/model.rb', line 80 def self.mock_opts opts = init_defaults.dup opts.merge!(mock_defaults) opts.merge!(overrides[:mock]) if overrides && overrides[:mock] evaluate(opts) end |
.overrides ⇒ Object
53 54 55 |
# File 'lib/mockumentary/model.rb', line 53 def self.overrides {} end |
.relationships ⇒ Object
39 40 41 |
# File 'lib/mockumentary/model.rb', line 39 def self.relationships @relationships ||= {} end |
.save_defaults ⇒ Object
21 22 23 24 25 26 |
# File 'lib/mockumentary/model.rb', line 21 def self.save_defaults @save_defaults ||= { :new_record => false, :id => :uid } end |
.save_fields ⇒ Object
122 123 124 |
# File 'lib/mockumentary/model.rb', line 122 def self.save_fields @save_fields ||= [:id, :created_at, :updated_at] end |
.save_opts ⇒ Object
93 94 95 96 97 |
# File 'lib/mockumentary/model.rb', line 93 def self.save_opts opts = save_defaults.dup opts.merge!(overrides[:save]) if overrides && overrides[:save] evaluate(opts) end |
.uid ⇒ Object
47 48 49 50 51 |
# File 'lib/mockumentary/model.rb', line 47 def self.uid @uid ||= 0 @uid += 1 @uid end |
Instance Method Details
#save(opts = {}) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/mockumentary/model.rb', line 115 def save(opts={}) self.class.save_opts.merge(opts).each do |key, value| self[key] = value end self end |