From 93a35dd3513db217f989a20c11ba822391fbd425 Mon Sep 17 00:00:00 2001 From: TheNotary Date: Thu, 2 Jun 2016 17:28:01 -0700 Subject: [PATCH 1/7] indicates the thought patterns of someone with an ActiveRecord background --- examples/rogue_associations.rb | 67 ++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 examples/rogue_associations.rb diff --git a/examples/rogue_associations.rb b/examples/rogue_associations.rb new file mode 100644 index 0000000..c74f4f8 --- /dev/null +++ b/examples/rogue_associations.rb @@ -0,0 +1,67 @@ +# This attempts to illustrate the redis way of... +# - has_one 1:1 (Rogue - equipment) +# - has_many through (Rogue - inventory) 1:1 many:many 1:1 +# - has_one 1:many (equipment - item) +# - Mixin usage to promote DRY code + +require "ohm" + +def battleable + attribute :name; index :name + attribute :hp + attribute :mp + list :inventory, :Item + + reference :equipment, :Loadout +end + + +# We define both a `Video` and `Audio` model, with a `list` of *comments*. +class Rogue < Ohm::Model + battleable # give Rogues all the attributes required to conduct battle +end + +class Monster < Ohm::Model + battleable # give monsters attributes required to conduct battle +end + +class Loadout < Ohm::Model + reference :head, :Item + reference :right_hand, :Item + reference :left_hand, :Item + reference :body, :Item + reference :legs, :Item + reference :feet, :Item + reference :wrist, :Item + reference :neck, :Item +end + +class Item < Ohm::Model + attribute :name + attribute :dmg + attribute :def + attribute :value +end + + +# Now let's require the test framework we're going to use called +# [cutest](http://github.com/djanowski/cutest) +require "cutest" + +# And make sure that every run of our test suite has a clean Redis instance. +prepare { Ohm.flush } + +test "all works" do + rogue = Rogue.create + + # TODO: can this be initialized on creation of a Rogue? + rogue.equipment = Loadout.create + + # TODO: + # 1) Can 'starting leggins' come automatic? + # 2) Is there a find_or_create method available so users don't wind up + # making an ineffient qty of Items? + rogue.equipment.legs = Item.create(name: "Starting Leggings", def: "1") + + # assert_equal audio.comments.size, 1 +end From cb1f5f03ca3b2bb829f32afd18e53a0b74f9e08f Mon Sep 17 00:00:00 2001 From: TheNotary Date: Thu, 2 Jun 2016 17:34:36 -0700 Subject: [PATCH 2/7] demonstrates casting to integer --- examples/rogue_associations.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/rogue_associations.rb b/examples/rogue_associations.rb index c74f4f8..fbaa0b4 100644 --- a/examples/rogue_associations.rb +++ b/examples/rogue_associations.rb @@ -8,11 +8,11 @@ def battleable attribute :name; index :name - attribute :hp - attribute :mp - list :inventory, :Item + attribute :hp, lambda { |x| x.to_i } # cast hp to integer + attribute :mp, lambda { |x| x.to_i } + list :inventory, :Item - reference :equipment, :Loadout + reference :equipment, :Loadout end @@ -38,9 +38,9 @@ class Loadout < Ohm::Model class Item < Ohm::Model attribute :name - attribute :dmg - attribute :def - attribute :value + attribute :dmg, lambda { |x| x.to_i } + attribute :def, lambda { |x| x.to_i } + attribute :value, lambda { |x| x.to_i } end From d9580da31f3692dcac8384e4fbacd4a3d67e5f40 Mon Sep 17 00:00:00 2001 From: TheNotary Date: Wed, 15 Jun 2016 14:12:35 -0700 Subject: [PATCH 3/7] added a gemfile so bundle install would work as expected --- Gemfile | 4 ++++ Gemfile.lock | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..8059bef --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in adventure_channel.gemspec +gemspec diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..50a20ed --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,30 @@ +PATH + remote: . + specs: + ohm (3.0.2) + nido + redic (~> 1.5.0) + stal + +GEM + remote: https://rubygems.org/ + specs: + clap (1.0.0) + cutest (1.2.3) + clap + hiredis (0.6.1) + nido (1.0.0) + redic (1.5.0) + hiredis + stal (0.1.0) + redic + +PLATFORMS + ruby + +DEPENDENCIES + cutest + ohm! + +BUNDLED WITH + 1.12.4 From dc1241e47b92841b7b8def03662ff7f137af7b4e Mon Sep 17 00:00:00 2001 From: TheNotary Date: Wed, 15 Jun 2016 15:36:42 -0700 Subject: [PATCH 4/7] made assertion that works, fixed flaws, closer to final draft? --- examples/rogue_associations.rb | 51 ++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/examples/rogue_associations.rb b/examples/rogue_associations.rb index fbaa0b4..fd7136c 100644 --- a/examples/rogue_associations.rb +++ b/examples/rogue_associations.rb @@ -12,27 +12,45 @@ def battleable attribute :mp, lambda { |x| x.to_i } list :inventory, :Item + attribute :equipment_id reference :equipment, :Loadout end - # We define both a `Video` and `Audio` model, with a `list` of *comments*. class Rogue < Ohm::Model battleable # give Rogues all the attributes required to conduct battle + + # instead of using Rogue.create, favor Rogue.spawn + def self.spawn(name: nil) + rogue = self.create(name: name) + rogue.equipment = Loadout.create + rogue.save + rogue + end end class Monster < Ohm::Model battleable # give monsters attributes required to conduct battle + + end class Loadout < Ohm::Model + attribute :head_id reference :head, :Item + attribute :right_hand_id reference :right_hand, :Item + attribute :left_hand_id reference :left_hand, :Item + attribute :body_id reference :body, :Item + attribute :legs_id reference :legs, :Item + attribute :feet_id reference :feet, :Item + attribute :wrist_id reference :wrist, :Item + attribute :neck_id reference :neck, :Item end @@ -41,27 +59,38 @@ class Item < Ohm::Model attribute :dmg, lambda { |x| x.to_i } attribute :def, lambda { |x| x.to_i } attribute :value, lambda { |x| x.to_i } + + def sell_to(target) + original_owner = "hmmm...." + end end + + # Now let's require the test framework we're going to use called # [cutest](http://github.com/djanowski/cutest) require "cutest" + # And make sure that every run of our test suite has a clean Redis instance. prepare { Ohm.flush } -test "all works" do - rogue = Rogue.create - - # TODO: can this be initialized on creation of a Rogue? - rogue.equipment = Loadout.create +test "a rogue can be equipped with leggins" do + rogue = Rogue.spawn - # TODO: - # 1) Can 'starting leggins' come automatic? - # 2) Is there a find_or_create method available so users don't wind up - # making an ineffient qty of Items? rogue.equipment.legs = Item.create(name: "Starting Leggings", def: "1") - # assert_equal audio.comments.size, 1 + # TODO: Should I do something to make saving a parent also save children? + rogue.save + rogue.equipment.save + + rogue = Rogue[rogue.id] # lookup the rogue from the DB to make sure things are persistent + assert_equal rogue.equipment.legs.def, 1 +end + +test "a rogue can sell her gear" do + rogue = Rogue.spawn + + end From 6f763ee519cdd89c003042afd4763040f8b40d4a Mon Sep 17 00:00:00 2001 From: TheNotary Date: Thu, 16 Jun 2016 09:30:29 -0700 Subject: [PATCH 5/7] reverts d9580da per discussion on bundler --- Gemfile | 4 ---- Gemfile.lock | 30 ------------------------------ 2 files changed, 34 deletions(-) delete mode 100644 Gemfile delete mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile deleted file mode 100644 index 8059bef..0000000 --- a/Gemfile +++ /dev/null @@ -1,4 +0,0 @@ -source 'https://rubygems.org' - -# Specify your gem's dependencies in adventure_channel.gemspec -gemspec diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index 50a20ed..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,30 +0,0 @@ -PATH - remote: . - specs: - ohm (3.0.2) - nido - redic (~> 1.5.0) - stal - -GEM - remote: https://rubygems.org/ - specs: - clap (1.0.0) - cutest (1.2.3) - clap - hiredis (0.6.1) - nido (1.0.0) - redic (1.5.0) - hiredis - stal (0.1.0) - redic - -PLATFORMS - ruby - -DEPENDENCIES - cutest - ohm! - -BUNDLED WITH - 1.12.4 From 5ec474a1c167e7c910ce7509ada8725bb24dfc7d Mon Sep 17 00:00:00 2001 From: TheNotary Date: Thu, 16 Jun 2016 09:47:52 -0700 Subject: [PATCH 6/7] removed unneeded *_id attributes --- examples/rogue_associations.rb | 9 --------- 1 file changed, 9 deletions(-) diff --git a/examples/rogue_associations.rb b/examples/rogue_associations.rb index fd7136c..17a0994 100644 --- a/examples/rogue_associations.rb +++ b/examples/rogue_associations.rb @@ -12,7 +12,6 @@ def battleable attribute :mp, lambda { |x| x.to_i } list :inventory, :Item - attribute :equipment_id reference :equipment, :Loadout end @@ -36,21 +35,13 @@ class Monster < Ohm::Model end class Loadout < Ohm::Model - attribute :head_id reference :head, :Item - attribute :right_hand_id reference :right_hand, :Item - attribute :left_hand_id reference :left_hand, :Item - attribute :body_id reference :body, :Item - attribute :legs_id reference :legs, :Item - attribute :feet_id reference :feet, :Item - attribute :wrist_id reference :wrist, :Item - attribute :neck_id reference :neck, :Item end From 22ad59cc688d3add46335dbc5cd554caca240866 Mon Sep 17 00:00:00 2001 From: TheNotary Date: Thu, 22 Sep 2016 16:08:40 -0500 Subject: [PATCH 7/7] cleaned up comments a bit --- examples/rogue_associations.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/rogue_associations.rb b/examples/rogue_associations.rb index 17a0994..9b7acb8 100644 --- a/examples/rogue_associations.rb +++ b/examples/rogue_associations.rb @@ -6,6 +6,7 @@ require "ohm" +# We use this method as a sort of 'mixin' for the Rogue and Monster classes def battleable attribute :name; index :name attribute :hp, lambda { |x| x.to_i } # cast hp to integer @@ -15,7 +16,7 @@ def battleable reference :equipment, :Loadout end -# We define both a `Video` and `Audio` model, with a `list` of *comments*. + class Rogue < Ohm::Model battleable # give Rogues all the attributes required to conduct battle @@ -30,8 +31,6 @@ def self.spawn(name: nil) class Monster < Ohm::Model battleable # give monsters attributes required to conduct battle - - end class Loadout < Ohm::Model @@ -83,5 +82,5 @@ def sell_to(target) test "a rogue can sell her gear" do rogue = Rogue.spawn - + # TODO: test for ability to sell gear end