Skip to content

Commit

Permalink
fix(builder): fix call configure with file and block
Browse files Browse the repository at this point in the history
  • Loading branch information
icyleaf committed Nov 2, 2018
1 parent cecc028 commit 8ab6d86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
28 changes: 19 additions & 9 deletions spec/totem/config_builder_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
require "../spec_helper"

module ConfigBuilderSpec
struct Config
include Totem::ConfigBuilder

build do
config_type "yaml"
config_paths ["/etc/totem", "~/.totem", "spec/fixtures"]
end
end

struct Clothes
include JSON::Serializable

Expand All @@ -19,15 +28,6 @@ module ConfigBuilderSpec
config_paths ["/etc/totem", "~/.totem", "spec/fixtures"]
end
end

struct Config
include Totem::ConfigBuilder

build do
config_type "yaml"
config_paths ["/etc/totem", "~/.totem", "spec/fixtures"]
end
end
end

describe Totem::ConfigBuilder do
Expand Down Expand Up @@ -60,6 +60,16 @@ describe Totem::ConfigBuilder do
config["hobbies"].as_a.last.should eq "go"
config["clothing"].as_h["jacket"].should eq "leather"
end

it "should works with given file and block" do
config = ConfigBuilderSpec::Config.configure("spec/fixtures/config.json") do |c|
c.set("name", "tavares")
end

config["name"].should eq "tavares"
config["batters"].size.should eq 1
config["batters"].as_h["batter"].as_a.first.as_h["type"].should eq "Regular"
end
end

describe "mapping" do
Expand Down
8 changes: 4 additions & 4 deletions src/totem/config_builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ module Totem
include JSON::Serializable

def self.configure(file : String)
load_wit_file(file)
load_with_file(file)
configure
end

def self.configure(file : String, &block)
load_wit_file(file)
def self.configure(file : String, &block : Totem::Config -> _)
load_with_file(file)
configure(&block)
end

Expand All @@ -71,7 +71,7 @@ module Totem
@@config.mapping(self)
end

private def self.load_wit_file(file)
private def self.load_with_file(file)
config_path = File.dirname(file)
config_name = File.basename(file, File.extname(file))
config_type = config_type(file)
Expand Down

0 comments on commit 8ab6d86

Please sign in to comment.